Smuggling

Snakes in a Box

A Docker + Python love story

A little about Docker

Not your dad's pants

"Docker containers wrap a piece of software in a complete file-system that contains everything needed to run: code, runtime, system tools, system libraries – anything that can be installed on a server. This guarantees that the software will always run the same, regardless of its environment."

- Sailors @ the shipyard

Lingo for your Sailor mouth

Dockerfile -  Contains the instructions to build an image. This is where your code is moved around and compiled.

Image - Built from a Dockerfile; a snapshot of the compiled code.

Container  - An instance of the image. You can create many copies of your image, and these will be your containers.

Registry - A place to store your Docker image. Similar to a git repo, you can pull/push images to/from the registry.

Docker Compose - An external tool used to link many Docker containers together.

"Docker slang will make your popularity scale horizontally like a hockey-stick."

typical workflow

1. Dockerfile is created

2. Dockerfile is tagged and built/compiled into a Docker image

​3. Docker Container is/are built and used.

4. Something missing? Repeat. Everything good? Ship it!

So what does that mean? where's the lovey part?

we'll get there. love is in the air tonight

BUILD
RUN

SHIP

building

Start of with a Docker image to start from. Free images @ docker hub.

 

Install dependencies and code.

 

Start up code or service 

# Linux image -- a VERY LIGHTWEIGHT version of Linux
FROM python:3.5-alpine

# Requirements -- install Postgres
RUN apk update && \
    apk add gcc musl-dev postgresql-client libffi-dev ca-certificates
RUN pip install --upgrade pip

# Ensure that if `requirements.txt` changes, we update everything
COPY requirements.txt /tmp/requirements.txt
RUN apk add postgresql-dev && \
    pip install --no-binary :all: -r /tmp/requirements.txt && \
    apk del postgresql-dev

# These are the bootstrap scripts for the rest of the project. They:
#     1. Setup postgres and a postgres database
#     2. Copy the files to set up the application for production or testing.
#     3. Run the application
COPY files/pgpass /var/lib/postgresql/.pgpass
RUN chown postgres:postgres /var/lib/postgresql/.pgpass

COPY files/run_app.sh /etc/run_app.sh
#COPY files/run_tests.sh /etc/run_tests.sh

# Move the code
COPY . /app
WORKDIR /app

CMD ["/etc/run_app.sh"]

Dockerfile

# Linux image -- a VERY LIGHTWEIGHT version of Linux
FROM python:3.5-alpine

# Requirements
RUN apk update
RUN pip install --upgrade pip

COPY requirements.txt /tmp/requirements.txt
RUN pip install --no-binary :all: -r /tmp/requirements.txt

# Move the code
COPY . /app
WORKDIR /app

CMD ["python", "app.py"]

Building

# We are going to build the Docker container and
# name the image "web_app".

docker build -t web_app .

# NOTE: This assumes that the Dockerfile and the code
# are within the same directory.

Sonata by docker compose

# We are going to build the Docker container and
# name the image "web_app".

docker build -t web_app .

# NOTE: This assumes that the Dockerfile and the code
# are within the same directory.

A little on docker compose

You can hook up a bunch of Docker images together, making a happy shipyard filled with Docker containers.

How i met your python

SMUGGLING SNAKES IN A BOX:

By Hector Rios

SMUGGLING SNAKES IN A BOX:

  • 875