This context provides a beginner's guide to understanding Dockerfile with a sample project, covering various Dockerfile instructions such as FROM, CMD, ENTRYPOINT, WORKDIR, ENV, COPY, LABEL, RUN, ADD, .dockerignore, ARG, EXPOSE, USER, and VOLUME.
Abstract
The context offers a comprehensive guide to Dockerfile, a script used to automate the creation of Docker images. It covers various instructions used in a Dockerfile, including comments, base image selection, default commands, executable commands, working directory setup, environment variables, file copying, metadata addition, command execution, remote file addition, context filtering, argument passing, port documentation, user and group setting, and volume creation. The guide uses a sample project to demonstrate the usage of these instructions and provides examples for each instruction.
Opinions
Dockerfile is essential for automating Docker image creation.
The FROM instruction is mandatory for building an image.
CMD and ENTRYPOINT instructions are used to provide default commands and executables for the container.
WORKDIR instruction sets the working directory for subsequent instructions.
ENV instruction sets environment variables for subsequent instructions.
COPY and ADD instructions are used to copy files or directories from the host to the container.
LABEL instruction adds metadata to the image.
RUN instruction executes commands in a new layer on top of the existing image.
.dockerignore file is used to exclude files or directories from being sent to the Docker daemon.
ARG instruction is used to pass arguments to subsequent instructions.
EXPOSE instruction serves as documentation for the port.
USER instruction sets the user name and group to use when running the image.
VOLUME instruction creates a mount point with the specified name.
Docker — A Beginner’s guide to Dockerfile with a sample project
A step by step guide to understanding the Dockerfile
Photo by Roger Hoyles on UnsplashAutomatic creation of Docker images through Dockerfile
Dockerfile is used to automate the Docker image creation. Docker builds images by reading instructions from the Dockerfile.
We will understand Dockerfile instructions by building a sample project. clone the below repo for all the examples.
Here are all the commands that we can use in the Dockerfile.
Comments
FROM
CMD
ENTRYPOINT
WORKDIR
ENV
COPY
LABEL
RUN
ADD
.dockerignore
ARG
EXPOSE
USER
VOLUME
Comments
Comments in the dockerfile start with # and you can put anywhere those comments.
# from base image node
FROM
This is the first command in the Dockerfile. Without this, we can’t build an image. We can build the image just with this command. when we build just with FROM, we are actually taking the base image CMD whenever the image is instantiated.
// run the image
docker run -it -d first-dockerfile
// use exec for interactiondocker exec -it f1edbfca3eac bash
Docker image built just with FROM command
CMD
CMD command is used to give the default commands when the image is instantiated, it doesn’t execute while build stage. There should be only one CMD per Dockerfile, you can list multiple but the last one will be executed.
WORKDIR sets the working directory for all the consecutive commands. we can have multiple WORKDIR commands and will be appended with a relative path. Consider the following example where we have two WORKDIR commands leads to /usr/node/app
// open in another terminal and doexec
docker exec -it <container id> bash
// see the current working directorypwd
build and run the image in one terminalexec in another terminal
ENV
ENV sets the environment variables for the subsequent instructions in the build stage. Consider the below example where we define the environment variable workdirectory and we used that later with $. There are two forms: single and multiple values
// open in another terminal and doexec
docker exec -it <container id> bash
// see the current working directorypwd
ENV in action
COPY
COPY is used to copy files or directories from source host filesystem to a destination in the container file system. consider this example where we are copying package.json from our system to container file system. we can verify that while building with the RUN command ls -l.
RUN ls -l command lists package.json
LABEL
LABEL is used to add some metadata to the image. if we use the same label as the base image and the most recent label value is applied.
// inspect the image
docker image inspect dockerfile7
we can see the LABEL in json output of inspect
RUN
RUN executes the instructions in a new layer on top of the existing image and commit those layers and the resulted layer will be used for the next instructions in the Dockerfile. consider this example we are doing npm install and ls -l with one RUN to avoid any additional layers.
npm install takes the package.json and created package-lock.json
ADD
ADD is used to add files or directories and remote files from URL from source host filesystem to a destination in the container file system. Consider this example where we are adding index.js from our system to container file system. We can verify that while building with the RUN command ls -l.
we can see index.js and other files in the container filesystem
.dockerignore
Whenever we build the image at the root level, the entire context is sent to the Docker daemon. Sometimes we don’t need to send all the content to Docker daemon, those files or directories should be added to this .dockerignore file.
for example, we have node_modules in the context and we have added that to .dockerignore file.
node_modules shouldn’t be sent to Docker daemon
ARG
ARG is used to pass some arguments to consecutive instructions and this is only command other than a comment can be used before FROM. We can see ARG usage in the below file and also we can pass that with the build command.
EXPOSE is used as documentation for the port. This is just a communication between the person who builds the image and the person who runs the container. It doesn’t serve any other purpose other than documentation.
For example, we have used port 3070 in the index.js file. So, we are letting people know who runs the container by using EXPOSE instruction in the Dockerfile.
USER
USER instruction sets the user name and optionally the user group to use when running the image and for any instructions that follow it in the Dockerfile
VOLUME
VOLUME is used to create a mount point with the specified name. Following are the examples of Dockerfile and running instructions.