Tech Notes: Docker compose for asycio/aiohttp microservices

Thu 23 April 2015
By leon

docs for docker compose with python asyncio/aiohttp

I have been very interested in building a set of web services running on python asyncio and aiohttp as a way of exploring the microservices tech trend that seems to be happening right now. I'm also interested in the Docker containerisation trend that's also happening currently. So I thought it would be an interesting learning exercise to explore the possibilities both these technologies working together.

These notes are written for myself as a way of remembering where I am in this intermittent learning process

Lots of detailed documentation at

http://docs.docker.com/compose/

Docker compose runs on python 2.7. However the docker containers exist on docker hub to run new versions of python (I've tested only with python3.4). This presents interesting problems in organising a pycharm project that can combine python 2 and python 3 code.

key files

docker-compose.yml

defines the docker containerised services that will make up the application, where there files exist and the port connectivity between them when they are up and running.

Dockerfile

Each service need a Dockerfile give docker the info it needs to construct the container for the service

So for an aiohttp service. I have the following Dockerfile

FROM python:3
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt

This file is really short because it leverages the prebuilt python dockerfile on docker hub

https://registry.hub.docker.com/_/python/

Getting started

first activate the virtualenv that docker-compose has installed into

cd /path/to/virtualenv;source bin/activate

you need to run boot2docker and set the environment variables docker requires for your shell

boot2docker up

this will output something like....

To connect the Docker client to the Docker daemon, please set:
    export DOCKER_HOST=tcp://192.168.59.103:2376
    export DOCKER_CERT_PATH=/Users/leon/.boot2docker/certs/boot2docker-vm
    export DOCKER_TLS_VERIFY=1

Once you have set these environment variables. Get the docker deamon ip address with

boot2docker ip

Compose will start and run your entire app with.

docker-compose up

or alternatively to have services run in the background..

docker-compose up -d

you can then view what is running with

docker-compose ps

using nginx proxy container service for python services

If you have an nginx container set in your docker-compose.yml with something similar to...

nginx:
  image: nginx
  volumes:
  - ./nginx/html/:/usr/share/nginx/html:ro
  - ./nginx/conf/:/etc/nginx:ro
  links:
  - web
  - api
  ports:
   - "80:80"

Then nginx will proxy your web request on port 80 of the docker host ip (eg 192.168.59.103) and route the request to one of your services based on the rules set in your ngix.conf file

Getting more info and talking directly to each service

docker-compose run command allows you to run one-off commands in the context of one of your services. So in the context of the nginx service mentioned previously

docker-compose run nginx env

will yield lots of information and setting available to the nginx docker container service.

Proudly powered by Pelican, and Python. Reading Room Theme by Leon Harris