Skip to content
Home » How to Create Docker Image with Dockerfile

How to Create Docker Image with Dockerfile

Dockerfile is a script with instructions on how to build a Docker image. These instructions are, in fact, a group of commands executed automatically in the Docker environment to build a specific Docker image.

In this tutorial, learn how to create Docker image with a Dockerfile.


  • A Linux system
  • Access to the command-line/terminal window
  • Access to a user account with root or sudo privileges
  • Docker installed and configured

Install Docker

If you are interested in how to use a Dockerfile to create an image, you probably already have Docker installed on your system.

In the unlikely case you do not, simply refer to one of our installation guides for Installing Docker on Ubuntu , Installing Docker on CentOS 7/RHEL 7 or CentOS 8.

How to Create a Dockerfile

The first thing you need to do is to create a directory in which you can store all the Docker images you build.

1. As an example, we will create a directory named MyDockerImages with the command:

mkdir MyDockerImages

2. Move into that directory and create a new empty file (Dockerfile) in it by typing:

cd MyDockerImages
touch Dockerfile

3. Open the file with a text editor of your choice. In this example, we opened the file using Nano:nano Dockerfile

4. Then, add the following content:

FROM ubuntu


RUN apt-get update

CMD ["echo", "Hello World"]
  • FROM – Defines the base of the image you are creating. You can start from a parent image (as in the example above) or a base image. When using a parent image, you are using an existing image on which you base a new one. Using a base image means you are starting from scratch (which is exactly how you would define it: FROM scratch).
  • MAINTAINER – Specifies the author of the image. Here you can type in your first and/or last name (or even add an email address). You could also use the LABEL instruction to add metadata to an image.
  • RUN – Instructions to execute a command while building an image in a layer on top of it. In this example, the system searches for repository updates once it starts building the Docker image. You can have more than one RUN instruction in a Dockerfile.
  • CMD – There can be only one CMD instruction inside a Dockerfile. Its purpose is to provide defaults for an executing container. With it, you set a default command. The system will execute it if you run a container without specifying a command.

5. Save and exit the file.

6. You can check the content of the file by using the cat command:

cat Dockerfile

Build a Docker Image with Dockerfile

The basic syntax used to build an image using a Dockerfile is:

docker build [OPTIONS] PATH | URL | -

To build a docker image, you would therefore use:

docker build [location of your dockerfile]

If you are already in the directory where the Dockerfile is located, put a . instead of the location:docker build .

By adding the -t flag, you can tag the new image with a name which will help you when dealing with multiple images:

docker build -t my_first_image .

Once the image is successfully built, you can verify whether it is on the list of local images with the command:docker images

The output should show my_first_image available in the repository.

Create a New Container

Launch a new Docker container based on the image you created in the previous steps. We will name the container “test” and create it with the command:

docker run --name test my_first_image

The Hello World message should appear in the command line, as seen in the image above.


Using Dockerfile is a simpler and faster way of building Docker image. It automates the process by going through the script with all the commands for assembling an image.

When building a Docker image, you also want to make sure to keep Docker image size light. Avoiding large images speeds-up building and deploying containers. Therefore, it is crucial to reduce the image size to a minimum.

Leave a Reply

Your email address will not be published. Required fields are marked *