How To Create A Docker Image Using A Dockerfile


How To Create A Docker Image Using A Dockerfile

How To Create A Docker Image Using A Dockerfile

What is Docker?

Have you ever wondered about Docker, Docker images, and Docker files? These questions often pop up in people’s minds. In this discussion, we will explore each of these concepts one by one.

Docker is an open-source platform for automating the deployment, scaling, and management of applications in containers. Containers are a lightweight, stand-alone, and executable software package that includes everything needed to run a piece of software, including the code, runtime, system tools, libraries, and settings.

Docker allows developers to package their applications into containers and run them in any environment, without the need for setting up a specific infrastructure for each application. This makes it easy to develop, test, and deploy applications quickly and consistently across different environments, and to scale and manage those applications in a efficient and effective way.

The core technology behind Docker is containerization, which allows multiple containers to run on the same host, sharing the host operating system kernel but isolating applications and their dependencies from each other. This enables containers to be highly portable and eliminates many of the challenges associated with deploying applications across different environments.

Docker is based on the idea of images, which are pre-configured snapshots of applications and their dependencies. Docker images can be created and stored in a central repository, such as Docker Hub, and used to create containers on any host with the Docker engine installed.

Docker provides a flexible, scalable, and efficient solution for building, deploying, and running applications in containers, and has become a popular choice for organizations of all sizes.

How Docker works?

Docker works by using a client-server architecture, where the Docker client communicates with the Docker daemon to build, run, and manage containers. The Docker client and daemon can run on the same host, or they can communicate over a network.

Here’s a high-level overview of how Docker works:

  1. Building containers: The Docker client communicates with the Docker daemon to build an image from a Dockerfile, which is a script that specifies the application and its dependencies. The Docker daemon pulls the required files and builds the image, which can then be stored in a registry for later use.
  2. Running containers: When a user runs a container, the Docker client communicates with the Docker daemon to create a new container from an image. The Docker daemon creates a new container and allocates the required resources, such as memory and CPU, to run the application.
  3. Managing containers: Docker provides a number of tools and APIs for managing containers, including starting and stopping containers, checking the status of containers, and removing containers that are no longer needed.
  4. Networking: Docker provides a number of networking options, including the ability to create isolated networks for containers and to map container ports to host ports for external access.
  5. Volumes: Docker also supports data persistence through volumes, which are directories or files on the host file system that can be mapped to a container and persist even if the container is deleted.

Docker works by abstracting the underlying host operating system and providing a standard interface for building, deploying, and managing containers, which makes it easy to develop, test, and deploy applications in a consistent and scalable manner.

Difference between Docker container and Docker Image

An active instance of a Docker image is known as a container. In other words, a Docker container is an isolated environment created from a Docker image.

A Docker image is a blueprint for creating a Docker container. It is a small, standalone executable package that contains the code, a runtime, libraries, environment variables, and configuration files, as well as anything else required to operate a piece of software.

In short, Docker images are the source code for containers, while containers are the running instances of images.

Learn how to create an image only from a Dockerfile if you’re just starting out as a Docker developer.

On Docker Hub, there are many ready-made images that you can use when developing with Docker, albeit you might not find the exact image you’re looking for. Alternately, perhaps you wish to produce unique photographs for a certain objective.

we’ll demonstrate how simple it is to create your own Docker image if you don’t want to entrust the deployment of your containerized apps and services to a third-party image.

What is necessary to create a Docker image

Docker must be set up on your preferred operating system. If you use a different operating system than Ubuntu Linux, you will need to adapt the Docker installation instructions.

We’ll be using Ubuntu Server 22.04 to demonstrate this tutorial. A user with sudo access is also required.

Creating a Dockerfile

To build an image based on the most recent release of Ubuntu and NGINX, we’ll develop a Dockerfile.

To store the Dockerfile, make a new directory with:

mkdir docker_images

Enter that new directory using the following method:

cd docker_images

Make a new Dockerfile by using the following command:

nano Dockerfile

In that new file, paste the following information:


# Base the image on the latest version of UbuntuFROM ubuntu:latest


## Declare your role as the image keeper (where EMAIL is your email address)
Label "EMAIL" is maintained.


# Update apt as well. Ubuntu RUN apt-get upgrade -y and apt-get update


# Install NGINXlRUN apt-get install nginx -y


# Open up port 80 (or whatever port you need) 80 EXPOSE


# Launch NGINX by typing ["nginx", "-g", "daemon off;"] in the ContainerCMD.

The various instructions are described as follows:

  • FROM: Specifies the default picture to be used.
  • MAINTAINER: The creator of the picture.
  • RUN: Directions for carrying out commands as the image is being created.
  • CMD: Provides defaults for running a command inside the image; your Dockerfile can only contain one CMD directive.

Create your Dockerfile, then use the CTRL+X keyboard shortcut to save and close it.

Creating a Docker image

To ensure that you always know which image to use, give your Docker image a unique name. Our image will be built using the command: We’ll call it tr test image.

docker build -t tr_test_image .

To let the docker command know that we are creating inside the current working directory, we add the period (.) at the end of the command. The most recent Ubuntu image will be retrieved by Docker, which will then create the NGINX-ready tr test image. The following command will allow you to confirm the image was made:

docker images

You ought to see something like this in the output:

tr_test_image latest 663ea67dc848   15 minutes ago   174MB

Next, using a command similar to this, you may deploy a container based on your image:

docker run -d -p 8001:80 tr_test_image

The NGINX welcome page should appear if you point a web browser to http://SERVER:8001, where SERVER is the IP address of the hosting server.

Easy Image Creation

The process of generating your own images using Dockerfiles ends there. Despite being straightforward, this image demonstrates how simple it is to create images and then deploy containers based on those images.

As you develop your picture building skills, only produce images with the smallest feasible footprint, employ multi-stage builds, and add only what is required.

Alexia Barlier
Faraz Frank

Hi! I am Faraz Frank. A freelance WordPress developer.