Introduction to Dockerfile

Below is a basic Dockerfile 

FROM ubuntu:latest
MAINTAINER Scott P. Gallagher <[email protected]>
RUN apt-get update && apt-get install -y apache2
ADD 000-default.conf /etc/apache2/sites-available/
RUN chown root:root /etc/apache2/sites-available/000-default.conf
EXPOSE 80
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

 The following is a brief introduction to a dockfile instruction:

 

instruction

The general format of the instruction is INSTRUCTION arguments, and the instruction includes FROM, MAINTAINER, RUN, etc.

FROM

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

The first instruction must be a FROM instruction. Also, if you create multiple images in the same Dockerfile, you can use multiple FROM instructions (once for each image).

MAINTAINER

The format is MAINTAINER <name>, specifying the maintainer information.

LABEL
The LABEL command is used to add additional information to the image. It can include a version number or a description. You should try to put the labels on one line. Each time you use a Label, it will add a layer to the image. It is used too much. tags can also cause mirroring to become less efficient. Label information can be viewed with the inspect command:
$docker inspect <image_id>
RUN
The format is RUN <command> or RUN ["executable", "param1", "param2"].

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

Each RUN instruction will execute the specified command based on the current image and submit it as a new image. Use \ to wrap a line when the command is long.

CMD
Three formats supported

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

    CMD command param1 param2 is executed in /bin/sh and provided to applications that need 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.

EXPOSE
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
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.
Environment variables can be viewed through docker inspect <IMAGE_ID>
You can use -e or --env to change the value during the docker run initialization command
$ docker run -e username=superuser
$ docker run --env username=superuser

example

ENV PG_MAJOR 9.3 (or ENV PG_MAGOR=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
The format is ADD <src> <dest>.

This command will copy the specified <src> to <dest> in the container. Where <src> 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 (automatically decompressed into a directory).

COPY
The format is COPY <src> <dest>.

Copy the <src> of the local host (which is the relative path to the directory where the Dockerfile is located) to the <dest> of the container.

COPY is recommended when using a local directory as the source directory.

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.

VOLUME
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 you can create the required users before, for example: RUN groupadd -r postgres && useradd -r -g postgres postgres. To temporarily gain administrator privileges you can use gosu instead of sudo.
The USER command can be used in the RUN, CMD or ENTRYPOINT command.
WORKDIR
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.

ONBUILD
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.

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

[...]

ONBUILD ADD . /app/src

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

[...]

If a new image is created based on image-A, when FROM image-A is used to specify the base image in the new Dockerfile, the contents of the ONBUILD instruction will be automatically executed, which is equivalent to adding two instructions later.

FROM image-A #Automatically run the followingADD . /app/srcRUN /usr/local/bin/python-build --dir /app/src

Mirrors that use the ONBUILD command are recommended to be marked in the label, such as ruby:1.9-onbuild.
  Differences between ENTRYPOINT and CMD:
1. ENTRYPOINT, which indicates the command to be executed when the image is initialized. It cannot be overwritten and must be remembered.
2. CMD, which means the default parameters of the mirror operation, which can be overridden
3. ENTRYPOINT/CMD can only exist once in the file, and the last one takes effect for multiple existences, only the last one takes effect, and the others are invalid!
4. You need to initialize and run multiple commands, which can be separated by &&, but the last one needs to be an infinite command, remember!
 

 Dockerfile - Best Practices

1. There should be a habit of using the .dockerignore file.

2. Minimize the number of packages per image, keeping them as small as possible. Do not install unnecessary packages.

3. Each container runs only one program process. Every time you need a new application, it's best to use a new container to run it.

4. Order sorting:

  Sort by the order of the commands actually used, as follows: 

          apt-get updata && apt-get install -y

  Or sort alphabetically:

        apt-get update && apt-get install -y \ apache2 \

                                      git\

                                      mysql

 

 

Guess you like

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