2014-01-27 12:42:38 +01:00
---
layout: default
2014-01-27 19:05:42 +01:00
title: Fig | Fast, isolated development environments using Docker
2014-01-27 12:42:38 +01:00
---
2014-01-27 16:03:21 +01:00
< strong class = "strapline" > Fast, isolated development environments using Docker.< / strong >
2014-01-27 12:42:38 +01:00
2014-10-10 12:06:49 +02:00
Define your app's environment with a `Dockerfile` so it can be reproduced anywhere:
2014-01-27 12:42:38 +01:00
2014-08-07 01:33:31 +02:00
FROM python:2.7
2014-01-27 16:03:21 +01:00
ADD . /code
WORKDIR /code
2014-01-28 01:07:59 +01:00
RUN pip install -r requirements.txt
2014-01-27 12:42:38 +01:00
2014-10-10 12:06:49 +02:00
Define the services that make up your app in `fig.yml` so they can be run together in an isolated environment:
2014-01-27 12:42:38 +01:00
```yaml
web:
build: .
2014-01-28 15:39:21 +01:00
command: python app.py
2014-01-27 12:42:38 +01:00
links:
- db
ports:
2014-02-20 13:53:43 +01:00
- "8000:8000"
2014-01-27 12:42:38 +01:00
db:
2014-08-07 01:33:31 +02:00
image: postgres
2014-01-27 12:42:38 +01:00
```
2014-01-27 19:15:52 +01:00
(No more installing Postgres on your laptop!)
2014-01-27 12:42:38 +01:00
Then type `fig up` , and Fig will start and run your entire app:
2014-05-06 18:13:56 +02:00
![example fig run ](https://orchardup.com/static/images/fig-example-large.gif )
2014-01-27 12:42:38 +01:00
There are commands to:
- start, stop and rebuild services
- view the status of running services
- tail running services' log output
- run a one-off command on a service
2014-01-27 16:03:21 +01:00
Quick start
-----------
2014-01-27 12:42:38 +01:00
Let's get a basic Python web app running on Fig. It assumes a little knowledge of Python, but the concepts should be clear if you're not familiar with it.
2014-03-25 13:26:33 +01:00
First, [install Docker and Fig ](install.html ).
2014-01-27 12:42:38 +01:00
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:
```python
from flask import Flask
from redis import Redis
import os
app = Flask(__name__)
2014-08-08 20:00:10 +02:00
redis = Redis(host='redis', port=6379)
2014-01-27 12:42:38 +01:00
@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` :
flask
redis
2014-01-28 11:32:55 +01:00
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` :
2014-01-27 12:42:38 +01:00
2014-08-07 23:50:55 +02:00
FROM python:2.7
2014-01-27 12:42:38 +01:00
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
2014-06-17 00:32:50 +02:00
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 ](https://docs.docker.com/userguide/dockerimages/#building-an-image-from-a-dockerfile ) and the [Dockerfile reference ](http://docs.docker.com/reference/builder/ ).
2014-01-27 12:42:38 +01:00
We then define a set of services using `fig.yml` :
web:
build: .
2014-01-28 11:32:55 +01:00
command: python app.py
2014-01-27 12:42:38 +01:00
ports:
2014-02-20 13:53:43 +01:00
- "5000:5000"
2014-01-27 12:42:38 +01:00
volumes:
- .:/code
links:
- redis
redis:
2014-08-07 23:50:55 +02:00
image: redis
2014-01-27 12:42:38 +01:00
This defines two services:
2014-01-28 11:32:55 +01:00
- `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.
2014-08-07 23:50:55 +02:00
- `redis` , which uses the public image [redis ](https://registry.hub.docker.com/_/redis/ ).
2014-01-27 12:42:38 +01:00
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
2014-08-07 23:50:55 +02:00
Pulling image redis...
2014-01-27 12:42:38 +01:00
Building web...
Starting figtest_redis_1...
Starting figtest_web_1...
2014-07-10 02:34:17 +02:00
redis_1 | [8] 02 Jan 18:43:35.576 # Server started, Redis version 2.8.3
web_1 | * Running on http://0.0.0.0:5000/
2014-01-27 12:42:38 +01:00
2014-10-06 15:21:20 +02:00
The web app should now be listening on port 5000 on your docker daemon (if you're using boot2docker, `boot2docker ip` will tell you its address).
2014-01-27 12:42:38 +01:00
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
2014-08-07 23:42:49 +02:00
That's more-or-less how Fig works. See the reference section below for full details on the commands, configuration file and environment variables. If you have any thoughts or suggestions, [open an issue on GitHub ](https://github.com/docker/fig ).