In this post lets talk about taming snakes inside a container. The article is a summary of lessons learnt while dockerizing python microservices. In case you want to see a detailed implementation of microservices with Docker and Python, kindly refer to this article which we have published.
The prime reasons that we go with container is that
- containers have lower memory footprints than VMs
- Containers provide an isolated user-space instance, instead of a machine emulation
It is easy to spin up new containers and set up environments. The images thus created can be shared and published to private / public registeries, and are ready to be used. This proviedes with a unified workflow whenever a new developer starts working on the codebase.
Building python images with a container
- We need not create an extra layer of virtual environment, sicne each container is unique in itself there is no additional need to create a virtual environment. Differenct Microservices can be put inside different containers.
- An external proxy like NGINX can be used or altogether a separate container for NGINX can be deployed.
Debugging Python Containers
- It is easy to run bash (or any other command ) on a running service
docker compose exec $SERVICE_NAME /bin/bash
- Log inspection of standard output and error
docker compose logs $SERVICENAME
- Low level information can be got through
docker inspect $SERVICENAME
Changes are lost once a running container shuts down. To overcome the same the strategies deployed are
- Data Volume to external host
- Another pattern is using separate Docker container to save the data
Testing & Tooling
- Docker adds consistency in your CI environments
- Control over what is in container implies repeatable workflow and simpler test environment
- Cloud based CI options with docker support out there.
- Docker Hub, can act as a CI server since you can configure it to create an automated build every time you push a new commit to Github.
- Internal API can vary radically for Docker from version to version hence usage of available tools is recommended
- Another Pythonic way of playing with Docker API is to use docker-py
Deployment & Scaling
- Load Balancing - HA Proxy, NGINX
- Provisioning - Ansible, Chef, Puppet
- Monitoring - Nagios, Pingdom, New Relic
- Logging - Fluentd Loggly
- Cloud Infrastructure - Swarm, Kubernetes, Magnum, CoreOS Fleet
Hope the summary and links were of help! Do let us know in comments what other stuff would you like us to write about.