Creating Docker File

Anil Kumar Chhetri
4 min readMay 24, 2021

In this post, I am going to discuss some common docker directives.

Photo by Ian Taylor on Unsplash

Introduction

Docker is a set of platform as a service (PaaS) products that use OS-level virtualization to deliver software in packages called containers” — Wikipedia

A directive is a command that is used to create a docker image. By creating a docker file we can create our own docker image that fulfills our purpose. We can configure our own program to run on docker. So some of the most used docker directive that we discussed today are as follows:

  1. FROM Directive
  2. LABEL Directive
  3. RUN Directive
  4. CMD Directive
  5. ENTRYPOINT Directive
  6. WORKDIR Directive
  7. COPY Directive
  8. ADD Directive
  9. VOLUME Directive

1. FROM Directive:

Usually FROM directive is the first line of most of the docker file. This is used to specify the parent image of the custom docker image. All the customization that we do is on top of the parent image. Parent image can be any image from docker hubs like ubuntu, alpine, Postgres, and many more. The FROM directive accepts the value of the image and tag. if a tag is not specified then the latest tag will be used.

Additionally, if we need to build the parent image then we use a scratch image, it is an empty image.

2. LABEL Directive

This directive is used to provide information about the author who is writing and maintaining the docker file. The Label directive takes key-value pair. It may also contain information about the docker file itself and act as a metadata provider. And lastly, there can be any number of LABEL directives in a docker file or you can write multiple key-value pairs in a single line.

Label code snippets

3. RUN Directives

The RUN directive is used to execute commands during the image build. This will create a new layer on top of the base image we got from FROM directive, execute the specific command and commit the results to the newly created layer. The main purpose of RUN directives is to install the required packages, update packages, create users or groups, and so on. Same as LABEL there can be multiple RUN directives also we can add multiple commands in the single RUN directive where each command is separated with && symbol.

RUN Code snippets

4. CMD Directive

A CMD directive is used to provide a default initialization command that will be executed with the container is created from the image. A docker file cannot contain more than one CMD directive if multiple CMD directive is created than only the last CMD directive will be effected. if an additional argument is provided while running the container CMD directive will be ignored.

CMD Code snippets
Note: Both RUN and CMD directives can be use to execute a shell command. RUN directive will be executed during the image build process, while command provide with CMD will be executed once a container is launched from built image.
RUN can be executed multple time while CMD can only be executed once.
Real time examples of these command can be, RUN is used to install the program to our image while CMD will be used to start the packages after image is built.

5. ENTRYPOINT Directive

ENTRYPOINT directive is used to provide the default initialization command that will be executed when a container is created from the Docker Image. As we can see both CMD and ENTRYPOINT directives perform the same action. The difference between both directives is that the ENTRYPOINT command cannot be overwritten by default. But if we want to overwrite the ENTRYPOINT we have to specify --entrypoint flag while running the image.

EntryPoint Code snippets

6. WORKDIR Directive

The WORKDIR directive is used to specify the current working directory of the docker container. Directive like CMD, ENTRYPOINT, ADD, COPY and RUN will be executed in this directory which is defined in the WORKDIR directive. Docker will automatically create the directory if not exists and make it the current working directory.

WORKDIR code snippets

7. COPY Directive

Copy directive is used to copy the files from our local directory to the docker image directory while the build of the image is completed. As its name suggested it takes files from one folder to the next location hence it required two paths. source location and destination location.

Copy Code snippets

8. Add Directive

Add directive is very similar to Copy directive as it has the same format. The only difference is that Add directive can take the URL as a source and also it automatically unzip the zip file while copying it to the destination location.

Copy is usually preferred over Add unless you have to copy the archive folder to the destination and unzip there.

Add code snippets

9. VOLUME Directive

Volume directive is used to persist the data and share the data between containers. We use volume directive within the Dockerfile to create docker volumes. Once the volume is created in the underlying host machine. All file changes to the volume mount of the Docker container will be copied to the mapped directory of the host machine.

Conclusion

Now we know about some directives that are mostly used in a docker file. Let's keep all we have learned in a single docker file and create an image out of it. After copying the content of Dockerfile we use the docker build command to create an image.

--

--