Getting started with Docker

I’ve been wanting to try out Docker for a while. Finally got my Ubuntu 14.04 server up and running, so now’s the time.

First off I installed docker as per the instruction on How to Install Docker on Ubuntu 14.04 LTS.

What version of Docker am I running?

Simple enough, just type docker version. Assuming the docker daemon is running you should see various bits of version information.

I’m currently running Client version 1.0.1

How to search for container images

We’re going to start by simply trying to find an existing images that we can pull onto our server, so typing docker search <name of image> will result in a list of images found with a match on the supplied image name.

For example docker search mongodb will return a list from the Docker hub of images with mongodbInstalling an image

Once we’ve found the image we want we need to “pull” it onto our machine using docker pull <name of image>

So let’s pull down the official mongodb image

docker pull mongo

This command will cause docker to download the current mongo image. Once completed you will not need to pull the image again it will be stored locally.

Hold on where are images stored locally?

Run docker info to see where the root directory for docker is, as well as information on the number of images and containers stored locally.

Runnning a container

Once we’ve pulled down an image we can run and application within the container, for example docker run <name of image> <command to run>

Let’s run our mongodb container by typing the following

  • docker run –name some-mongo -d mongo
  • docker run -it –link some-mongo:mongo –rm mongo sh -c ‘exec mongo “$MONGO_PORT_27017_TCP_ADDR:$MONGO_PORT_27017_TCP_PORT”‘

In the above some-mongo should be replaced with the name you want to use.

Note: These command lines will run mongodb in interactive mode, i.e. we will be placed into the mongodb shell.

Updating the container

An image itself may be based upon an OS, such as Ubuntu. So we can actually run commands on the operating system in the container, for example apt-get. Installing software into an OS container is cool but we will then want to persist such changes to the container.

Whilst the changes have been made, they are not yet persisted to the containter. To persist our changes we need to commit them.

First we need to find the ID of the container, for example running docker ps -l, then we save the changes using docker commit <ID> <new container name>.

You needn’t type the whole ID from the call to docker ps -l, the first three or four characters (assuming they’re unique) will suffice.

The value returned from the commit is the new image id.

Inspect

We can view more extensive information on a container by using docker inspect <ID>. Remember the ID can be found using docker ps.

Inspect allows is to see all sorts of information including the image’s IP address.

Pushing an image to the Docker Hub Registry

Once we’re happy with our image we can push it to the docker hub to allow others to share it. Simply use docker push <name of image>.

To find the images on your machine simply use docker images.

Removing all containers and all images

We can use docker’s ps command along with the docker rm command

docker rm $(docker ps -a -q)

to remove all containers and we can use the docker rmi command to remove images as follows

docker rmi $(docker images -q)