How to build a mirror using Dockerfile

Source: Internet
Author: User
Original address: http://blog.csdn.net/we_shell/article/details/38445979

Dockfile is a script that is interpreted by the Docker program, Dockerfile consists of a single instruction, each of which corresponds to a command under Linux. The Docker program translates these dockerfile instructions into real Linux commands. Dockerfile has its own writing format and support commands, and the Docker program solves the dependencies between these commands, similar to makefile. The Docker program will read the Dockerfile and generate a custom image according to the instructions. Compared to the black box of image, Dockerfile's obvious script is easier for the user to accept, and it clearly shows how the image was created. With Dockerfile, when we need to customize our own additional requirements, simply add or modify instructions on the dockerfile to regenerate the image, eliminating the hassle of knocking commands.


1. Dockerfile Writing rules and instructions for use
The dockerfile instruction is case-insensitive, it is recommended to use uppercase, # as a comment, each line supports only one instruction, and each instruction can carry multiple parameters.
Dockerfile's instructions can be divided into two types, building instructions and setting instructions according to the function. The build directive is used to build an image whose specified action is not executed on the container running image; The settings directive is used to set the property of the image, and the specified action is executed in the container running image.

(1) from (specify base image)The build instructions must be specified and need to be preceded by dockerfile other directives. Subsequent directives are dependent on the image specified by the directive. The base image specified by the from instruction can be in the official remote repository or in the local repository.
the directive is available in two formats:
[Plain]View plain Copy from <image> specifies the base image for the last modified version of the image. Or:
[Plain]View plain Copy from <image>:<tag> specifies the base image as a tag version of the image.

(2) maintainer (used to specify the image creator information)A build instruction for writing information about the creator of an image to an image. When we execute the Docker inspect command on the image, the output has a corresponding field that records that information.
format:
[Plain]View Plain Copy Maintainer <name>
(3) RUN (for software installation)The build command, run, runs any command that is supported by the underlying image. If the underlying image chooses Ubuntu, then the Software Management section can only use Ubuntu commands.
the directive is available in two formats:
[Plain]View Plain copy run <command> (the command is run in a shell-'/bin/sh-c ') RUN ["Executable", "param1", "param2"  ... ] (EXEC form)
(4) CMD (sets the action to be performed when container starts)Set instructions for container the operation specified at startup. This can be either executing a custom script or executing a system command. The directive can only exist once in a file, and if there are multiple, only the last one is executed.
the directive is available in three formats:
[Plain]View plain copy cmd ["Executable", "param1", "param2"] (like a exec, this is the preferred form) cmd command param1 param 2 (as a shell) when dockerfile specifies EntryPoint, use the following format:
[Plain]View plain copy CMD ["param1", "param2"] (as default parameters to EntryPoint) entrypoint specifies an executable script or path to the program that the specified script or program will Executed with param1 and param2 as parameters. So if the cmd command uses the above form, then the dockerfile must have a matching entrypoint.

(5) entrypoint (sets the action to be taken at container startup)The set instruction, which specifies the command to execute when the container starts, can be set more than once, but only the last one is valid.
two different formats:
[Plain]View plain copy entrypoint ["Executable", "param1", "param2"] (like an exec, the preferred form) entrypoint command para M1 param2 (as a shell) the use of this instruction is divided into two cases, one for use alone and the other with the cmd command.
When used alone, if you also use the cmd command and CMD is a full executable command, then the cmd and entrypoint will overwrite each other with only the last cmd or entrypoint valid.
[Plain]The View plain copy # CMD command will not be executed, only the entrypoint instruction is executed CMD echo "Hello, world!" EntryPoint ls-l Another usage and the CMD directive used to specify the default parameters of the EntryPoint, when the cmd instruction is not a complete executable command, only the parameter part; The entrypoint instruction can only use JSON to specify the execution command. You cannot specify parameters.
[Plain]View plain Copy from Ubuntu CMD ["-L"] entrypoint ["/usr/bin/ls"]
(6) User (users who set up the container container)Set the command, set the user to start the container, the default is the root user.
[Plain]View Plain Copy # Specifies memcached's running user entrypoint ["memcached"] User daemon or entrypoint ["memcached", "-U", "daemon"]
(7) EXPOSE (specifies that the container needs to be mapped to the port of the host machine)Sets the instruction that maps the port in the container to a port on the main machine Cheng. When you need to access the container, you can use the host machine's IP address and the mapped port instead of the container's IP address. To complete the operation requires two steps, first in dockerfile use expose to set the container port to be mapped, and then specify the-p option when running the container and the port set by the expose, so that the expose set the port number is randomly mapped Cheng a port number in the main machine. You can also specify the port that needs to be mapped to the host machine, making sure that the port number on the host machine is not being used. Expose instruction can be set multiple port number at a time, corresponding to run the container, can be matched multiple times using the-P option.
format:

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.