Docker is a great tool to test out new application architectures. To make sure that my architectures routed the right calls to the right places, I needed to have a simple containerised web service that just logged when it was called.

In this post, I am going to describe how to create a simple web service with Docker and Python, it is even small enough to fit in a tweet. This post will be brief.

Boot2Docker for OSX

If you are using OSX, then Boot2Docker is the tool for you. It works by using a virtual machine to host Docker and letting the OSX command line ‘remotely’ call it with the docker command.

To install Boot2Docker: brew install boot2docker

To initialise and start the virtual machine: boot2docker init boot2docker up

Finally, to link docker to the Docker host: export DOCKER_IP=boot2docker ip export DOCKER_HOST=boot2docker socket``

Call docker ps to test that the docker is correctly linked, this will return successfully if it is working. If the Docker host is not running, or the DOCKER_HOST environment variable is not set, the error will look something like: Get [http:///var/run/docker.sock/v1.12/containers/json:](http:///var/run/docker.sock/v1.12/containers/json:) dial unix /var/run/docker.sock: no such file or directory

Small Web Service

Lets build the service. Create a file called Dockerfile that contains: FROM python:3 EXPOSE 80 CMD ["python", "-m", "http.server"]

A a new image is defined from the python version 3 image. When the container is run it will execute python -m http.server, which starts a HTTP server on the exposed port 80.

Build the Dockerfile with: docker build -t python/server .

The image is built with the tag python/server and can be run with: docker run -i -t -p 8000:80 python/server

This maps the containers port 80 to the hosts port 8000 (-p 8000:80).

-i lets the container take STDIN and pipes it to the container. So if you Ctrl+C the container will be killed, making services easier to manage.

-t gives the container a pseudo-tty, that basically pipes the containers output to STDOUT so you can see when the server has been called.

Use curl $DOCKER_IP:8000 to call the service and see the container log your call.

Conclusion

I am having a blast mucking around with Docker.

Further Reading

The Docker Book

Vagrant: Up and Running