Introduction

A 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 create a specific Docker image.

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

tutorial on how to create docker images with dockerfile

Prerequisites

  • 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 or Installing Docker on CentOS 7/RHEL 7.


Note: Users who are just starting to use Docker can learn more about managing Docker containers and basic Docker commands.


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 create.

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

command for creating a new dockerfile in linux

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

MAINTAINER sofija

RUN apt-get update

CMD ["echo", "Hello World"]

example of dockerfile used to build simple docker image

    • 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

example of dockerfile content

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 .

example of a command to build docker image from dockerfile


Note: You may receive an error saying Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker...

This means the user does not have permission to access the Docker engine. Solve this problem by adding sudo before the command or run it as root.


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.

example of available local docker images

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

example of launching a new container based on the docker image

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

Conclusion

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

Now that you have learned how to create and use this document, move on to exploring additional possibilities Docker has to offer.