3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Docker Compose Getting Startedをやってみる

Last updated at Posted at 2016-07-28

複数コンテナを簡単に管理するためのツール
Docker Composeを触ってみたときのメモです。

環境

Mac OSX

事前準備

インストール

# Homebrew Cask をインストールする
$ brew install caskroom/cask/brew-cask

# VirtualBox をインストールする
$ brew cask install virtualbox

# Docker, Docker Machine, Docker Compose をインストールする
$ brew install docker docker-machine docker-compose

Dockerホストを作成

作成

$ docker-machine create -d virtualbox default

確認

$ docker-machine ls
NAME        ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
default     -        virtualbox   Running   tcp://192.168.99.100:2376           v1.11.2

環境変数確認

$ docker-machine env default
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/a12499/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell:
# eval $(docker-machine env default)

環境変数設定

$ eval $(docker-machine env default)

セットアップ

プロジェクト用ディレクトリ作成

$ mkdir composetest
$ cd composetest

テスト用プログラム作成

app.py
from flask import Flask
from redis import Redis

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)

テスト用アプリの依存関係を定義したファイル作成

requirements.txt
flask
redis

Docker imageを作成する

Dockerfileを作成する

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

ビルド

$ docker build -t web .

サービス定義

docker-compose.yml作成

docker-compose.yml
version: '2'
services:
  web:
    build: .
    ports:
     - "5000:5000"
    volumes:
     - .:/code
    depends_on:
     - redis
  redis:
    image: redis

ビルド&Composeでテストアプリ起動

$ docker-compose up

確認

IPアドレス取得

$ docker-machine ip default
192.168.99.100

http://192.168.99.100:5000/
にブラウザでアクセス

無事にアクセス回数を表示するページを表示できました。

スクリーンショット 2016-07-28 19.12.15.png

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?