Ubuntu Server 18.04 – Automating Docker image creation with Dockerfiles

How to build a Docker Compose YAML files

I’ve mentioned previously in this tutorial that anything worth having a server do more than once should be automated, and building a Docker container is no exception. A Dockerfile is a neat way of automating the building of Docker images by creating a text file with a set of instructions for their creation. Docker is able to take this file, execute the commands it contains, and build a container. It’s magic.

The easiest way to set up a Dockerfile is to create a directory, preferably with a descriptive name for the image you’d like to create (you can name it whatever you wish, though), and inside it create a text file named Dockerfile. For a quick example, copy this text into your Dockerfile and I’ll explain how it works:

FROM ubuntu
MAINTAINER Jay <jay@somewhere.net>
# Update the container's packages
RUN apt update; apt dist-upgrade -y
# Install apache2 and vim
RUN apt install -y apache2 vim
# Make Apache automatically start-up`
RUN echo "/etc/init.d/apache2 start" >> /etc/bash.bashrc

Let’s go through this Dockerfile line by line to get a better understanding of what it’s doing:

FROM ubuntu

We need an image to base our new image on, so we’re using Ubuntu as a starting point.
This will cause Docker to download the ubuntu:latest image from the Docker Hub, if we haven’t already downloaded it locally. If we do have it locally, it will just use the locally cached version.

MAINTAINER Jay <myemail@somewhere.net>

Here, we’re setting the maintainer of the image. Basically, we’re declaring its author. This is optional, so you don’t need to include that if you don’t want to.

# Update the container's packages

Lines beginning with a hash symbol (#) are ignored, so we are able to create comments within the Dockerfile. This is recommended to give others a good idea of what your Dockerfile is doing.

RUN apt update; apt dist-upgrade -y

With the RUN command, we’re telling Docker to run a specific command while the image is being created. In this case, we’re updating the image’s repository index and performing a full package update to ensure the resulting image is as fresh as can be. The -y option is provided to suppress any requests for confirmation while the command runs.

RUN apt install -y apache2 vim-nox

Next, we’re installing both apache2 and vim-nox. The vim-nox package isn’t required, but I personally like to make sure all of my servers and containers have it installed. I mainly included it here to show you that you can install multiple packages in one line.

RUN echo "/etc/init.d/apache2 start" >> /etc/bash.bashrc

In the previous section, we copied the startup command for the apache2 daemon into the /etc/bash.bashrc file to serve as an example of how to automatically start an application. We’re including that here so we won’t have to do this ourselves when containers are created from the image. Naturally, you’d want to write an ENTRYPOINT script, but that’s a relatively advanced topic and we don’t really need to focus on that to get an understanding of how Docker works.

Great, so now we have a Dockerfile. So, what do we do with it? Well, turn it into an image of course! To do so, we can use the docker build command, which can be executed from within the directory that contains the Dockerfile. Here’s an example of using the docker build command to create an image tagged packt/apache-server:1.0:

docker build -t packt/apache-server:1.0 .

Once you run that command, you’ll see Docker create the image for you, running each of the commands you asked it to. The image will be set up just the way you like. Basically, we just automated the entire creation of the Apache container we used as an example in this section. If anything goes wrong, Docker will print an error to your shell. You can then fix the error in your Dockerfile and run it again, and it will continue where it left off.

Once complete, we can create a container from our new image:

docker run -dit -p 8080:80 packt/apache-server:1.0 /bin/bash

Almost immediately after running the container, the sample Apache site will be available on localhost:8080 on the host. With a Dockerfile, you’ll be able to automate the creation of your Docker images. That was easy, wasn’t it? There’s much more you can do with Dockerfiles; feel free to peruse Docker’s official documentation to learn more. Exploration is key, so give it a try and experiment with it.

Comments are closed.