Friday, November 15, 2024
Google search engine
HomeGuest BlogsHow to Create Docker Image with Dockerfile

How to Create Docker Image with Dockerfile

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

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

tutorial on how to create or build docker image with dockerfiletutorial on how to create or build docker image 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 , Installing Docker on CentOS 7/RHEL 7 or CentOS 8.

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

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
command for creating a new dockerfile in linuxcommand 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 imageexample 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 contentexample 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 dockerfileexample 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 imagesexample 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 imageexample 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 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.

Was this article helpful?
YesNo

RELATED ARTICLES

Most Popular

Recent Comments