Scott Johnston

Getting Started with Docker Orchestration using Fig

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:

flask
redis

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 | [8] 02 Jan 18:43:35.576 # Server started, Redis version 2.8.3
web_1   |  * Running on http://0.0.0.0:5000/

Open up http://localhost:5000 in your browser (or http://localdocker:5000 if you’re using docker-osx) and you should see it running!

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!

Learn More

, , ,

Scott Johnston

Getting Started with Docker Orchestration using Fig


20 Responses to “Getting Started with Docker Orchestration using Fig”

  1. Lee

    Looking good, fig allow to have a simple yaml file to enable people to have an overview of the whole application stack with the needed components.

    Would be nice if the basic docker command would include fig.
    docker-stack up or something like that. Fig is nice but creates confusion for the beginner user.

    Reply
  2. the0ther

    would this be useful in any way in a production environment? the website only mentions dev environments.

    Reply
    • James Turnbull

      Also useful for production!

      Reply
      • Arthur Barr

        It seems to me that in production, you’d probably want the containers to be supervised in some way (e.g. with system, upstart or supervisord). It would be great to see Fig add some support for this, to allow for consistency from development through to production.

        Reply
      • Erik

        Since fig doesn’t handle automatically restarting crashed containers it’s probably a good idea to use something like supervisord/systemd/etc. in production right? If so, what value does fig provide in a production env?

        Reply
      • Andreas Steffan

        Absolutely! In fact, I skipped over it when I saw it first because of the emphasis on development. It might be missing features for production, but production use should really be mentioned.

        Reply
  3. Piotr Czekaj

    Does fig works on Windows + boot2docker? If it does how to install it? (when I try to run downloaded fig, I get command not found error, probably boot2docker is missing some shared library required by fig)

    Reply
    • Oded G

      I have the same issue did you solve it?

      Reply
    • Burhan Irmikci

      If you have Python installed on your computer, you can install fig by invoking the following command in the command prompt:

      %YOUR_PYTHON_PATH%\Scripts\pip install -U fig

      and then, you should be able to use fig in your working directory.

      set BOOT2DOCKER_IP=192.168.59.103
      set DOCKER_HOST=tcp://%BOOT2DOCKER_IP%:2375

      %YOUR_PYTHON_PATH%\Scripts\fig up

      Reply
  4. Manuel Astudillo

    Seems like a little useful tool. Any plans to make fig also work to describe multi-host apps?

    Reply
  5. Joel P

    Does anyone know of any tool like fig that allows you to specify different configurations? I want to be able to use the same containers, but in slightly different configurations (links/volumes/injected environment variables) for different setups.

    I don’t want a complex orchestration/deployment system right now, just something to declaratively specify these different configurations for local development.

    Reply
    • Walter

      You can use the “–file FILE” option to specify an alternate fig configuration file. This way you just need to add e.g. a “fig-production.yml” to your setup and use it with the mentioned option.

      Reply
  6. BrianF

    I am wondering where this will go compared to Century Link’s Panamax. It seems like they’ve implemented something like fig with a web UI as well. Correct me if I’m wrong. Probably we will just have options, but I am a fan of using tools straight from the docker core team. Just seems more reliable long term.

    Reply
  7. fmontmasson

    Hello,

    I am fond of docker of fig but I found linking in fig interfere with model of docker.

    The name of link specified in the fig.yml

    e.g.
    links:
    – redis

    do not correspond to the prefix used by corresponding environment variable in the container because the fig manager add the index of the service after the name of the container.

    In your example it is not the environment variable REDIS_PORT… which is injected in container but REDIS_1_PORT…

    For clarity, I would recommend to define distinctly name of the service from the container and simplify name of the container to remove useless prefix added by fig
    e.g.
    web_service:
    build: .
    command: python app.py
    ports:
    – “5000:5000”
    volumes:
    – .:/code
    links:
    – redis_service:redis
    redis_service:
    image: orchardup/redis

    and

    Reply
  8. Chris Cockrell

    How do the Fig commands that are the equivalent of the Docker commands (working_dir, entrypoint, user, hostname, domainname, mem_limit, privileged, etc)
    work? Do they replace the equivalent in the Dockerfile? Or should they be used only in one or the other?

    If the Fig.yml commands override the Dockerfile commands, does that imply that a service is not just dockerised, but is now a fig-based dockerised service?

    Reply
  9. Chris Cockrell

    I can see I missed a paragraph at the start of the fig.yml page, which says that the Dockerfile-equivalents are respected in preference to the ones specified in fig.yml.

    But then surely you should ALWAYS use the Dockerfile version of a command if the image may at any point be used without Fig? If you put the commands in fig.yml, but not in the Dockerfile, then the Dockerfile is incomplete and cannot be used on its own.

    And if that is the case, why put the equivalent commands into Fig at all? They are redundant and don’t do anything that Dockerfile versions don’t.

    Based on my current understanding (which may very well be incomplete), this all seems a little muddled.

    Reply
  10. Anxuy

    when run ‘fig up’,the log like this:
    Recreating yttl_cache_1…
    Recreating yttl_db_1…
    Recreating yttl_app_1…
    No such image: d647797b82d5 (tag: latest)
    Problem:I try to fig build my app again,build success,and then I try run fig up to run all containers,then ‘No such image’ out.How to fix this?

    Reply

Leave a Reply to Erik

Click here to cancel reply.

Get the Latest Docker News by Email

Docker Weekly is a newsletter with the latest content on Docker and the agenda for the upcoming weeks.