Skip to content

Instantly share code, notes, and snippets.

@YutaGoto
Created May 21, 2017 09:54
Show Gist options
  • Save YutaGoto/e76424c77b1c4091683811e92e797c7f to your computer and use it in GitHub Desktop.
Save YutaGoto/e76424c77b1c4091683811e92e797c7f to your computer and use it in GitHub Desktop.

Docker Compose で Python を動かすの巻

どうもこんにちは、ごっちです。

Djangoの勉強も進めているのですが、その前にシンプルなPythonでも動かしたいなと思って、この記事を投稿。

今回はDockerの公式にならって進めます。

ファイルの準備

  • Dockerfile
  • requirements.txt
  • docker-compose.yml
  • app.py

app.py

from flask import Flask
from redis import Redis

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

@app.route('/')

def hello():
	count = redis.incr('hits')
	return 'Hello from Docker! I have been seen {} times.\n'.format(count)

if __name__ == '__main__':
	app.run(host='0.0.0.0', debug=True)

requirements.txt

flask
redis

Dockerfile

FROM python:3.6.1-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "app.py"]

docker-compose.yml

version: '2'
services:
  web:
    build: .
    ports:
     - "5000:5000"
    volumes:
     - .:/code
  redis:
    image: "redis:alpine"

サーバーの起動

$ docker-compose up
Creating network "pythondocker_default" with the default driver
Building web
Step 1/5 : FROM python:3.6.1-alpine
3.6.1-alpine: Pulling from library/python
79650cf9cc01: Pull complete
581a2604819e: Pull complete
ada98fe44e3a: Pull complete
70b4de28adfd: Pull complete
bcd83fe4359e: Pull complete
...
redis_1  | 1:C 21 May 08:05:21.279 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
...
redis_1  | 1:M 21 May 08:05:21.283 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1  | 1:M 21 May 08:05:21.283 # Server started, Redis version 3.2.9
redis_1  | 1:M 21 May 08:05:21.283 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis_1  | 1:M 21 May 08:05:21.283 * The server is now ready to accept connections on port 6379
web_1    |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
web_1    |  * Restarting with stat
web_1    |  * Debugger is active!
web_1    |  * Debugger PIN: 132-463-211

ここで http://localhost:5000 にアクセスすると、、、

python_web

まぁ至ってシンプルなテキストが表示されるかと。。。

そもそも

Docker Compose ってWebサーバーと化さなくても動くもんだよね、たしか。。(勉強しておきます。)

Djangoのチュートリアルが一通り終わったらそっちのほうに手を出してみよ。

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment