Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic commands of Dockerfile

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what are the basic commands of Dockerfile". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Dockerfile basic command

FROM-based on the base image

FROM centos # make base image (based on centos:latest) FROM scratch # do not depend on any base image (base image), build FROM tomcat:9.0.22-jdk8-openjdk from scratch

LABEL & MAINTAINER-description information

MAINTAINER 3337777777.com # name of person or organization LABEL version= "1.0" LABEL description= "three minutes doomed, seven points depends on hard work"

WORKDIR-set the working directory

WORKDIR / usr/localWORKDIR / usr/local/newdir # destination directory does not exist and will be created automatically # try to use absolute path

ADD & COPY-copy files

ADD hello / # copy to follow the path ADD test.tar.gz / # add to the more directory and extract # ADD besides copying, it also has the function of adding remote files.

ENV-sets the environment constant

ENV JAVA_HOME / usr/local/openjdk8RUN ${JAVA_HOME} / bin/java-jar test.jar# uses environment constants as much as possible, which can improve program maintainability and Dockerfile execution instructions.

RUN & CMD & ENTRYPOINT

RUN: execute commands when Build is built

ENTRYPOINT: the command executed when the container starts

CMD: execute default commands or parameters after the container starts

Difference: different timing of execution

RUN-run at build time

RUN yum install-y vim # shell command format RUN ["yum", "install", "- y", "vim"] # exec command format

Why does Docker use two writing formats for RUN?

This involves some underlying principles of the linux system.

Shell operation mode

Exec operation mode

Using the Exec method, the current process is replaced with the Exec process, and the PID is left unchanged

After execution, exit directly, and will not return to the previous process environment.

= >

In cases where it is not clear which method to use, the Exec format is recommended.

ENTRYPOINT-start command

ENTRYPOINT (entry point) is used to execute commands when the container starts

Only the last ENTRYPOINT in the Dockerfile will be executed

ENTRYPOINT ["ps"] # recommend Exec format

CMD-default command

Commands used by CMD to set default execution

If more than one CMD appears in the Dockerfile, only the last one executes

If an instruction is attached when the container starts, the CMD is ignored

CMD ["ps", "- ef"] # recommended Exec format

Combined use of ENTRYPOINT and CMD

FROM centosENTRYPOINT ["ps"] CMD ["- ef"] # at this point, ENTRYPOINT and CMD will use ps-ef "what are the basic Dockerfile commands". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report