Dockerfile self-made image

Source: Internet
Author: User
Tags file url
1. dockerfile command description:

Dockerfile commands are case-insensitive. We recommend that you use uppercase letters and # As annotations. Each line supports only one command, and each command can carry multiple parameters.
Dockerfile commands can be divided into two types based on their functions: Build commands and set commands.
The build command is used to build an image. The specified operation is not executed on the container that runs the image;
The setting command is used to set the attributes of an image. The specified operation is executed in the container that runs the image.

(1) From (specify the basic image)
Build command, which must be specified and before other commands of dockerfile. Subsequent commands depend on the image specified by the command. The basic image specified by the from command can be in the official remote repository or local repository.
This command has two formats:
From <image>
Specify the base image as the last modified version of the image. Or:
From <image >:< tag>
Specify the basic image as a tag version of the image.

(2) maintainer (used to specify the image creator Information)
The build command is used to write information related to the image producer to the image. When we execute the docker inspect command for the image, the corresponding field in the output records the information.
Format:
Maintainer <Name>

(3) Run (for software installation)
Build command, run can run any commands supported by the basic image. If Ubuntu is selected as the basic image, the software management part can only use Ubuntu commands.
This command has two formats:
Run <command> (the command is run in a shell-/bin/sh -c)
Run ["executable", "param1", "param2"...] (Exec form)

(4) cmd (set the operations performed when the container is started)
SET command, used for the operation specified when the iner is started. This operation can be a custom script or a system command. This command can only exist once in the file. If there are multiple commands, only the last one is executed.
This command has two formats:
CMD ["executable", "param1", "param2"] (like an exec, This is the preferred form)
CMD command param1 param2 (as a shell)

(5) entrypoint (set the operations performed when the container is started)
Set the command to specify the Command executed when the container is started. It can be set multiple times, but only the last one is valid.
Two formats:
Entrypoint ["executable", "param1", "param2"] (like an exec, the preferred form)
Entrypoint command param1 param2 (as a shell)
This command can be used in two cases: one is used independently, and the other is used together with the CMD command.
If you still use the CMD command and the CMD command is a complete executable command, the CMD command and entrypoint will overwrite each other with only the last cmd or entrypoint.
CMD command will not be executed, only entrypoint command will be executed
CMD echo "Hello, world !"
Entrypoint LS-l
Another method is to use with the CMD command to specify the default parameter of entrypoint. In this case, the CMD command is not a complete executable command, but only a parameter; the entrypoint command can only be used to specify the execution command in JSON mode, but cannot specify parameters.
From Ubuntu
CMD ["-l"]
Entrypoint ["/usr/bin/ls"]

(6) user (the user who sets the container)
Set commands and set the user who starts the container. The default value is root.
Run user of memcached
Entrypoint ["memcached"]
User daemon
Or
Entrypoint ["memcached", "-u", "daemon"]

(7) Expose (specify the port that the container needs to map to the host machine)
SET command, which maps the port in the container to a port in the host machine. To access a container, you can use the IP address of the host machine and the mapped port instead of the container IP address. Two steps are required to complete the entire operation. First, set the container port to be mapped using expose in dockerfile, and then specify the-P option when running the container plus the port set by expose, in this way, the port number set for expose is randomly mapped to a port number on the host machine. You can also specify the port to be mapped to the host machine. In this case, make sure that the port number on the host machine is not used. The expose command can set multiple port numbers at a time. The-P option can be used multiple times when a container is running.
Format:
Expose <port> [<port>...]
Map a port
Expose port1
Commands used to run containers
Docker run-P port1 Image

(8) ENV (used to set environment variables)
Build command, set an environment variable in the image.
Format:
Env <key> <value>
After setting, subsequent run commands can be used. After the container is started, you can use docker inspect to view the environment variable, you can also set or modify environment variables when docker run -- env key = value.
If you have installed the Java program and need to set java_home, you can write in dockerfile as follows:
Env java_home/path/to/Java/dirent

(9) add (copy the file from SRC to the Dest path of the container)
Token );
Format:
Add <SRC> <DEST>
<SRC> it is the relative path of the constructed source directory, which can be a file or directory path or a Remote File URL;
<DEST> is the absolute path in the iner.

(10) volume (specify a mount point)
Set commands to enable a directory in the container to store data persistently. This directory can be used by the container itself or shared with other containers. We know that the container uses aufs (overlay). This file system cannot persist data. when the container is closed, all changes will be lost. This command can be used in dockerfile when the application in the container requires persistent data.
Format:
Volume ["<mountpoint>"]

From base
Volume ["/tmp/Data"]
Run the docker file to generate the image container. After the data in the/tmp/data directory is closed, the data in the container still exists. For example, if another container also needs persistent data and wants to use the/tmp/data directory shared by the above container, you can run the following command to start a container:
Docker run-t-I-rm-volumes-from container1 image2 bash
Container1 is the ID of the first container, and image2 is the name of the second container running image.

(11) workdir (switch directory)
Set commands, which can be switched multiple times (equivalent to the CD command) and take effect for run, CMD, and entrypoint.
Format:
Workdir/path/to/workdir

Run Vim a.txt in/P1/P2
Workdir/P1
Workdir p2
Run Vim a.txt

(12) onbuild (executed in the sub-image)
Onbuild <dockerfile keyword>
The commands specified by onbuild are not executed when the image is built, but are executed in its sub-image.

2. dockerfile case practice

(1) preparation: need to prepare the jdk-7u79-linux-x64.rpm tomcat7.tar.gz installation package and the operating system daocloud. IO/centos

(2) Compile the dockerfile

FROM daocloud.io/centos:6.6RUN mkdir -p /home/demo/ADD ./jdk-7u79-linux-x64.rpm /home/demo/ADD ./tomcat7.tar.gz /home/demo/#install jdkRUN rpm -ivh /home/demo/jdk-7u79-linux-x64.rpmENV JAVA_HOME /usr/java/latestENV PATH $JAVA_HOME/bin:$PATHENV LANG en_us.UTF-8#expose poet EXPOSE 8080ENTRYPOINT /home/demo/tomcat7/bin/startup.sh && tail -F /home/demo/tomcat7/logs/catalina.out

(3) create an image
Docker build-T Tomcat: V1.0.

(4) check that the image is successfully created.

(5) test whether the image is available.

Start normally.

Dockerfile self-made image

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.