VOOZH about

URL: https://thenewstack.io/docker-basics-how-to-use-dockerfiles/

⇱ Docker Basics: How to Use Dockerfiles - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2025-01-30 11:20:50
Docker Basics: How to Use Dockerfiles
tutorial,
Containers

Docker Basics: How to Use Dockerfiles

This tutorial will walk you through Dockerfile basics, including the process of how to craft and how to run a Dockerfile. This will be a fairly basic Dockerfile, but one you can easily build upon.
Jan 30th, 2025 11:20am by Jack Wallen
👁 Featued image for: Docker Basics: How to Use Dockerfiles
Feature image by Esi Grünhagen from Pixabay.
This article has been updated from when it was originally published on June 18, 2019.

Understanding Dockerfiles: Construction and Application

A Dockerfile is a script that automates the creation of Docker images, essential for building containers. Key sections include specifying a base image (FROM), executing commands (RUN), copying files (COPY), exposing ports, and setting environment variables. Utilizing Dockerfiles enhances efficiency, reusability, and standardization in image creation.

To construct a Dockerfile, create a directory and define specific keywords like FROM, RUN, and MAINTAINER. For example, a basic Dockerfile for updating an Ubuntu image involves commands for updating packages and installing build-essential.

After creating the Dockerfile, build the image using the command docker build -t “NAME:Dockerfile,” (where NAME is the name you want to use).  This process enables the management of multiple variations of containers efficiently, simplifying deployment from a single image.

Overview

A Dockerfile is a text file that contains instructions for building and configuring an image in Docker. It’s used to automate the process of creating a container from scratch, using various layers to build up the final image. Dockerfiles typically contain the following sections:

  • From: This line specifies the base image that your new image will be built on top of.
  • Run or Command: These lines run commands during the build process, such as installing dependencies or setting environment variables.
  • Copy: This instruction copies files from the current directory into the container at a specific location.
  • Exposed port: This line specifies which ports will be exposed when the image is created and run in containers.
  • Environment variables: These lines set environment variables for your application to use.

Using Dockerfiles has several benefits, including efficient image creation, reusability, portability, standardization, a faster build process, and better testing.

In this article, you’ll learn the basics of a Dockerfile, how to construct a Dockerfile, how to build a Docker image from a Dockerfile, and how to deploy containers using a Dockerfile.

By using a Docker image, it is not only possible to deploy one container after another, it’s quite easy. Once you’ve pulled the image from a registry (such as Docker Hub), each container can then be deployed with a single docker command. But what happens when you find yourself having to deploy numerous containers (each for a different purpose) from the same image? All of a sudden, the management of those containers can get a bit cumbersome.

Say, for example, you pull down the latest Ubuntu image for development. Before you can develop with that container, there are a number of modifications you want to make to the image (such as upgrading software and adding the necessary development packages for the job at hand).

For this, you could manually edit each image as needed (creating a new image for each necessary variation on the theme), or you could construct a Dockerfile for each variation. Once you have your Dockerfile basics constructed, you can quickly build the same image over and over, without having to take the time to do it manually. Carefully crafted Dockerfiles can save you considerable time and effort.

I want to walk you through the process of how to use Dockerfile. I will demonstrate by using the latest Ubuntu image, updating and upgrading that image, and then installing the build-essential package. This will be a fairly basic Dockerfile, but one you can easily build upon.

Dockerfile Basics

Before we construct our Dockerfile, you need to understand what makes up the file. This will be a text file, named Dockerfile, that includes specific keywords that dictate how to build a specific image. The specific keywords you can use in a file are:

  • ADD copies the files from a source on the host into the container’s filesystem at the set destination.
  • CMD can be used for executing a specific command within the container.
  • ENTRYPOINT sets a default application to be used every time a container is created with the image.
  • ENV sets environment variables.
  • EXPOSE associates a specific port to enable networking between the container and the outside world.
  • FROM defines the base image used to start the build process.
  • MAINTAINER defines the full name and email address of the image creator.
  • RUN is the central executing directive for Dockerfiles, also known as the run Dockerfile command.
  • USER sets the UID (or username) which is to run the container.
  • VOLUME is used to enable access from the container to a directory on the host machine.
  • WORKDIR sets the path where the command, defined with CMD, is to be executed.
  • LABEL allows you to add a label to your docker image.

Not all keywords are required for a Dockerfile to function. Case in point, our example will only make use of FROM, MAINTAINER, and RUN.

Constructing the Dockerfile

Before we create the basic Dockerfile, we need to make a new directory from which to work. We’ll create the dockerbuild directory with the command:

