[Introduction to Dockerfile of Docker]

Dockerfile basic structure

Generally, a Dockerfile is divided into four parts: basic image information, maintainer information, image operation instructions, and execution instructions when the container starts . # is a comment in the Dockerfile.



 

simple example file 

# This my first nginx Dockerfile

# Version 1.0

 

# Base images

FROM centos

 

#MAINTAINER maintainer information

MAINTAINER tianfeiyu 

 

#ENV Set environment variables

ENV PATH /usr/local/nginx/sbin:$PATH

 

The #ADD file is placed in the current directory, and it will be automatically decompressed after copying it

ADD nginx-1.8.0.tar.gz /usr/local/  

ADD epel-release-latest-7.noarch.rpm /usr/local/  

 

#RUN execute the following commands 

RUN rpm -ivh /usr/local/epel-release-latest-7.noarch.rpm

RUN yum install -y wget lftp gcc gcc-c++ make openssl-devel pcre-devel pcre && yum clean all

RUN useradd -s /sbin/nologin -M www

 

#WORKDIR is equivalent to cd

WORKDIR /usr/local/nginx-1.8.0 

 

RUN ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-pcre && make && make install

 

RUN echo "daemon off;" >> /etc/nginx.conf

 

#EXPOSE map port

EXPOSE 80

 

#CMD run the following command

CMD ["nginx"]

 



 

 

FROM : Specify the base image, in which image to build

The format is FROM <image> or FROM <image>:<tag>.

The first instruction must be a FROM instruction.

 

MAINTAINER : Specify maintainer information

The format is MAINTAINER <name>

RUN : the command to execute in the mirror

The format is RUN <command> or RUN ["executable", "param1", "param2"]

 

The former will run the command in the shell terminal, ie /bin/bash -c; the latter will be executed using exec. Specifying the use of other terminals can be achieved in the second way, such as RUN ["/bin/bash", "-c", "echo hello"] .

 

WORKDIR : Specifies the current working directory, equivalent to cd

The format is WORKDIR /path/to/workdir

Configure the working directory for subsequent RUN , CMD , ENTRYPOINT instructions.

Multiple WORKDIR directives can be used, and subsequent commands, if the argument is a relative path, will be based on the path specified by the previous command. E.g

WORKDIR /a

WORKDIR b

WORKDIR c

RUN pwd

Then the final path is /a/b/c.

 

EXPOSE : Specifies the port to be opened by the container

The format is EXPOSE <port> [<port>...]

Tell the Docker server the port number exposed by the container for use by the interconnected system. You need to pass -P when starting the container, and the Docker host will automatically assign a port forwarding to the specified port.

 

ENV : define environment variables

The format is ENV <key> <value> . Specify an environment variable that will be used by subsequent RUN commands and persisted when the container is running.

E.g

ENV PATH /usr/local/nginx/sbin:$PATH

 

COPY : Copy the local host's (relative path to the directory where the Dockerfile is located) to the .

The format is COPY.

 

ADD : Equivalent to COPY, but more powerful than COPY

The format is ADD <src> <dest>

This command will copy the specified into the container. It can be a relative path to the directory where the Dockerfile is located; it can also be a URL; it can also be a tar file, which will be automatically decompressed when copied into the container.

 

VOLUME : mount directory

The format is VOLUME ["/data"]

Create a mount point that can be mounted from the local host or other containers, generally used to store databases and data that needs to be maintained.

 

USER

The format is USER daemon

Specify the user name or UID when running the container, and subsequent RUNs will also use the specified user. When the service does not require administrator privileges, you can specify the running user through this command. And the required user can be created before, eg: RUN useradd -s /sbin/nologin -M www.

 

ENTRYPOINT two formats:

ENTRYPOINT ["executable", "param1", "param2"]

 

ENTRYPOINT command param1 param2 (executed in shell)

Configure the command to be executed after the container is started, and cannot be overridden by the parameters provided by docker run. There can only be one ENTRYPOINT in each Dockerfile, and when multiple are specified, only the last one will take effect.

 

CMD supports three formats

CMD ["executable","param1","param2"] Use exec to execute, the recommended way;

CMD command param1 param2 is executed in /bin/bash and is provided to applications that require interaction;

CMD ["param1","param2"] default parameters provided to ENTRYPOINT;

Specifies the command to be executed when the container is started. Each Dockerfile can only have one CMD command. If multiple commands are specified, only the last one will be executed. If the user specifies the command to run when starting the container, the command specified by CMD will be overwritten.

 

ONBUILD : Does not take effect when building this image, and takes effect when building an image based on this image

 

The format is ONBUILD [INSTRUCTION]

Configure the operation instructions to be executed when the created image is used as the base image for other newly created images.

 

The difference between ENTRYPOINT and CMD : ENTRYPOINT specifies the entry when the image is started, and CMD specifies the command when the container starts. When the two are shared, the complete startup command is like ENTRYPOINT + CMD. The advantage of using ENTRYPOINT is that when we start the image, it is like starting an executable program, and only parameters need to be specified on the CMD; in addition, it is not easy to make mistakes when we need to customize the CMD.

 

Dockerfile using CMD:

[root@sta2 test]# cat Dockerfile 

FROM mysql

CMD ["echo","test"]

 

Dockerfile using ENTRYPOINT:

[root@sta2 entrypoint]#  cat  Dockerfile 

FROM mysql

ENTRYPOINT ["echo","test"]

 

Conclusion: ENTRYPOINT cannot override the parameters during execution, and CMD can override the default parameters.

 

 

 

Dockerfile

FROM base image

MAINTAINER maintains this information

RUN What command to run, add RUN in front of the command

ADD Add some files to it, copy the files, it will be automatically decompressed

WORKDIR The current working directory

VOLUME directory mount

EXPOSE open ports

RUN process to keep running

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326230835&siteId=291194637