As you begin your Docker developer journey, learn how to build an image using nothing more than a Dockerfile.
When developing with Docker, you will find numerous pre-made images on Docker Hub, although you may not find the exact image you are looking for. Or maybe you want to create custom images for a specific purpose. If you don’t want to hand over the implementation of your containerized apps and services to an image built by a third party, let me show you how easy it is to build your own Docker image.
SEE: Hack and secure Docker containers (TechRepublic Academy)
What you need to build a Docker image
Docker must be installed on the operating system of your choice. I will be demonstrating this tutorial using Ubuntu Server 22.04; if you are using an operating system other than Ubuntu Linux, you will need to adjust the Docker installation steps. You also need a user with sudo privileges.
How to write a Dockerfile
We are going to create a Dockerfile to build an image based on the latest version of Ubuntu, including NGINX.
Create a new directory to house the Dockerfile with:
Go to that new folder with the following:
Create the new Dockerfile with the command:
In that new file, paste the following content:
# Base the image on the latest version of Ubuntu
# Identify yourself as the image maintainer (where EMAIL is your email address)
# Update apt and update Ubuntu
RUN apt-get update && apt-get upgrade -y
# Install NGINXl
RUN apt-get install nginx -y
# Expose port 80 (or whatever port you need)
# Start NGINX within the Container
CMD ["nginx", "-g", "daemon off;"]
Here is a description of the different guidelines:
- FROM: defines the base image that will be used.
- MAINTAINER: The author of the image.
- RUN: Instructions for executing commands while building the image.
- CMD: Provides default settings for running a command within the image; there can only be one CMD directive in your Dockerfile.
Once your Dockerfile has been created, save and close it with the keyboard shortcut CTRL + X.
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 call our image tr_test_image and build it with the command:
docker build -t tr_test_image .
The reason we have the . at the end of the command is to inform the docker command that we are building within the current working directory. Docker grabs the latest Ubuntu image and builds tr_test_image with NGINX pre-installed. You can check if the image was created with the command:
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 where SERVER is the IP address of the hosting server, you should see the NGINX welcome page.
Easy image building
That’s all you need to do to create your custom images with Dockerfiles. While this image is simple, it illustrates how easy it is to build images and then deploy containers based on those images. As you get more advanced with your image build, only create images with the smallest possible footprint, use a multi-stage build, and add only what is necessary.
Subscribe to TechRepublic’s How to make technology work on YouTube for the latest technical advice for business professionals from Jack Wallen.