Dockerfile
Docker can build images automatically by reading the instructions from a Dockerfile.
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.
Traditionally, the Dockerfile is called Dockerfile and located in the root of the context. You use the -f flag with docker build to point to a Dockerfile anywhere in your file system.docker build -f /path/to/a/Dockerfile .
You can specify a repository and tag at which to save the new image if the build succeeds:docker build -t repositoryName/appName .
List of instructions in the Dockerfile
FROMMAINTAINERRUNCMDEXPOSEENVCOPYWORKDIRARG
FROM
FROM imageName or FROM imageName:tag
The FROM instruction sets the Base Image for subsequent instructions. As such, a valid Dockerfile must have FROM as its first instruction. The image can be any valid image, it is especially easy to start by pulling an image from the Public Repositories.
FROMmust be the first non-comment instruction in the Dockerfile.- The
tagvalue are optional. If you omit either of them, the builder assumes alatestby default. The builder returns an error if it cannot match thetagvalue.
MAINTAINER
MAINTAINER Author name
The MAINTAINER instruction allows you to set the Author field of the generated images.
RUN
RUN has 2 forms:
RUN <command>(shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)RUN ["executable", "param1", "param2"] (exec form)
The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.
Layering RUN instructions and generating commits conforms to the core concepts of Docker where commits are cheap and containers can be created from any point 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 contain the specified shell executable.
The default shell for the shell form can be changed using the SHELL command.
CMD
The CMD instruction has three forms:
CMD ["executable","param1","param2"](exec form, this is the preferred form)CMD ["param1","param2"](as default parameters to ENTRYPOINT)CMD command param1 param2(shell form)
There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will 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 ENTRYPOINT instruction as well.
EXPOSE
EXPOSE port
The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. EXPOSE does not make the ports of the container accessible to the host. To do that, you must use either the -p flag to publish a range of ports or the -P flag to publish all of the exposed ports. You can expose one port number and publish it externally under another number.
ENV
ENV <key> <value>
The ENV instruction sets the environment variable <key> to the value <value>. This value will be in the environment of all “descendant” Dockerfile commands and can be replaced inline in many as well.ENV <key> <value>, will set a single variable to a value. The entire string after the first space will be treated as the <value>, including characters such as spaces and quotes.
COPY
COPY <src> <dest>
The COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.
WORKDIR
WORKDIR /path/to/workdir
The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. If the WORKDIR doesn’t exist, it will be created even if it’s not used in any subsequent Dockerfile instruction.
ARG
You can use an ARG or an ENV instruction to specify variables that are available to the RUN instruction. Environment variables defined using the ENV instruction always override an ARG instruction of the same name. Consider this Dockerfile with an ENV and ARG instruction.
docker build --build-arg ARGUMENTNAME=valueARG
FROM node:alpine
ARG ARGUMENTNAME
ENV NEWENV $ARGUMENTNAME
...
Example of Dockerfile
FROM node:alpine
MAINTAINER Edgar Garcia
ARG VSTS_NPM_AUTH_TOKEN
ENV VSTS_NPM_AUTH_TOKEN $VSTS_NPM_AUTH_TOKEN
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install app dependencies
COPY . /usr/src/app/
RUN npm install
RUN npm run build
EXPOSE 8080
CMD [ "node", "server.js" ]