Last month we announced Docker, Inc.’s acquisition of Orchard, builders of a Docker hosting service as well as an orchestration tool, Fig. If you’ve started using Docker and have been wondering how to define and control a multi-container service – for example, a web app in one container and a database in another – then we think you’ll find Fig really helpful.
For this first example, we’ll show how to get a Python web app working with Redis, each in a separate container. We’ll show how Fig lets you define the link between the two containers and indicate that the web app container is dependent on the Redis container. Finally, we’ll show how, with a single command, Fig will …
- pull the Redis image from Docker Hub;
- build the Python app; and
- start the containers in the correct sequence.
Ready? Let’s go!
First, install Docker and Fig.
You’ll want to make a directory for the project:
$ mkdir figtest $ cd figtest
Inside this directory, create app.py, a simple web app that uses the Flask framework and increments a value in Redis:
from flask import Flask from redis import Redis import os app = Flask(__name__) redis = Redis(host="redis_1", port=6379) @app.route('/') def hello(): redis.incr('hits') return 'Hello World! I have been seen %s times.' % redis.get('hits') if __name__ == "__main__": app.run(host="0.0.0.0", debug=True)
We define our Python dependencies in a file called requirements.txt:
Next, we want to create a Docker image containing all of our app’s dependencies. We specify how to build one using a file called Dockerfile:
FROM orchardup/python:2.7 ADD . /code WORKDIR /code RUN pip install -r requirements.txt
This tells Docker to install Python, our code and our Python dependencies inside a Docker image. For more information on how to write Dockerfiles, see the Docker user guide and the Dockerfile reference.
We then define a set of services using fig.yml:
web: build: . command: python app.py ports: - "5000:5000" volumes: - .:/code links: - redis redis: image: orchardup/redis
This defines two services:
- web, which is built from Dockerfile in the current directory. It also says to run the command python app.py inside the image, forward the exposed port 5000 on the container to port 5000 on the host machine, connect up the Redis service, and mount the current directory inside the container so we can work on code without having to rebuild the image.
- redis, which uses the Docker Hub image orchardup/redis.
Now if we run fig up, it’ll pull a Redis image, build an image for our own code, and start everything up:
$ fig up Pulling image orchardup/redis... Building web... Starting figtest_redis_1... Starting figtest_web_1... redis_1 |  02 Jan 18:43:35.576 # Server started, Redis version 2.8.3 web_1 | * Running on http://0.0.0.0:5000/
If you want to run your services in the background, you can pass the -d flag to fig up and use fig ps to see what is currently running:
$ fig up -d Starting figtest_redis_1... Starting figtest_web_1... $ fig ps Name Command State Ports ----------------------------------------------------------------- figtest_redis_1 /usr/local/bin/run Up figtest_web_1 /bin/sh -c python app.py Up 5000->5000/tcp
fig run allows you to run one-off commands for your services. For example, to see what environment variables are available to the web service:
$ fig run web env
See fig –help other commands that are available.
If you started Fig with fig up -d, you’ll probably want to stop your services once you’ve finished with them:
$ fig stop
This obviously just touches the surface of what’s possible with Docker and Fig – check-out the reference links below for more details. And we’ll be sharing more Docker + Fig orchestration in future posts, so stay tuned.
Dockerize early and often!