Hello Docker

In this article we will learn how to wrap simplest app into image.


Before going to our case study docker needs to be installed and running. You can follow instructions for your operating system on Docker website

App description

All our application is going to do is to serve static text. To do it we will use standard command nc. No dependencies. All we need is

 CMD while :; do { echo -ne "HTTP/1.0 200 OK\r\n\r\nBOOM: $NAME";  } | nc -lp 80; done

Introducing Dockerfile

To deal with your application Docker needs to gain knowledge about application. It's done via creating file named Dockerfile.

FROM gliderlabs/alpine:3.4


CMD while :; do { echo -ne "HTTP/1.0 200 OK\r\n\r\nBOOM: $NAME";  } | nc -lp 80; done
There are just three lines. With first one we specify the base image. In example provided we've selected linux alpine since it's the most light alternative. It will provide you support of very basic linux tools inside you wrapped app. You can build container on top of ubuntu or other distributions if you need more dedicated tools. In the second line we specify that particular port should be accessible from outside. And finally we tell what command to execute on application run.

Building image

When Dockerfile created we can build an image with docker build -t myworker . We've just build docker image from current directory and named it myworker.

Running app

To run application you need to execute docker run -d -p 9090:80 myworker. We specified that we want to run application with 80 port be available as 9090. Now we can hit localhost:9090 from our browser and see response.

Stopping app

We can run as many instances of the image as we want providing different ports. Each such instance named container. So, to stop application we need to know container id not image id. It's printed out when you run container, but if you missed it you can look it up. Just run docker ps -al and it will list you all containers.

Images manipulation

Unlike many other build systems that put artifacts into current directory Docker stores all local images in one place. You can see the list with docker images. To share your image you need to push it to docker registry. You can play around with a free repository