Monday, May 23, 2016

Running an applicication in docker

In the last blog post I wrote about how to run a docker container, in this blog post I explore running applications.

Running an application
Running an application inside a container is done with docker run. For example
docker run ubuntu /bin/echo "hello world"
outputs hello world

In the command above we tell docker to run the image ubuntu and to run in the container /bin/echo and pass it as parameter "hello world". When the application is executed the container is shutdown.

What is the point?
Okay, I admit hello world isn't the world best example but the point is that we ran /bin/echo in the container and passed it on the parameter "hello world". It executed and once executed it exited the container.

As we saw in the previous post docker run -i -t ubuntu /bin/bash does basically the same thing. It runs /bin/bash within the container and it is only because /bin/bash isn't finished executing that the container doesn't stop.

Cleaning up
Once your container has exited you can actually work with it again.

docker ps

Will show you all running containers and

docker ps -a

Will show you all running and exited containers.

If you have a container that isn't of any use to you any more you can just use

docker rm

To remove the container from the system.

Once you are getting the hang of it you will have a number of container with the status exited. To clean up my containers I use

docker ps -aq -f=exited | xargs docker rm



Monday, May 9, 2016

Using docker for the first time ...

In my previous post I described how to install docker, in this post we are going to download a pre-built image and run it.

To check if the docker installation is working you can run
docker info

Downloading the Ubuntu Image
We start by downloading an Ubuntu Image to play with. Ubuntu posts its image to the docker repository of images called the docker hub. To pull the image from the hub you do
docker pull ubuntu

You will see that the image is getting downloaded to your local image cache and the hashes are shown and the message "pull complete".  The hash is called the image ID and are the first 12 characters of the full name of the image. If you reissue the pull, you will get the information that the the image already exists.

The Image Cache
To get an overview of your image cache you do
docker images

This will show you the name of the images, their ID, size and when they got created. To get more information about the image you can do docker inspect followed by the IMAGE ID.

Starting the Container
To start your first container you do docker run -i -t ubuntu /bin/bash. This will open a prompt in your container. The -i flag tells docker it is an interactive container. The -t creates a pseudo-TTY and attaches it to stdin and stdout.

To exit the container you need to enter the sequence ctrl+p followed by ctrl+q. To have an overview of what is running you have docker ps -a.

To reattach the image back to the TTY you do docker attach followed by the IMAGE ID.

Quitting the Container
When you are done with the container you simply type exit and it closes your session. If you run a docker ps -a after you have exited your container you will see it described as "exited x minutes ago".

Installing Software
We will come back on how to prepare the container but I just already want to point out that if you start a container, install software and then exit the container the installed software is no longer in the container since every time you run docker run -i -t ubuntu /bin/bash a new container is started.

The same logic also applies to data, so if you want to use a container for running something and need a configuration, you got to make sure the configuration is stored outside of your container.

What Happens When You Run a Container?
When you type docker run it tells the daemon you want it to run a container. The parameters we specified are the image it needs to run and what to start if the image is running. In our case above we specified the Ubuntu image and to run /bin/bash.

  1. If the image is not available on the system it will be pulled from the Docker Hub 
  2. Once the image exists on the system docker will create a container.
  3. The container is created with a read-write layer on top.
  4. The network interface is set up to communicate with the localhost.
  5. The IP address is set up.
  6. The requested process is executed.