Taming Snakes inside a Container
Tags: python, docker
Published: 2018-08-06 05:34:42

---
title: Taming Snakes inside a Container
published: true
description: Comprehensive Taming Snakes inside a Container
tags: python, docker
---

In this post, let's talk about taming snakes inside a container. The article is a summary of lessons learned 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](https://blog.apcelent.com/setup-microservices-architecture-in-python-with-zeromq-docker.html).

![Alt text of image](https://blog.apcelent.com/images/python-docker-container-apcelent.png)

The prime reasons that we go with the 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 registries, and are ready to be used. This provides 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 the virtual environment since each container is unique in itself there is no additional need to create a virtual environment. Different 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
`

## Persistence

Changes are lost once a running container shuts down. To overcome the same the strategies deployed are

  * Data Volume to an 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 the usage of available tools is recommended.
  * Another Pythonic way of playing with Docker API is to use [docker-py](https://github.com/docker/docker-py).

## Deployment & Scaling

  * Load Balancing - [HA Proxy](http://www.haproxy.org/), [NGINX](https://www.nginx.com/)
  * Provisioning - [Ansible](https://www.ansible.com/), [Chef](https://www.chef.io/chef/), [Puppet](https://puppet.com/)
  * Monitoring - [Nagios](https://www.nagios.org/), [Pingdom](https://www.pingdom.com/), [New Relic](https://newrelic.com/)
  * Logging - [Fluentd](https://www.fluentd.org/guides/recipes/docker-logging) [Loggly](https://www.loggly.com/blog/top-5-docker-logging-methods-to-fit-your-container-deployment-strategy/)
  * Cloud Infrastructure - [Swarm](https://www.docker.com/products/docker-swarm), [Kubernetes](https://kubernetes.io/), [Magnum](https://docs.openstack.org/magnum/latest/), [CoreOS Fleet](https://github.com/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.

The article originally appeared on [Apcelent Tech Blog](https://blog.apcelent.com/taming-snakes-inside-container.html).