mkdir ~/dockerbuild

Change into that newly created directory with the command:

cd ~/dockerbuild

Now, we’ll craft our Dockerfile. Create the new file with the command:

nano Dockerfile

Within that file, paste the following to run a Dockerfile:

FROM ubuntu:latest

MAINTAINER NAME EMAIL

RUN apt-get -y update

RUN apt-get -y upgrade

RUN apt-get install -y build-essential

NAME is your full name, and EMAIL is your email address.

Save and close that file.

Building Your Docker Image

With the basic Dockerfile complete, you can now build the image from that file. Issue the command (from within the ~/dockerbuild directory):

docker build -t "NAME:Dockerfile" .

Where NAME is the name of the new image to be created, it’s important to note that NAME must be all lower case, otherwise, the build will fail.

For example, say you want to create images for web development, app development, and security development. You could issue the following commands:

docker build -t "appdev:Dockerfile" .

docker build -t "webdev:Dockerfile" .

docker build -t "secdev:Dockerfile" .

This will begin the process of downloading the ubuntu:latest image and building the image according to the Dockerfile (Figure 1):

👁 Image

Figure 1: The image has been built.

Once the build(s) are complete, issue the command:

docker images

You should see all of the newly build images, now available for use (Figure 2):

👁 Image

Figure 2: Newly created images ready to be deployed.

How to Run Dockerfile for Rocky Linux

Let’s say you want to create an image using Rocky Linux that updates the pulled Rocky Linux image and installs a web server. For this, we’d first create a new directory with the command:

mkdir ~/rockylinux

Change into that directory with the command:

cd ~/rockylinux

Create the new Dockerfile with the command:

nano Dockerfile

Paste the following contents into that file for the Dockerfile run command:

FROM rockylinux:9

MAINTAINER NAME EMAIL

RUN dnf makecache

RUN dnf upgrade -y

RUN dnf install -y httpd

Where NAME is your name and EMAIL is your email address.

Save and close the file. Build the image with the command:

docker build -t “webdev_rockylinux:Dockerfile” .

Depending on how much upgrading is necessary, this particular build will take a bit longer than the Ubuntu image. Once the build completes, issue the command docker images to see that your newly built (CentOS-based) image is ready (Figure 3):

👁 Image

Figure 3: The Rocky Linux image is available for deployment.

Docker Image Building Made Easy

And that’s all there is to building Docker images with Dockerfiles. This is a much more efficient and standard method for creating new images than is committing changes to a pulled image. Once you are proficient in how to use Dockerfiles, there’s no limit to the types of images you can create.

(Editor’s note: This post has been updated. It originally ran June 19, 2019).

FAQ: Using Dockerfiles

1. What is a Dockerfile?

A Dockerfile is a text file that contains a series of instructions on how to build a Docker image. It defines the environment and the application that will run inside a container.

2. What are the basic commands used in a Dockerfile?

Here are some essential commands you might encounter:

  • FROM: Specifies the base image to use for the new image.
  • RUN: Executes commands in a new layer on top of the current image and commits the results.
  • COPY: Copies files or directories from the host filesystem into the image.
  • CMD: Provides defaults for an executing container, such as the command to run.
  • ENTRYPOINT: Configures a container to run as an executable.

3. How do I build an image from a Dockerfile?

To build an image, navigate to the directory containing your Dockerfile and run the following command in your terminal:

docker build -t your-image-name .

4. What is the difference between CMD and ENTRYPOINT?

  • CMD: Sets default commands and/or parameters for the container. It can be overridden when running the container.
  • ENTRYPOINT: Configures the container to run as an executable. It is not overridden by command line arguments.

5. How can I optimize my Dockerfile?

To optimize your Dockerfile, consider the following tips:

  • Minimize the number of layers: Combine commands where possible using &&.
  • Order your commands wisely: Place commands that change less frequently at the top to take advantage of caching.
  • Use .dockerignore: Exclude files and directories that are not needed in the image to reduce size.

6. What is a multi-stage build?

A multi-stage build allows you to use multiple FROM statements in your Dockerfile, which is useful for separating the build environment from the runtime environment to help significantly reduce the final image size.

7. How do I run a container from my image?

Once your image is built, you can run a container using:

docker run -d IMAGE

Where IMAGE is the name of the image to be used.

8. Where can I find more information about Dockerfiles?

For more detailed information, you can refer to the official Docker documentation, which provides extensive resources on Dockerfiles and their usage.

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
Docker is a sponsor of The New Stack.
TNS owner Insight Partners is an investor in: Docker.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.