If you’re starting your Docker developer journey, learn how to build an image using nothing more than a Dockerfile.
When developing with Docker, you’ll find numerous pre-made images on Docker Hub, though you might not find the exact image you want. Alternatively, perhaps you want to create custom images for a specific purpose. If you don’t want to hand over 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)
What you need to build a Docker image
You’ll need Docker installed on your operating system of choice. I’ll demonstrate this tutorial with Ubuntu Server 22.04; if you use an OS other than Ubuntu Linux, you’ll need to modify the Docker installation steps. You also need a user with sudo privileges.
How to write a Dockerfile
We’re going to create a Dockerfile to build an image based on the latest version of Ubuntu, which includes NGINX.
Create a new directory to house the Dockerfile with:
Change into that new directory with the following:
Create the new Dockerfile with the command:
In that new file, paste the following contents:
# 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’s a description of the different directives:
- 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 defaults for executing a command within the image; there can be only one CMD directive in your Dockerfile.
With your Dockerfile created, save and close it with 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’ll 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 will pull the latest Ubuntu image and build tr_test_image with NGINX pre-installed. You can verify 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 http://SERVER:8001, where SERVER is the IP address of the hosting server, you should see the NGINX welcome page.
Easy image building
That’s all there is to creating your custom images with Dockerfiles. Although this image is simple, it makes it clear how easy it is to build images and then deploy containers based on those images. As you get more advanced with your image building, only create images with the smallest footprint possible, use multi-stage build and add only what is necessary.
Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.