Modal Title

Docker Basics: How to Use Dockerfiles

Ths tutorial will walk you through the process of crafting a Dockerfile. I will demonstrate by using the latest Ubuntu image, update and upgrade that image, and then install the build-essential package. This will be a fairly basic Dockerfile, but one you can easily build upon.
Jun 19th, 2019 12:00pm by
Featued image for: Docker Basics: How to Use Dockerfiles
Feature image by Esi Grünhagen from Pixabay.
The following post is the latest in a series of tutorials on using Docker and associated container tools. Check back every other week for a new edition.

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 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 crafting a Dockerfile. I will demonstrate by using the latest Ubuntu image, update and upgrade that image, and then install 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 own 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 a full name and email address of the image creator.
  • RUN is the central executing directive for Dockerfiles.
  • 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 Dockerfile, we need to make a new directory from which to work. We’ll create the dockerbuild directory with the command:

Change into that newly created directory with the command:

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

Within that file, paste the following:

Save and close that file.

Building Your Image

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

Where NAME is the name of the new image to be created.

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

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

Figure A: The image has been built.

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

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

Figure B: Newly created images ready to be deployed.

Dockerfile For CentOS

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

Change into that directory with the command:

Create the new Dockerfile with the command:

Paste the following contents into that file:

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

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

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 C):

Figure C: The CentOS image is available for deployment.

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 with Dockerfiles, there’s no limit to the types of images you can create.

Group Created with Sketch.
TNS owner Insight Partners is an investor in: Docker.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.