How to Use Dockerfile to create images and Dockerfile to create images
Original article, the original address: http://www.cnblogs.com/fengzheng/p/5181222.html
Purpose of creating an image
First, DockerHub or some other image repositories have provided enough images, including the minimum version and some versions of third-party software installed with mysql, nginx, and apache. Although it is enough, in some cases it cannot meet our needs. For example, if we need to install a third-party software that is rarely used, we can only use an image in the public warehouse to start the container, then install the software, modify the configuration, and so on in the container as required, and then submit the image. These operations are described in the previous article. After completing this operation, you can use the following two methods to customize the image:
1. save the image as a tar package using save and export, and import the tar image package as needed.
2. push the configured image to our private warehouse (create a private warehouse using docker) or a Registered Common warehouse. If necessary, use pull directly.
Both methods can be used, but the automation is low, the degree of freedom is not enough, and customization is troublesome. In this case, let's look at a more automated creation method.
Dockerfile Structure
Dockerfile consists of four parts: basic image information, maintainer information, image operation instructions, and commands executed when the container is started.
# This dockerfile uses the ubuntu image# VERSION 2 - EDITION 1# Author: docker_user# Command format: Instruction [arguments / command] ..# Base image to use, this must be set as the first lineFROM ubuntu# Maintainer: docker_user <docker_user at email.com> (@docker_user)MAINTAINER docker_user docker_user@email.com# Commands to update the imageRUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.listRUN apt-get update && apt-get install -y nginxRUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf# Commands when creating a new containerCMD /usr/sbin/nginx
Where # Table comments can be used to mark descriptive text.
The FROM keyword specifies the image source. The default value is DockerHub. You can also write a private repository image, for example, localhost: 5000/centos: 6.7. If the specified image name already exists locally, it is obtained directly from the local cache. MAINTAINER specifies the author of the image, then runs RUN, ADD, and so on for the image operation, and finally the command that is triggered when the container is started.
Commands in Dockerfile
FROM:Specify the image name in the format of FROM <image> or FROM <image >:< tag>, for example, FROM ubuntu or FROM ubuntu: 12.04.
MAINTAINER:Image author, in the formatMAINTAINER <name>
RUN:Format:RUN <command>OrRUN ["executable", "param1", "param2"].
The former will run commands in the shell terminal, that is/bin/sh -cThe latter usesexecRun. The second method can be used to specify other terminals, for exampleRUN ["/bin/bash", "-c", "echo hello"].
Each entryRUNThe command runs the specified command on the basis of the current image and submits it to the new image. You can use\Line feed.
CMD:Three formats are supported.
1. CMD ["executable", "param1", "param2"] execute with exec. recommended method;
2. Run CMD command param1 param2 in/bin/sh and provide it to the application to be interacted;
3. CMD ["param1", "param2"] provides default parameters for ENTRYPOINT;
Specify the command to be executed when the container is started. Each Dockerfile can have only one CMD command. If multiple commands are specified, only the last one will be executed. If you specify the running command when starting the container, the command specified by CMD is overwritten.
EXPOSE:Format:EXPOSE <port> [<port>...].
Inform the Docker server of the port number exposed by the container for the interconnected system. -P is required when the container is started. The Docker host will automatically allocate a port to forward it to the specified port.
ENV:Format:ENV <key> <value>. Specify an environment variable.RUNCommand used and maintained during container running. This corresponds to the variable definition in the program language and can be referenced as needed. For example:
ENV PG_MAJOR 9.3ENV PG_VERSION 9.3.4RUN 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:ADD <src> <dest>.
This command will copy the specified<src>To the container<dest>. Where<src>It can be a relative path of the directory where Dockerfile is located, a URL, or a tar file (automatically decompressed as a directory ).
COPY:Format:COPY <src> <dest>.
Copy<src>(The relative path of the directory where Dockerfile is located)<dest>. We recommend that you use the local directory as the source directory.COPY.
The difference between COPY and ADD is that ADD has the function of Automatically Extracting and supporting URL paths.
ENTRYPOINT:
Two formats:
ENTRYPOINT ["executable", "param1", "param2"]
ENTRYPOINT command param1 param2(Executed in shell ).
Configure the commands executed after the container is started and cannot bedocker runThe provided parameters are overwritten.
Each Dockerfile can contain only oneENTRYPOINTWhen multiple are specified, only the last one takes effect.
CMD and ENTRYPOINT comparison:Both commands can only be used once and run when the docker run Command is executed. If there are multiple commands, only the last one is executed.
The difference between the two lies in the parameter transmission method. If the following command is defined in Dockerfile
CMD echo hello
Or
ENTRYPOINT ["echo","hello"]
When running the command docker run containerId echo hello, specify the CMD input result as world. It can be seen that the specified command in Dockerfile is overwritten, while the ENTRYPOINT is specified, the output is hello echo world. It can be seen that the specified command is used as the parameter of the specified ENTRYPOINT command.
VOLUME:Format:VOLUME ["/data"]. Create a mount point that can be mounted from a local host or other containers. It is generally used to store databases and data to be maintained. However, it does not make sense to specify this attribute in Dockerfile, because there is no way to specify the directory of the local host. If you need to specify a mount point, you can specify it when executing the docker run command:
docker run -it -v /home/fengzheng/ftp/:/data 859666d51c6d /bin/bash
USER:Format:USER daemon. Specify the username or UID when running the container.RUNThe specified user is also used.
When the service does not require administrator permission, you can use this command to specify the user to run. In addition, you can create the required users before, for example:RUN groupadd -r postgres && useradd -r -g postgres postgres. You can usegosuBut not recommendedsudo.
WORKDIR:Format:WORKDIR /path/to/workdir. For subsequentRUN,CMD,ENTRYPOINTCommand to configure the working directory. MultipleWORKDIRCommand. If the parameters of subsequent commands are relative paths, they are based on the paths specified by the previous commands. For example
WORKDIR /aWORKDIR bWORKDIR cRUN pwd
The final path is/a/b/c.
ONBUILD:Format:ONBUILD [INSTRUCTION].
Configure the operation commands executed when the created image is used as the basic image of another newly created image.
For example, Dockerfile creates an image using the following content:image-A.
[...]ONBUILD ADD . /app/srcONBUILD RUN /usr/local/bin/python-build --dir /app/src[...]
If you create A new image based on image-FROM image-AWhen the basic image is specifiedONBUILDCommand content, equivalent to adding two commands later.
FROM image-A#Automatically run the followingADD . /app/srcRUN /usr/local/bin/python-build --dir /app/src
UseONBUILDThe image of the command, which should be specified in the label, for exampleruby:1.9-onbuild.
Install nginx Based on CentOS6.7 and source code
The nginx-1.9.tar.gz installation package and CentOS6-Base-163.repo (163 source) were first written, put the two files in the same directory, and create a file named Dockerfile under this directory. In this file, the source replacement, nginx compilation and installation, and some dependent packages are installed. The Dockerfile content is as follows:
# This is a test ubuntu 12.04 image dockerfile # Author: fengzheng # Base image, this must be set as the first line # localhost: 5000/centos: 6.7 is the image of my private repository and can be replaced with centos: 6.7 (image in DockerHub) FROM localhost: 5000/centos: 6.7 MAINTAINER fengzheng # Commands to update the imageRUN mkdir/usr/guest nginx-1.9.9.tar.gz/usr/nginx1.9.9/# RUN yum-y install tar # RUN tar-zxvf/usr/nginx1.9.9/nginx-1.9.9.tar.gzRUN cd/ etc/yum. repos. d/& mv CentOS-Base.repo CentOS-Base.repo.bakADD CentOS6-Base-163.repo/etc/yum. repos. d/RUN cd/etc/yum. repos. d/& mv CentOS6-Base-163.repo CentOS-Base.repo \ & yum clean all & yum makecache \ & yum-y install gcc \ & yum-y install yum install-y pcre-devel \ & yum-y install zlib-devel \ & yum-y install openssl -- devel \ & cd/usr/nginx1.9.9/nginx-1.9.9 /&&. /configure & make install # If daemon off is set, nginx cannot be started # RUN echo "\ ndaemon off;">/etc/nginx. conf # Commands when creating a new container # To start nginx, run/usr/local/nginx/sbin. /configureCMD/bin/bash
Finally, run the command "docker build-t nginx-centos: 6.7 ."
"." Indicates searching the Dockerfile in the current directory. The "-t" parameter specifies the image name and tag.