Skip to content

Commit

Permalink
Merge pull request #3 from kakakakakku/v2.0.1
Browse files Browse the repository at this point in the history
Added v2.0.1
  • Loading branch information
kakakakakku committed Sep 2, 2018
2 parents 7de217d + b355a85 commit 884f425
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,57 @@ $ docker push ${DOCKER_HUB_ACCOUNT}/docker-hands-on-nginx
自分の Docker Hub を確認すると,以下のように正常にイメージが公開できています.

![](images/docker_hub.png)

## Docker Compose で複数コンテナを実行する

Docker Compose を使うと,複数コンテナを実行することができます.Docker Compose の設定ファイルは `dockerfiles/composetest/docker-compose.yml` です.以下のような構成になり,`services` 直下を見ると `web` コンテナと `redis` コンテナを実行していることがわかります.

```yaml
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
```

さっそく実行してみましょう.Docker Composer では `docker-compose up` コマンドを実行します.

```sh
$ docker-compose -f dockerfiles/composetest/docker-compose.yml up -d
Starting composetest_redis_1 ... done
Starting composetest_web_1 ... done
```

そして `http://0.0.0.0:5000/` に接続してみましょう.ブラウザをリロードすると,アクセス回数をカウントするアプリケーションを簡単に実行することができました.

![](images/composetest.png)

アクセス回数のデータはどこに保存されているのでしょう?実行中の `redis` コンテナに接続してみましょう.

まず `docker ps` コマンドで実行中のコンテナ一覧を確認します(`CONTAINER ID` は異なります).次に `docker exec` コマンドで `redis` コンテナに接続をします.コンテナ内部で Redis に接続し,保存されている `hits` キーの値を確認することができます.このように `docker exec` コマンドを使うとコンテナに接続することができます.

```sh
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1fc29424e6d0 composetest_web "python app.py" 14 minutes ago Up 14 minutes 0.0.0.0:5000->5000/tcp composetest_web_1
f20f5ec95f34 redis:alpine "docker-entrypoint.s…" 14 minutes ago Up 14 minutes 6379/tcp composetest_redis_1

$ docker exec -it f20f5ec95f34 /bin/sh

/data # redis-cli

127.0.0.1:6379> GET hits
"10"
127.0.0.1:6379> quit
```

動作確認後は Docker Composer を停止しておきましょう.

```sh
$ docker-compose -f dockerfiles/composetest/docker-compose.yml stop
Stopping composetest_web_1 ... done
Stopping composetest_redis_1 ... done
```
5 changes: 5 additions & 0 deletions dockerfiles/composetest/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM python:3.4-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
29 changes: 29 additions & 0 deletions dockerfiles/composetest/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import time

import redis
from flask import Flask


app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)


def get_hit_count():
retries = 5
while True:
try:
return cache.incr('hits')
except redis.exceptions.ConnectionError as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)


@app.route('/')
def hello():
count = get_hit_count()
return 'Hello World! I have been seen {} times.\n'.format(count)

if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
8 changes: 8 additions & 0 deletions dockerfiles/composetest/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
2 changes: 2 additions & 0 deletions dockerfiles/composetest/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
flask
redis
Binary file added images/composetest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 884f425

Please sign in to comment.