What are the different instructions in the dockerfile?
-
Man page for dockerfile: http://bit.ly/2jdIkV2
Docker documentation on Dockerfile: https://docs.docker.com/engine/reference/builder/Explanation of the main instructions.
The FROM instructions tells docker which image to use, the "AS name" can be used to give a new name for an image for a new build stage in the Dockerfile. Only the ARG instruction can go above the FROM.
FROM <image> [AS <name>]
FROM <image>[:<tag>] [AS <name>]
FROM <image>[@<digest>] [AS <name>]The RUN instruction has two forms, notice the square brackets when using the exec form. You can have a number of RUN instruction that run one after another, performing commits after each. The exec form does not read environment variables, as it does not invoke a command shell, use shell form if you want to use variables.
Shell form: RUN <command>
Exec form: RUN ["executable", "param1", "param2"]The CMD instruction has three forms and you can only have one CMD instruction, if you have more than one the last one is used. The main purpose for the CMD is to provide defaults to the executing container. Many people get confused when beginning to use Dockerfiles, asking what the difference between RUN & CMD is, RUN actually executes commands and CMD does not, CMD specifies the intended command for the image.
Exec form: CMD ["executable","param1","param2"]
Defaults to ENTRYPOINT: CMD ["param1","param2"]
Shell form: CMD command param1 param2The ENTRYPOINT instruction allows you to configure a container that will run as an executable. There are two forms of ENTRYPOINT
Exec form: ENTRYPOINT ["executable", "param1", "param2"]
Shell form: ENTRYPOINT command param1 param2It executes as follows:
- If in shell form then CMD and Docker run arguments are ignored
- if in exec form and docker run arguments specified, it overrides CMD
- if in exec form and CMD is specified, it will use CMD
The ENV instruction has two forms, and specified an environment variable.
ENV key value
Allows multiple variables ENV key1=value1 key2=value2 ...The LABEL adds metadata to an image.
LABEL <key>=<value> <key>=<value> <key>=<value> ...
The EXPOSE tell the docker daemon what port(s) the container will have exposed. The -p & -P in docker run actually do the publishing of the ports.
EXPOSE <port> [<port>/<protocol>...]
The USER instruction switch to the user, which much exist for it to switch to it.
USER <user>[:<group>]
USER <UID>[:<GID>]The WORKDIR instruction sets the working directory, it's like using $ cd on the command line. It can be combined with ENV variables.
The COPY instruction copeis files or directories from src to dest on the container. It has two forms, the second form is used for white space paths
COPY <src>... <dest>
COPY ["<src>",... "<dest>"]The ADD instruction is similar to COPY, but moves file or directories to the container.
The VOLUME instruction creates a mount point on the container from the host or other computer.
© Lightnetics 2024