Dockerfile instructions and basic structure

  • 2021-01-19 22:32:27
  • OfStack

Using Dockerfile allows users to create custom images.

The basic structure

Dockerfile consists of one line of command statements and supports comment lines that begin with a #. Dockerfile is divided into four parts: basic mirroring information, maintainer information, mirroring instructions, and container startup instructions.

Such as:


//  Basic image information 
FROM daocloud.io/node:7 
//  Maintainer information 
MAINTAINER abel.yang <527515025@qq.com>
LABEL Descripttion="This image is build for web"
// Mirror operation instruction 
RUN mkdir -p /opt/apps/epp
COPY . /opt/apps/epp
WORKDIR /opt/apps/epp/epp-web/server
ENV LANG C.UTF-8
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo Asia/Shanghai > /etc/timezone
EXPOSE 3001
// Instructions are executed when the container starts 
CMD [ "npm", "start" ]

Where 1 must first indicate the name of the image based on which it is based, and then recommend the maintainer information. This is followed by mirroring instructions, such as RUN, which execute the following commands on the image.

Each time an RUN instruction is run, a new layer is added to the image and committed. Finally, there is the CMD directive, which specifies the operational commands to run the container.

instruction

INSTRUCTION arguments, instructions including FROM, MAINTAINER, RUN, etc

[

Format for FROM < image > Or FROM < image > : < tag > .

]

Instruction 1 must be FROM instruction. Also, if you create more than one image in the same Dockerfile, you can use more than one FROM directive (once per image).

MAINTAINER

The format is MAINTAINER, specifying maintainer information.

RUN

The format is RUN or RUN [" executable ", "param1", "param2"].

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

Each RUN instruction executes the specified command based on the current image and commits as a new image. You can use \ to wrap lines when the command is long.

CMD

Three formats are supported

CMD ["executable","param1","param2"] Use exec to execute, recommended; CMD command param1 param2 is implemented in /bin/sh for applications that require interaction; CMD ["param1","param2"] Default parameters provided to ENTRYPOINT;

Specifies the command to be executed when the container is started. There can be only one CMD command per Dockerfile. If multiple commands are specified, only the last one will be executed.

If the user specifies a command to run when starting the container, the command specified in CMD is overridden.

EXPOSE

[

Format for EXPOSE < port > [ < port > . ] .

]

Tells Docker the exposed port number of the server container for use by the interconnected system. When starting the container, you need to pass -P. The Docker master will automatically assign a port to be forwarded to the specified port.

ENV

[

Format for ENV < key > < value > . Specify an environment variable that will be used by subsequent RUN instructions and will be maintained during container runtime.

]

ADD

[

Format for ADD < src > < dest > .
This command will copy the specified < src > Into the container < dest > . Among them < src > It can be a relative path to the directory where Dockerfile is located. It could be 1 URL; It can also be an tar file (automatically decompressed into a directory).

]

COPY

[

Format for COPY < src > < dest > .
Copy the localhost's < src > (relative to the directory where Dockerfile is located) to the container < dest > .

]

When using the local directory as the source directory, it is recommended to use COPY.

ENTRYPOINT

Two formats:

ENTRYPOINT ["executable", "param1", "param2"] ENTRYPOINT command param1 param2 (executed in shell).

Configure commands that are executed when the container is started and cannot be overridden by arguments provided by docker.

There can be only one ENTRYPOINT in each Dockerfile, and when multiple ENTRYPOINT is specified, only the last one is in effect.

VOLUME

The format is VOLUME [" /data "].

Create a mount point that can be mounted from the localhost or other container. It can be used to store the database and the data that needs to be maintained.

USER

The format is USER daemon.

Specify the user name or UID when the container is run. Subsequent RUN will also use the specified user.

This command can be used to specify a user to run when the service does not require administrator rights. And you can create the users you need earlier

Such as: RUN groupadd -r postgres && useradd -r -g postgres postgres . To obtain temporary administrator privileges can be used gosu "Is not recommended sudo .

WORKDIR

[

The format is WORKDIR /path/to/workdir.

]

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

Multiple WORKDIR directives can be used, with subsequent commands based on the path specified by the previous command if the argument is a relative path. For example,


WORKDIR /a 
WORKDIR b 
WORKDIR c 
RUN pwd 
 The final path is  /a/b/c . 

ONBUILD

[

The format is ONBUILD [INSTRUCTION].

]

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

For example, Dockerfile creates the mirror image-A with the following.


[...]
ONBUILD ADD . /app/src
ONBUILD RUN /usr/local/bin/python-build --dir /app/src
[...]

If based on image - A to create a new image, new Dockerfile use FROM image - A specified base image, will automatically perform ONBUILD instruction content, equivalent to add two instructions in the rear.

FROM image-A


 # Automatically run the following
ADD . /app/src
RUN /usr/local/bin/python-build --dir /app/src

Use a mirror of the ONBUILD directive. It is recommended to indicate this on the label, for example ruby:1.9-onbuild .

Create a mirror image

After Dockerfile is written, the image can be created using the docker build command.


docker build -t  Image name  .
//  Pay attention to  .  Don't forget. 

Here are two examples of Dockerfile on Dockerhub.


# Nginx
#
# VERSION 0.0.1
FROM ubuntu
MAINTAINER Victor Vieux <victor@docker.com>
RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server
#  in ubuntu Installed based on the parent image inotify-tools,nginx,apache2,openssh-server , thereby creating 1 A new one Nginx The mirror. 

# Firefox over VNC
#
#VERSION 0.3
FROM ubuntu
# Install vnc,xvfb in order to create a 'fake' display and firefox
RUN apt-get update && apt-get install -y x11vnc firefox
RUN mkdir /.vnc
# Setup a pssword
RUN x11vnc -storepasswd 1234 ~/.vnc/passwd
#Autostart firefox
RUN bash -c 'echo "firefox" >> /.bashrc'
EXPOSE 5900
CMD ["x11vnc", "-forever", "-usepw", "-create"]
#  Based on the ubuntu The parent image, installed firefox and vnc Software, after startup, the user can pass 5900 Port by vnc Way to use firefox . 

conclusion


Related articles: