Fig1. the study of Fig
-Orchestration tool of docker-
July,30,2014
Tadayasu Yotsu
2. July,30,2014 Docker inc announced to aquire Orchard
Laboratories Ltd. This is assumed that 「Fig」which is
orchestration tool for docker will be standard tool.
Recently, there are some orchestration tools such as
kubernetes. I am interested in orchestration tool, so
I try to study it.
First of all, I will run fig by reading fig home
page(http://www.fig.sh/index.html).
3. Install
まずはdockerのversion1.0以降をインストール。
実行環境はAWS EC2、RHEL6.5
$ sudo yum -y install http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-
release-6-8.noarch.rpm
$ sudo yum -y install docker-io
$ docker -v
Docker version 1.0.0, build 63fe64c/1.0.0
$ sudo service docker start
次にFigをインストール。
$ curl -L https://github.com/orchardup/fig/releases/download/0.5.1/linux > fig
$ sudo mv fig /usr/local/bin/
$ sudo chmod +x /usr/local/bin/fig
$ fig --version
fig: /lib64/libc.so.6: version `GLIBC_2.14' not found (required by /tmp/
_MEIlZ8Qx8/libz.so.1)
glibcのバージョンをアップデートするのは面倒なので、python版をインストール
$ python -V
Python 2.7.8
$ sudo pip install -U fig
5. Get started with Django
Figのセットアップを行い、Django/PostgreSQLアプリの実行を行う。
始める前に3つのファイルの設定を行う。
まず最初にアプリケーションはアプリに実行に必要なものを含んだdockerコ
ンテナ内で実行する予定である。そのためdockerコンテナ内で何を実行する
のかDockerfileを使用して定義する。
FROM orchardup/python:2.7
ENV PYTHONUNBUFFERED 1
RUN apt-get update -qq && apt-get install -y python-psycopg2
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
これでimageの中にアプリケーションがインストールされる。
7. db:
image: orchardup/postgresql
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
links:
- db
最後にこれら全てをfig.ymlと呼ばれるファイルでくっつける。アプリケーショ
ンがWeb Serverとデータベースのサービスで構成されることを定義する。そし
てどのDockerイメージを使用し、どのようにリンクするか、コンテナ内でどの
volumeをマウントするか、そしてどのポートをexposeするかを定義する。
fig.ymlの詳細はhttp://www.fig.sh/yml.htmlを参照。
とりあえず作成するファイルはこれで全て。
$ pwd
/home/ec2-user/fig
$ ls
Dockerfile fig.yml requirements.txt
8. db:
image: orchardup/postgresql
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
links:
- db
最後にこれら全てをfig.ymlと呼ばれるファイルでくっつける。アプリケーショ
ンがWeb Serverとデータベースのサービスで構成されることを定義する。そし
てどのDockerイメージを使用し、どのようにリンクするか、コンテナ内でどの
volumeをマウントするか、そしてどのポートをexposeするかを定義する。
fig.ymlの詳細はhttp://www.fig.sh/yml.htmlを参照。
とりあえず作成するファイルはこれで全て。
$ pwd
/home/ec2-user/fig
$ ls
Dockerfile fig.yml requirements.txt
9. # fig run web django-admin.py startproject figexample .
Creating fig_db_1...
Pulling image orchardup/postgresql…
省略
Building web...
---> Using cache
---> 648d5cf80800
Step 2 : RUN apt-get update -qq && apt-get install -y python-psycopg2
---> Using cache
---> af5aedf45f81
Step 3 : RUN mkdir /code
---> Using cache
---> 93dd07412b09
Step 4 : WORKDIR /code
---> Using cache
---> 1497430bfa1d
Step 5 : ADD requirements.txt /code/
---> 2b8a2b0f1df7
Removing intermediate container 7c7d76a6bf7a
Step 6 : RUN pip install -r requirements.txt
---> Running in 1dfba113bb7f
Downloading/unpacking Django (from -r requirements.txt (line 1))
Installing collected packages: Django
Successfully installed Django
Cleaning up...
---> 2d2464646cf6
Removing intermediate container 1dfba113bb7f
Step 7 : ADD . /code/
---> a38ea44a1f49
Removing intermediate container 6c1d8cd2ac06
Successfully built a38ea44a1f49
# ls
Dockerfile fig.yml figexample manage.py requirements.txt
それではfig runを使ってDjango Projectを起動してみる。
Figは最初にDockerfileを使っ
てWeb Serviceのためのイメー
ジをビルドする。それからコンテ
ナの中で”django-admin.py
startproject figexample .”
コマンドを実行する。
実行後のカレントディレクトリに
Djangoアプリが生成される。
10. # docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
fig_web latest a38ea44a1f49 13 minutes ago 550.9 MB
orchardup/postgresql latest ca3d005ec1e8 5 hours ago 478 MB
orchardup/postgresql 9.1 f9686ed36bcb 5 hours ago 318 MB
orchardup/postgresql 9.3 8096d940fd75 4 days ago 477.9 MB
centos centos6 0c752394b855 7 weeks ago 124.1 MB
centos latest 0c752394b855 7 weeks ago 124.1 MB
orchardup/python 2.7 cdc18ed4ed6f 9 weeks ago 514.3 MB
centos 6.4 539c0211cd76 16 months ago 300.6 MB
fig_web,orchardup/postgresql x3/orchardup/
pythonのイメージが追加されている。
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
479c8630b668 orchardup/postgresql:latest /usr/local/bin/run 17 minutes ago Up 17
minutes 5432/tcp fig_db_1,fig_web_run_1/db_1,fig_web_run_1/fig_db_1
起動しているコンテナはorchardup/postgresqlのみ。
11. DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'docker',
'USER': 'docker',
'PASSWORD': 'docker',
'HOST': os.environ.get('DB_1_PORT_5432_TCP_ADDR'),
'PORT': os.environ.get('DB_1_PORT_5432_TCP_PORT'),
}
}
データベース接続のセットアップを行う。figexample/settings.pyファイ
ルの”DATABASES=…”を修正する。
これらの設定はorchardup/postgresql Dockerイメージによって
定義されている。
12. # fig up
Recreating fig_db_1...
Creating fig_web_1...
Attaching to fig_db_1, fig_web_1
db_1 | 2014-08-02 07:03:38 GMT ERROR: role "docker" already exists
db_1 | 2014-08-02 07:03:38 GMT STATEMENT: CREATE USER docker WITH SUPERUSER;
db_1 |
db_1 | 2014-08-02 07:03:39 GMT ERROR: database "docker" already exists
db_1 | 2014-08-02 07:03:39 GMT STATEMENT: CREATE DATABASE docker OWNER docker TEMPLATE DEFAULT;
db_1 |
db_1 | 2014-08-02 07:03:39 GMT LOG: database system was shut down at 2014-08-02 07:03:39 GMT
db_1 | 2014-08-02 07:03:39 GMT LOG: autovacuum launcher started
db_1 | 2014-08-02 07:03:39 GMT LOG: database system is ready to accept connections
web_1 | Validating models...
web_1 |
web_1 | 0 errors found
web_1 | August 02, 2014 - 07:03:40
web_1 | Django version 1.6.5, using settings 'figexample.settings'
web_1 | Starting development server at http://0.0.0.0:8000/
web_1 | Quit the server with CONTROL-C.
”fig up”を実行する。
ブラウザでlocalhost:8000にアクセスし、Djangoのページが表示されるか確認する。
※ちなみにEC2ではDefaultでX11fowardingが有効になっておらずライブラリも足り
てないのでインストールする必要がある。
$ sudo yum install xorg-x11-xauth.x86_64 xorg-x11-server-utils.x86_64 firefox
13. # fig run web python manage.py syncdb
Creating tables ...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Dockerに対して管理コマンドを実行。
fig upを実行してデータベースのセットアップを行う。
とりあえず、ここまでがドキュメントに記載されている内容。定義ファイルを書い
てfigコマンドを実行するだけで環境が作れるので便利だと思うが、1つのコンテ
ナだけだとあまりありがたみが分からないので、次回は複数のコンテナで試してみ
る。
15. Tag
image
タグもしく部分一致のイメージ
モートを指定可能。ローカルに存在しない場合、
Fig
image: ubuntu
image: orchardup/postgresql
image: a4bc65fd
build
Dockerfile
生成された名前を持ってビルドとタグ付けをし、そ
の後そのイメージを利用する。
build: /path/to/build/dir
command デフォルトのコマンドをオーバライドさせる。 command: bundle exec thin -p 3000
links
別のサービス中のコンテナへのリンク。リンクの別
名を指定できる。それはどの環境変数が
を定義する。
db -> DB_1_PORT
db:database -> DATABASE_1_PORT
links:
- db
- db:database
- redis
ports
Expose ports.
を指定するか、コンテナだけのポート
random host port will be chosen)
ports:
- "3000"
- "8000:8000"
- "49100:22"
- "127.0.0.1:8001:8001"
expose
Expose ports
い)
expose:
- "3000"
- "8000"