An attempt at walking you through a basic 'Docker' setup

This will be a walk through of a very basic Docker setup, from pulling down a base image to making a simple change and commit. I'm going to assume you already know what Docker is.

Searching Docker images

Let's pull down a Docker base image.

Using the docker search command will allow you to search for images. You will get an output of different images, both official and unofficial. These are hosted on Docker's own hub website. For this example I will search for an 'Ubuntu' base image.

docker search ubuntu

Pulling and running

Pulling the image you want is a simple case of typing:

docker pull ubuntu

Once you have pulled this down, you can create a container based on your image. In this example I will use an interactive shell to launch into my new container based on my base ubuntu image. The container will stop immediately unless you have decided to run an application as a daemon.

docker run -i -t ubuntu bash

The -t flag assigns a pseudo-tty or terminal inside our new container and the -i flag allows us to make an interactive connection by grabbing the standard in (STDIN) of the container. Basically allowing us to enter the container and input whatever command you want. You can also create a daemonized container which would run a process of your choosing in the background of a container, the example I've linked shows you how to echo 'hello world' in a loop.

If you end the shell process, your container will be stopped immediatly. This is normal behaviour.

If you run through an interactive shell, you will see your container id is represented as the hostname in this shell. You can also see all your container id's by running docker ps -l once exited.

Port fowarding

You can port foward for a container using the -p command.

-p host_machine_port:container_port

Used in full context with the run command would look something like this.

docker run -i -t -p 5555:80 ubuntu bash

You can also forward to a specific interface too.

docker run -i -t -p ubuntu bash

Making changes and commiting

Following on from how we launched into an interactive terminal, we can make and save changes to this container.

Let's make a directory as a simple example inside your interactive container.

mkdir testdir

and then exit from your interactive terminal (which will stop the container).

If you run docker diff <container_id> you will notice that your directory addition has been made.

Commiting this change is a simple case of running docker commit <container_id> <new_image_name>. Your changes will be stored as a new image, and of course you'll be able to spin up a new container from your saved image. This could then be pushed up to the Docker hub for others to use.

Further reading

'Woah, Docker looks cool' is what you should be thinking right now. You'll probably be wondering how to setup a NAT network and how to build a Dockerfile. I recommend the following resources