Details of Docker base Dockerfile command

  • 2020-06-12 11:30:05
  • OfStack

Dockerfile is a 1-text configuration file that allows users to quickly create custom images using Dockerfile. We'll start with the basic structure of Dockerfile and the many instructions it supports, as well as explain how to write custom images of Dockerfile by executing instructions.

The basic structure

Dockerfile consists of 1 line command statements and supports comment lines beginning with #. In general, Dockerfile is divided into four parts: base image information, maintainer information, mirror operation instructions, and container startup execution instructions. Such as:


# This dockerfile uses the Ubuntu image
# VERSION 2
# Author: docker_user
# Command format: Instruction [arguments / command]  ... 

#  The first 1 The row must specify the container image on which it is based 
FROM ubuntu

#  Maintainer information 
MAINTAINER docker_user docker_user@email.com

#  Mirror operation instruction 
RUN echo  " deb http://archive.ubuntu.com/ubuntu/ raring main universe "  >> /etc/apt/sources.list
RUN apt-get update && apt-get install -y nginx
RUN echo  " \ndaemon off; "  >> /etc/nginx/nginx.conf

#  The instructions are executed when the container starts 
CMD /usr/sbin/nginx

Where 1 must first specify the name of the mirror on which it is based, and then 1 generally specifies the information about the maintainer. This is followed by the mirror operation instruction, such as the RUN instruction, which executes the following command on the mirror. For each RUN instruction run, the image adds a new layer and commits. Finally, the CMD directive specifies the operation command to run the container.

Here are two examples from dockerhub that give students a basic sense of the STRUCTURE of Dockerfile.

The first is to install es25EN-ES26en, nginx, apache2, ES29en-ES30en and other software on the basis of Ubuntu image to create a new nginx image:


# nginx
# VERSION 0.0.1
FROM ubuntu
MAINTAINER Victor Vieus <victor@docker.com>
RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server

The second is also based on the ubuntu image, with firefox and vnc software installed. After startup, users can use firefox via vnc via port 5900:


# 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 xvfb firefox
RUN mkdir /.vnc
# setup a password
RUN x11vnc -storepasswd 1234 ~/.vnc/passwd
# Autostart firefox
RUN bash -c  ' echo  " firefox "  >> /.bashrc'
EXPOSE 5900
CMD [ " x11vnc " ,  " -forever " ,  " -usepw " ,  " -create " ]

instruction

The general format of the instruction is INSTRUCTION arguments, including FROM, MAINTAINER, RUN, etc., described below respectively.

FROM

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

Instruction 1 of Dockerfile must be the FROM instruction. Also, if multiple images are created in the same Dockerfile, multiple FROM directives can be used.

MAINTAINER

Format for MAINTAINER < name > , specifies the maintainer information.

Note: the MAINTAINER directive has been discarded and it is recommended to use the LABEL directive.

LABEL

Format for:


LABEL <key>=<value> <key>=<value> <key>=<value> ...

The LABEL directive adds a label to the image. One LABEL is one key-value pair.

Here are some examples:


LABEL "com.example.vendor"="ACME Incorporated"
LABEL com.example.label-with-value="foo"
LABEL version="1.0"
LABEL description="This text illustrates \that label-values can span multiple lines."

We can add more than one LABEL to the image, and it is important to note that each LABEL directive generates a new layer. So it's best to combine the multiple LABEL additions into one command:


LABEL multi.label1="value1" multi.label2="value2" other="value3"

Or you could write it like this:


LABEL multi.label1="value1" \
   multi.label2="value2" \
   other="value3"

If the newly added LABEL has the same name as the existing LABEL, the new value overrides the old value.

We can use the docker inspect command to see the LABEL information for the mirror.

RUN

There are two formats, respectively:


RUN <command>

RUN [ " executable " ,  " param1 " ,  " param2 " ]

The former runs the command from the shell terminal, /bin/ ES129en-ES130en, while the latter executes using exec. Specify other terminals that can be used in a second way, such as RUN [" /bin/bash ", "-ES135en", "echo hello"].

Each RUN instruction executes the specified command from the current mirror and commits it to the new mirror. You can use \ to wrap lines when the command is long.

CMD

Support 3 formats:

CMD [" executable ", "param1", "param2"] using exec, recommended.

CMD command param1 param2 is implemented in /bin/sh for applications that need to interact.

CMD [" param1 ", "param2"] is the default parameter provided to ENTRYPOINT.

Specify the commands to execute when the container is started, and each Dockerfile can have only one CMD command. If more than one CMD command is 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 is overwritten.

EXPOSE

Format for:


EXPOSE <port> [<port> ... ]

For example, EXPOSE 22 80 8443

Tell the Docker service that the container needs to expose the port number for use by the interconnected system. When the container is started, the -ES187en parameter requires the Docker host to assign one port to forward to the specified port. Using the -p parameter, you can specify which port on the host to map to.

ENV

Format for ENV < key > < value > . Specify an environment variable that will be used by subsequent RUN directives and will remain in place while the container runs. Such as:


ENV PG_MAJOR 9.3
ENV PG_VERSION 9.3.4
RUN curl -SL http://example.com/postgres-$PG_VERSION.tar.xz | tar -xJC /usr/src/postgress &&  ... 
ENV PATH /usr/local/postgres-$PG_MAJOR/bin:$PATH

ADD

Format for:


# nginx
# VERSION 0.0.1
FROM ubuntu
MAINTAINER Victor Vieus <victor@docker.com>
RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server
0

This command copies the specified < src > Into the container < dest > . Among them < src > It can be 1 relative path (file or directory) to the directory where Dockerfile is located; It could be 1 URL; It can also be an tar file (automatically unzipped to a directory).

COPY

Format for:


# nginx
# VERSION 0.0.1
FROM ubuntu
MAINTAINER Victor Vieus <victor@docker.com>
RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server
1

Copy localhost's < src > (is the relative path to the directory where Dockerfile resides, file or directory) is in the container < dest > . When the target path does not exist, it is automatically created. COPY is recommended when using the local directory as the source directory.

ENTRYPOINT

There are two formats:


# nginx
# VERSION 0.0.1
FROM ubuntu
MAINTAINER Victor Vieus <victor@docker.com>
RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server
2

Configure the commands executed after the container is started and cannot be overridden by the parameters provided by docker run.

There can only be one ENTRYPOINT per Dockerfile, and when multiple ENTRYPOINT are specified, only the last one takes effect.

VOLUME

Format for:


 VOLUME ["/data"]

Create a mount point that can be mounted locally or from another container, typically to hold databases and data that needs to be held, etc.

USER

Format for:


# nginx
# VERSION 0.0.1
FROM ubuntu
MAINTAINER Victor Vieus <victor@docker.com>
RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server
4

Specify the user name or UID when the container is run, and subsequent RUN will also use the specified user. This command can be used to specify the user to run when the service does not require administrator privileges. And you can create the required users before, for example: RUN ES275en-ES276en postgres & & useradd-r postgres postgres

WORKDIR

Format for:


# nginx
# VERSION 0.0.1
FROM ubuntu
MAINTAINER Victor Vieus <victor@docker.com>
RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server
5

Configure the working directory for subsequent RUN, CMD, and ENTRYPOINT directives. Multiple WORKDIR instructions can be used, and subsequent commands, if the argument is a relative path, will be based on the path specified by the previous command. Such as:


# nginx
# VERSION 0.0.1
FROM ubuntu
MAINTAINER Victor Vieus <victor@docker.com>
RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server
6

The final path is /a/b/c.

ONBUILD

Format for:


# nginx
# VERSION 0.0.1
FROM ubuntu
MAINTAINER Victor Vieus <victor@docker.com>
RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server
7

Configure the action instructions that are executed when the created image is the base image of another newly created image. For example, Dockerfile created the mirror ES312en-ES313en using the following.


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

If a new image is created based on ES317en-ES318en and the base image is specified using FROM ES321en-ES322en in the new Dockerfile, the ONBUILD instruction content is automatically executed, equivalent to adding two directives later.


# nginx
# VERSION 0.0.1
FROM ubuntu
MAINTAINER Victor Vieus <victor@docker.com>
RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server
9

Create a mirror image

After writing Dockerfile, you can create a mirror using the docker build command.

The basic format is docker build [option] path, which reads Dockerfile under the specified path (including subdirectories) and sends everything under that path to the docker server, which creates the image. Therefore, it is generally recommended that the directory where Dockerfile is placed is empty.

In addition, the.dockerignore file allows docker to ignore directories and files in the path. To specify the tag information for the image, use the -ES344en option.
For example, if you specify the path of Dockerfile as /tmp/docker_builder/ and want to generate the mirror tag as build_repo/first_image, you can use the following command:


$ sudo docker build -t build_repo/first_image /tmp/docker_builder/

Related articles: