Understanding Dockerfile commands, creating image and your first container!

Prafulla Ashtikar
4 min readSep 23, 2020

If you are new to Docker, I would recommend you to read my Understanding Docker blog first.

Dockerfile: A text file that contains all commands needed to build a docker image. The commands are run in the same order as they appear in the Dockerfile. Think of Dockerfile commands as a step-by-step recipe on how to build your docker image.

You ‘build’ the Dockerfile to get an image and you ‘run’ the image to get a container.

Dockerfile example

Create a file named Dockerfile in your favourite text editor and type in the following commands:

FROM ubuntu
LABEL maintainer = "my_email@xyz.com"
RUN apt-get update
CMD ["echo", "Hey! I created my first container!!"]

Understanding the commands:

  1. FROM: Specify Base image . Use FROM scratch instead of FROM ubuntu if you want to create your own base image (Scratch is an explicitly empty image on Docker Hub)
  2. LABEL maintainer: MAINTAINER command (deprecated) is used to set the Author field of the generated images. Use LABEL maintainer command instead for better flexibility. LABEL enables setting any metadata you require, and can be viewed easily.
  3. RUN: Run the specified command. The RUN instruction executes the commands in a new layer on top of the current image and commits the results. The resulting committed image will be used for the next step in the Dockerfile
  4. CMD: Run the specified command when the container is started. The main purpose of CMD is to provide defaults for an executing container. There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last one will take effect. (ENTRYPOINT, which is similar to the CMD command is explained later in this blog.)

RUN gets executed during the creation of image. CMD gets executed when the container is started.

Creating image:

Type the following in the terminal:

docker build -t <imageName>:<tag> <path_of_directory_containing_Dockerfile>

-t is optional if you want to give a name and tag to the Docker image. You can also simply type:

docker build <path_of_directory_containing_Dockerfile>

All the commands in Dockerfile will get executed as “steps” in order and you should be able to see this as the last line in the output:

Successfully built <imageId>

Creating Container:

Copy the <imageId> that you just got and type the following in the terminal:

docker run <imageId>

You should see the output as:

Hey! I created my first container!!

Try adding multiple CMD commands in your DockerFile and observe that only the last ones is considered and others are ignored.

The command ENTRYPOINT and how it is different from CMD:

Just like CMD, ENTRYPOINT runs when the container is started. However, there are some interesting differences between the two commands.

  • You can override CMD by specifying your own command while starting the container. Run your container again but this time as follows:
docker run <imageId> echo 'Oh yes! I can override CMD!!'
  • ENTRYPOINT gets executed as it is, you cannot override it.

Edit your Dockerfile as follows to understand the ENTRYPOINT command:

FROM ubuntu
LABEL maintainer = "my_email@xyz.com"
RUN apt-get update
ENTRYPOINT ["echo", "You cannot override me ever!"]

Build and run like you did earlier to get this output:

You cannot override me ever!

Now, run your container again to see if you can override the ENTRYOINT command:

docker run <imageId> 'You can append though!!'

Observe the new output:

You cannot override me ever! You can append though!!

Let’s play around with CMD and ENTRYPOINT:

Edit your Dockerfile again:

FROM ubuntu
LABEL maintainer = "my_email@xyz.com"
RUN apt-get update
ENTRYPOINT ["echo","Hello"]
CMD ["User"]

Observe that CMD acts as the default for ENTRYPOINT. If you override it while creating the container, you can get different outputs! Fun stuff, isn’t it ? Try it out! :)

Cleaning up:

Now that we have understood Dockerfile, image and container, you can delete the images as well as containers to save space on your system. To automatically remove the container after it exits, add — rm to the above command, like this:

docker run --rm <imageId>

Note: It does not remove the image, it only removes the container.

Following are some useful commands to view and delete your images and containers:

  • docker ps -a : to show a list of all containers
  • docker ps : to show a list of all containers that are currently running
  • docker images : see a list of all images on your system
  • docker rmi <imageId>: to delete image
  • docker rm <containerId>: to delete container
  • docker container start <containerId> : Start one or more stopped containers

There are many more Dockerfile commands, click here to learn more!

--

--