Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

About Docker's fig


May 22, 2021 Docker From entry to practice


Table of contents


Quickly build a Docker-based isolated development environment

Use Dockerfile file to specify your app environment so that it can be replicated anywhere:

FROM python:2.7
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt

Specify the different services used by your app in the fig.yml file, allowing them to run together in a separate environment:

web:
  build: .
  command: python app.py
  links:
   - db
  ports:
   - "8000:8000"
db:
  image: postgres

Note that postgress no longer needs to be installed!

Then execute the fig up and then Fig will start and run your app.

About Docker's fig

The commands available for Fig are:

  • Start, stop, and rebuild services
  • View the running status of the service
  • View the input log for the running service
  • Send a command to the service

Get started quickly

Let's try to get a basic Python web app running on Fig. This experiment assumes that you already know something about Python, and if you're not familiar with it, it's not a problem to know the conceptual stuff.

First, install Docker and Fig

Create a directory for your project

$ mkdir figtest
$ cd figtest

Go to the app.py a simple web app that allows a self-added value on Redis, based on the Flask framework.

from flask import Flask
from redis import Redis
import os
app = Flask(__name__)
redis = Redis(host='redis', 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)

Specify the Python .txt the application in the requirements.txt file.

flask
redis

Next we're going to create a Docker image that contains all the dependencies of the app, and here's Dockerfile file.

FROM python:2.7
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt

The above first tells Docker to install Python in the container, and the path of the code also has the Python dependency package. For more information about Dockerfile, you can see Mirror Creation and Dockerfile for use

We then specify a fig.yml file:

web:
  build: .
  command: python app.py
  ports:
   - "5000:5000"
  volumes:
   - .:/code
  links:
   - redis
redis:
  image: redis

Two services are specified here:

  • Web service, created from Dockerfile directory. It also explains how to python app.py inside the container, forward 5000 ports open in the container to the local host, connect to the Redis service, and mount the current directory into the container so that we can use the code directly without rebuilding the image.
  • redis service, we use the public mirror redis.
  • Now if you fig up command, it pulls the redis image and starts all the services.
$ fig up
Pulling image 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/

This web app has already started listening to 5000 ports in your docker daemon (if you're using boot2docker, boot2docker ip you'll see its address).

If you want to run your service in the background, you can add -d parameter when you execute the fig up command, and fig ps what processes are 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 can help you send commands to the service. For example, look at the environment variables that the web service can get:

$ fig run web env

Execute the help command fig --help the other available parameters.

Assuming you start fig up -d you can stop your service with the following command:

$ fig stop

The above more or less describes how to use Fig. Y ou can learn more about commands, configurations, and environment variables by looking at the reference section below. If you have any ideas or suggestions, you can do so on GitHub.