Dockerfike quickly create a custom Docker mirror
A, Table of contents
1.docker Typical structure
2. Instruction Introduction
3. Create Docker Mirrors
Second, the structure
The Dockerfile is divided into four parts: the underlying mirror letter, the maintainer information, the mirroring operation instructions, and the execution instructions when the container starts. For example:
#第一行必须指令基于的基础镜像 from
Ubutu
#维护者信息
maintainer docker_user docker_user@mail.com
#镜像的操作指令
Apt/sourcelist.list
Run apt-get update && apt-get install-y Ngnix
run echo "\ndaemon off;" >>/etc/ngnix/nignix.conf
#容器启动时执行指令
Cmd/usr/sbin/ngnix
1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15
Third, the directive
1. From instruction
From or from:
Dockerfile The first rule must be the from command. If you create multiple mirrors with the same dockerfile, you can use multiple from directives (once per mirror)
2, maintainer
Format is maintainer, specifying the maintainer's information
3, RUN
Format is run or run ["Executable", "Param1", "param2"]
The former runs on the shell terminal, that is,/bin/sh-c, which runs with exec. Example: RUN ["/bin/bash", "-C", "echo Hello"]
Each run instruction executes on the current underlying mirror and commits a new mirror. When the command is longer, you can use "/" to wrap the line.
4. cmd instruction
Supports three formats:
CMD ["Executable", "Param1", "param2"] using exec executive, recommended
CMD command param1 param2, executed on/bin/sh
CMD ["Param1", "param2"] is provided to entrypoint to do default parameters.
Each container can only execute a cmd command, multiple cmd commands, only the last one is executed.
5, expose
The format is expose [...].
Tells Docker the port number exposed by the server container for use by the interconnection system. When you start Docker, you can pass-p, and the host automatically assigns a port number forward to the specified port. With-p, you can specify which local port map to come from
For example:
Expose 22 80 8443
6, ENV
The format is ENV. Specifies an environment variable that will be used by subsequent run instructions and persisted while the container is running.
For example
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
1 2 3 4
7, ADD
Format is ADD.
This command copies the specified to the container. This can be a relative path to the directory where Dockerfile is located, a URL, or a tar file (automatically unzipped to a directory). The
8, COPY
The format is COPY.
Copies the local host's (the relative path of the directory where dockerfile resides) into the container.
COPY is recommended when using the local directory as the source directory.
9, EntryPoint
Two types of formats:
entrypoint ["Executable", "param1", "param2"]
entrypoint command param1 param2 (executed in shell).
Configures the commands that are executed after the container is started and cannot be overwritten by the parameters provided by Docker run.
There can only be one entrypoint per dockerfile, and when multiple are specified, only the last one will be effective.
10, VOLUME
The format is VOLUME ["/data"].
Create a mount point that can be mounted from a local host or other container, typically to hold the database and the data that needs to be maintained.
11, USER
The format is USER daemon.
Specifies the user name or UID when the container is run, and subsequent run uses the specified user.
When the service does not require administrator permissions, you can specify that the user is run by this command. And you can create the required users before, such as RUN groupadd-r postgres && useradd-r-G postgres postgres. To get administrator privileges temporarily, you can use Gosu instead of sudo.
12, Workdir
The format is Workdir/path/to/workdir.
Configure the working directory for subsequent RUN, CMD, and entrypoint directives.
You can use multiple Workdir directives, and subsequent commands, if the arguments are relative paths, are based on the path specified by the previous command. For example
workdir/a
Workdir b
Workdir C
RUN pwd
The final path is/a/b/c.
13, Onbuild
The format is onbuild [instruction].
Configures the action instructions that are executed when a mirrored mirror is created as the underlying image of another newly created mirror.
For example, Dockerfile uses the following content to create a mirrored image-a.
[...]
Onbuild ADD. /app/src
Onbuild RUN/USR/LOCAL/BIN/PYTHON-BUILD–DIR/APP/SRC
[...]
If a new mirror is created based on A, the onbuild instruction content is automatically executed when the new Dockerfile uses the from image-a to specify the underlying mirror, which is equivalent to adding two instructions later.
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, recommended in the label, such as Ruby:1.9-onbuild.
1 2 3 4 5 6 7
Third, create a mirror
Create a mirror from the Docker build.
The command reads all the dockefile under the specified path, including subdirectories, and sends all the contents of the directory to the server, creating a mirror from the server. Alternatively, you can allow Docker to ignore the specified directory or file by creating a. dockerignore file (adding one matching pattern per row)
Format is Docker build [options] Path
Need to develop label information, you can use the-t option
For example: The Dockerfile path is/tmp/docker_build/, and the generated mirror label is Build_repo/my_images
$dudo Docker build-t build_repo/my_images/tmp/docker_build/