DOCKER HOME LAB | Building containers with Dockerfile

In the previous post we looked at managing, finding and working with containers.

In this post we will be learning how to programmatically make or build container images using dockerfile.


DOCKER HOME LAB | Where to Start ?

DOCKER HOME LAB | Managing Docker Containers

DOCKER HOME LAB | Commit changes to a container

DOCKER HOME LAB | Using / Finding / Sharing in Docker Public index

DOCKER HOME LAB | Building Containers with Dockerfile

DOCKER HOME LAB | Affect container with Dockerfile


Manually or interactively making Docker containers can be a pain, especially if you need to do it often or in multiple places. Docker addresses this by providing “dockerfile” that lets you define how to make a Docker container. Docker can then build the container from the instructions in that file in the same way no matter what environment.

Let’s try this with our sshd server container from the previous example.

When we make a dockerfile its best to create it inside a directory, this is often the product directory for the container. It’s important to do this because of various conventions in the Docker ecosystem.

So for start let’s create a directory for our new Docker image project


Inside we will make a file called dockerfile  ( you can name anything,  but it make more sense to name its dockerfile, so people know what it is, and to follow conventions used by other tooling )

create Dockerfile

We will edit the dockerfile with vim. The format of the dockefile is simple.

It’s simply list of instructions like a script. Each instruction has a key word in all capital letters. The first we will use is FROM

FROM always goes on the top of the file and we use it to specify the base image we want to use for this container. In our example we will use Ubuntu as we have been

Dockerfile FROM

Under FROM its best practice to specify an author or owner of this container using the MAINTAINER instruction, and it takes a name and e-mail address in standard email format


Now the interesting instructions. The main instruction is RUN. This is the equivalent of us loading bash and interactive running a command. Here I’m running two commands in one. I do this to group significant operations into the same layer. Each instruction will also create a new layer in the image.

RUN apt-get update && apt-get install -y openssh-server

Dockerfile RUN

There are many other instructions but we’ll explore them in subsequent posts.

Now we can build this dockerfile using the docker build command, it will take the path or URL of the project directory containing a dockerfile, hence the importance of name it dockerfle and having it in a specific directory.

docker build

When the docker build finish it will have produced an image, but it won’t have a name, just an ID.

docker build

I you have noticed by now, for this execution,  I have set in the command the –t option in the docker build, this flag  is to specify an image repository name to use for the resulting image, passed in the path to the dockerfile project, in this case the current directory.

docker build -t galvezjavier/javier-ssh .

As you can see now we have an image that has the sshd server package installed, and all build from a dockerfile descriptor file

inspect the new container

Dockerfile makes an already quick process even quicker, and introduce the concept of source files for containers, we will expand on this concept in next posts.

Now we will see the use of the ADD instruction to add files to our contain image.

When making container images you may want to include or add configurations files. Since is a best practice to version these files we want to keep them in the source for our container project, but how do we get them into the container? .. We simply use the ADD instruction in dockerfile.

So lets go  back to our sshd server container project directory where we have our dockerfile.

We are going to keep the sshd configuration file in this project and add it to the container we built for some customization.

get file from container

Let’s get a default configuration file to start. We can get it from our container, by simply running cat and piping into a file:

docker run –rm -it galvezjavier/javier-ssh cat /etc/ssh/sshd_config > sshd_config

cat sshd_config

As you can see, now we have a copy of the default sshd configuration file down to our project directory.

edit the config file with custom parameter

For this demo let’s just edit and change the port from 22 to 2222

Dockerfile ADD

Now that we have a custom configuration file that we want to include in our container image.  We will use the ADD instruction in dockerfile to add the new file.

The first argument of ADD is the source path which is relative to the project directory at build time

The second argument is the full path to the destination. It sort of the copy command, except it does have some interesting characteristics, for example the source could be an URL which could be downloaded and placed at the destination argument. You want to check the docs for advanced semantics, but often you will use it in this way

That’s all. Now we can rebuild the image using docker build as we did last time. The command will use the dockerfile with the ADD command and create a new image and copy the sshd-config file to the new container.

docker build

Then we will just go into new container and inspect the sshd configuration file

verify the custom parameter in the container

There is our port change. So this is the file from our project directory!

As you can see now, it is very easily to include file from your project directory into your container image. This is perfect for configuration files or other artifacts. Remember you can add an entire directory or even downloaded files over http.

In the next post we will see how we can use dockerfile instructions to affect users that use the container.

Docker Home Lab

Docker Home Lab


  1. […] DOCKER HOME LAB | Building Containers with Dockerfile […]

  2. […] DOCKER HOME LAB | Building Containers with Dockerfile […]

  3. […] DOCKER HOME LAB | Building Containers with Dockerfile […]

Speak Your Mind


%d bloggers like this: