Using Dockerfiles to build new Docker images

Using Dockerfiles to build new Docker images

Category : How-to

Get Social!

docker-logoIn a previous blog post I detailed the steps involved in creating a new Docker container, making some changes and saving the image back to the local repository. The process described works well but it’s a very manual affair which Docker has a solution for.

Docker has a scripting language which can be used to create a new instance with a predefined list of commands and properties which will be used to create your new Docker instance.You could, for example, use a docker file to install Apache, configure the firewall and any further configurations we may need to make.

The benefits to using a Dockerfile, rather than making all the changes directly and saving the image are that the underlying OS and the additions that you wish to make are completely independent. You can, for example, run a Dockerfile on any OS image. Using the example that follows, you could run the Dockerfile on either a Debian or Ubuntu OS without changing a thing.

Create a directory to hold your DockerFile project, which we’ll call apache2 for this example.  I’ll be placing all my DockerFiles in their own project directory under dockerfiles in my home directory.

mkdir -p /home/james/dockerfiles/apache2

Open a text file named Dockerfile in your favourite text editor in the project folder we just created. This is the standard file structure that Docker expects when creating DockerFiles.

vi /home/james/dockerfiles/apache2/Dockerfile

There are various commands we can use within a Dockerfile. The first command is the FROM statement which indicates which image should be used when creating your instance. I’m going to use the ubuntu image which I have previously downloaded to my local Docker server.

FROM ubuntu:14.04

Add MAINTAINER or author for the template. This is your name, username or whatever handle you’d like to be known as.

MAINTAINER James Coyle <[email protected]>

We are now going to use the RUN command to specify the commands that should be executed on the instance during creation. The commands will be executed in the order they appear in the Dockerfile. We will be installing Apache2 so we’ll be using the apt-get command to install.

RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get install -y apache2

Next we’ll make a data directory on the host where we will keep our web files that are to be served by Apache.

RUN mkdir -p /data/apache/www
RUN chown -R root:www-data /data/apache/www

It’s a good idea to separate the Docker container from any user data so that a the container can be used for different purposes. What this mean in our example is that we will keep all the website data (HTML files, etc.) out of our container, leaving only the Apache software and general configuration within the container. This means that we can reuse our Docker image to create containers for other websites.

Using the Docker VOLUME command we can map a directory on the Docker host to a folder inside the container which will be configured once your container is created. The below example makes the directory /data/apache/www available for mapping later.

Add the VOLUME reference to your Dockerfile.

VOLUME /data/apache/www

We will need to be able to reach our container on port 80 so that we can use the Apache service over the network. Docker uses the EXPOSE command followed by a port number to allow incoming traffic to the container. Add the below entry to allow port 80.

EXPOSE 80

We now need to do some find and replace magic to change the Apache default site configuration to point to our new location, rather than the Apache default. This isn’t a Docker specific command, but is required for this example.

RUN sed -i 's#DocumentRoot /var/www#DocumentRoot /data/apache/www#' /etc/apache2/sites-available/000-default.conf

Finally we’ll need to tell Docker what should be executed in order to ‘run’ this container.  For this example, we use the apache2ctl command with the FOREGROUND switch.

ENTRYPOINT /usr/sbin/apache2ctl -D FOREGROUND

And that’s it, your first DockerFile. Run your newly created DockerFile to build the image by changing to the project directory and using the docker build command to create it. Use the -t switch to specify a tag for the image.

cd /home/james/dockerfiles/apache2
docker build -t apache2:test .

It will take a few minutes for the image to build. Once complete, you’ll be able to see it in the Docker image list by using the command docker image.

[email protected]:~/apache2# docker images
REPOSITORY  TAG  IMAGE ID     CREATED        VIRTUAL SIZE
apache2     test fdf56ad12ffa 4 minutes ago  228.2 MB

 

The whole DockerFile:

FROM ubuntu:14.04

MAINTAINER James Coyle <[email protected]>

RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get install -y apache2

RUN mkdir -p /data/apache/www
RUN chown -R root:www-data /data/apache/www

VOLUME /data/apache/www

EXPOSE 80

RUN sed -i 's#DocumentRoot /var/www#DocumentRoot /data/apache/www#' /etc/apache2/sites-available/000-default.conf

ENTRYPOINT /usr/sbin/apache2ctl -D FOREGROUND

 


Related posts:


4 Comments

Bob Fitzgerald

16-Nov-2015 at 10:25 pm

Excellent tutorial, very useful for newcomers to the world of docker.

    james.coyle

    16-Nov-2015 at 10:29 pm

    Thanks, Bob.

Tobias

20-Dec-2015 at 3:40 pm

Thanks for a great tutorial!

I got an error during “RUN apt-get upgrade -y”:
Get:25 http://archive.ubuntu.com/ubuntu/ trusty/main ssl-cert all 1.0.33 [16.6 kB]
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
dpkg-preconfigure: unable to re-open stdin:

I wonder how to fix this…

Maybe this is the solution:
http://stackoverflow.com/questions/22466255/is-it-possibe-to-answer-dialog-questions-when-installing-under-docker

Rafael Rojas

31-Dec-2016 at 9:06 pm

Hi James, great tutorial. One question, when you define the volume, where is the directory? On localhost or in the container? Thnx

Leave a Reply to Tobias Cancel reply

Visit our advertisers

Search

Quick Poll

What type of VPN protocol do you use?

Visit our advertisers