How to

How to build a Docker image from a Dockerfile

As you begin your journey as a Docker developer, you will learn how to build an image using nothing more than a Dockerfile.

A developer who writes software.
Image: Seventyfour/Adobe Stock

When developing with Docker, you can find numerous prebuilt images on Docker Hub, although you may not find the exact image you want. Alternatively, you might want to create custom images for a specific purpose. If you don’t want to hand off the deployment of your containerized apps and services to an image built by a third party, I’ll show you how easy it is to build your own Docker image.

SEE: Hacking and securing Docker containers (TechRepublic Academy)

Jump to:

What you need to build a Docker image

Docker must be installed on the operating system of your choice. I will demonstrate this tutorial using Ubuntu Server 22.04; If you’re using an operating system other than Ubuntu Linux, you’ll need to change the Docker installation steps. You also need a user with sudo privileges.

How to write a Dockerfile

We will create a Dockerfile to build an image based on the latest version of Ubuntu which includes NGINX.

Create a new directory to hold the Dockerfile:

mkdir docker_images

Change to this new directory using the following steps:

cd docker_images

Create the new Dockerfile with the command:

nano Dockerfile

In this new file, paste the following content:


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


# Identify yourself as the image maintainer (where EMAIL is your email address)LABEL maintainer="EMAIL"


# Update apt and update UbuntuRUN apt-get update && apt-get upgrade -y


# Install NGINXlRUN apt-get install nginx -y


# Expose port 80 (or whatever port you need)EXPOSE 80


# Start NGINX within the ContainerCMD ["nginx", "-g", "daemon off;"]

Here is a description of the different directives:

  • FROM: Defines the base image that is used.
  • MAINTENANCE: The author of the picture.
  • RUN: Instructions for running commands when creating the image.
  • CMD: Provides default values ​​for executing a command within the image; There can only be one CMD directive in your Dockerfile.

After creating your Dockerfile, save and close it using the CTRL+X keyboard shortcut.

How to build a Docker image

Be sure to give your Docker image a specific name so you always know which image to use. We name our image tr_test_image and build it with the command:

docker build -t tr_test_image .

The reason why we the . At the end of the command, the docker command is informed that we are building within the current working directory. Docker pulls the latest Ubuntu image and creates tr_test_image with NGINX preinstalled. You can verify that the image was created with the following command:

docker images

You should see something like this in the output:

tr_test_image latest 663ea67dc848   15 minutes ago   174MB

You can then deploy a container based on your image with a command like this:

docker run -d -p 8001:80 tr_test_image

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

Simple image building

That’s all you need to start building your custom images with Dockerfiles. Although this image is simple, it makes it clear how easy it is to create images and then deploy containers based on those images. As you get more advanced with your image creation, only create images in the smallest footprint possible, use a multi-stage build, and add only what is necessary.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for the latest tech advice for business professionals from Jack Wallen.


Leave a Reply

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

Back to top button