Transferred from: http://blog.163.com/digoal@126/blog/static/163877040201410411715832/
When creating an image using Dockerfile, there are several instructions that are easier to confuse, RUN, CMD, EntryPoint. Run is an instruction that runs at building image and can write multiple run commands in Dockerfile. CMD and entrypoint are the instructions that run when the container is run, only one is written, and if more than one is written, the last entry takes effect. The difference between CMD and entrypoint is that CMD is overwritten by the command at run time, and entrypoint is not overwritten by the command when it is run, but can also be specified. Example: Usage:docker run [OPTIONS] IMAGE [COMMAND] [ARG ...] --entrypoint= "" Overwrite the default entrypoint of the image
Docker run postgres:9.3.5 psql here psql is command, will overwrite dockerfile cmd, but will not overwrite entrypoint. If you want to overwrite entrypoint, you can enter--entrypoint= "...." During Docker run.
CMD and entrypoint are generally used to create image with background service, such as Apache, database, etc. When you start container with this image, the service starts automatically.
Detailed usage of these instructions: RUN
Run has 2 forms:run <command> (the command was run in a shell-/bin/sh-c-Shell form) Run ["Executable", "param1" , "param2"] (exec form)
The RUN instruction would execute any commands with a new layer on top of the "the current" image and commit the results. The resulting committed image is used for the next step in the Dockerfile.
Layering RUN instructions and generating commits conforms to the core concepts of Docker where commits is cheap and conta Iners can is created from the any-in-an image's history, much like source control.
The exec form makes it possible to avoid shell string munging, and to RUN commands using a base image that does not contai N/bin/sh.
Note: To use a different shell, and other than '/bin/sh ', with the exec form passing in the desired shell. For example, RUN ["/bin/bash", "-C", "echo Hello"]
Note: The Exec form is parsed as a JSON array, which means it must use Double-quotes (") around words not s Ingle-quotes (').
Note: Unlike the shell form, the exec form does not invoke a command shell. This means the normal shell processing does not happen. For example, CMD ["echo", "$HOME"] won't do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example:cmd ["sh", "-C", "E Cho "," $HOME "].
The cache for RUN instructions isn ' t invalidated automatically during the next build. The cache for a instruction like RUN Apt-get dist-upgrade-y would be reused during the next build. The cache for Runinstructions can is invalidated by using the--no-cache flag, for example Docker build--no-cache.
See the Dockerfile Best Practices Guide for more information.
The cache for RUN instructions can is invalidated by ADD instructions. See below for details. Known Issues (RUN) Issue 783 is about file permissions problems, can occur when using the AUFS file system. You might notice it during an attempt to rm a file, for example. The issue describes a workaround.
CMD
The cmd instruction have three forms:cmd ["Executable", "param1", "param2"] (exec form, this is the preferred form) CMD ["Pa Ram1 "," param2 "] (as default parameters to entrypoint) CMD command param1 param2 (Shell form)
There can only is one CMD instruction in a Dockerfile. If you list more than one CMD then only the lastcmd would take effect.
The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an Entrypointins Truction as well.
Note: If CMD is used to provide default arguments for the entrypoint instruction, both the CMD Andentrypoint Inst Ructions should is specified with the JSON array format.
Note: The Exec form is parsed as a JSON array, which means it must use Double-quotes (") around words not s Ingle-quotes (').
Note: Unlike the shell form, the exec form does not invoke a command shell. This means the normal shell processing does not happen. For example, CMD ["echo", "$HOME"] won't do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example:cmd ["sh", "-C", "E Cho "," $HOME "].
When a used in the shell or exec formats, the CMD instruction sets the command, is executed when running the image.
If you have the shell form of the CMD, then the <command> would execute IN/BIN/SH-C:
From Ubuntu
CMD echo "This is a test." | WC-
If you want to run your <command> without a shell then you must express the command as a JSON arra Y and give the full path to the executable. This array form is the preferred format of CMD. Any additional parameters must is individually expressed as strings in the array:
From Ubuntu
CMD ["/USR/BIN/WC", "--help"]
If you would like your container to run the same executable every time and then you should consider usingentrypoint in Combin ation with CMD. See entrypoint.
If The user specifies arguments to Docker run then they would override the default specified in CMD.
Note: Don ' t confuse run with CMD. Run actually runs a command and commits the result; Cmddoes not execute anything at build time, but specifies the intended command for the image.
entrypoint
EntryPoint has forms:entrypoint ["Executable", "param1", "param2"] (exec form, the preferred form) entrypoint command param1 param2 (Shell form)
There can only is one entrypoint in a Dockerfile. If you had more than one entrypoint and then only the last one in the Dockerfile would have a effect.
An entrypoint helps-configure a container that's can run as an executable. That's, when you specify a entrypoint, then the whole container runs as if it's just that executable.
Unlike the behavior of the CMD instruction, the entrypoint instruction adds an entry command that won't be is over Written when arguments is passed to Docker run. This allows arguments to is passed to the entry point, i.e. Docker run <image>-D would pass the-d argument to the E Ntry Point.
You can specify parameters either in the EntryPoint JSON array (as in ' like ' an exec ' above), or by using a CMD instruction . Parameters in the entrypoint instruction is not being overridden by the