compose做python容器和redis容器

mac2024-05-16  31

编写一个python web案例,应用compose编排服务。例子包含2个容器,一个python web容器,一个是Redis数据库容器。 首先构建python web镜像和容器: (1). 创建composeapp目录

[root@client /]# mkdir composeapp [root@client /]# cd composeapp/ [root@client composeapp]# touch Dockerfile

(2). 编写python程序

[root@client composeapp]# touch app.py [root@client composeapp]# vi app.py 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 Docker Book reader! I have been seen {0} times'.format(redis.get('hits')) if __name__ == "__main__": app.run(host="0.0.0.0", debug=True) (3). 创建python的依赖包描述文件 ~ [root@client composeapp]# touch requirements.txt [root@client composeapp]# vi requirements.txt flask redis

(4). 编写python web容器的镜像Dockerfile文件

[root@client composeapp]# vi Dockerfile FROM python:2.7 MAINTAINER James Turnbull <james@example.com> ENV REFRESHED_AT 2016-08-01 ADD . /composeapp WORKDIR /composeapp RUN pip install -r requirements.txt

(5). 构建镜像

[root@client composeapp]# sudo docker build -t jamtur01/composeapp . [root@client composeapp]# sudo docker build -t jamtur01/composeapp . Sending build context to Docker daemon 4.096 kB Step 1 : FROM python:2.7 Trying to pull repository 192.168.200.11:5000/python ... Pulling repository 192.168.200.11:5000/python Error: image python not found Trying to pull repository docker.io/library/python ... 2.7: Pulling from docker.io/library/python 75a822cd7888: Pull complete 57de64c72267: Pull complete 4306be1e8943: Pull complete 871436ab7225: Pull complete 37c937b0ca47: Pull complete 608a51124afe: Pull complete 086c59e7b25f: Pull complete Digest: sha256:43b6a6df624798dd8889bde2c02a48a263faa1d72f0de9e134de84c4079ac365 Status: Downloaded newer image for docker.io/python:2.7 ---> acf0d719f268 Step 2 : MAINTAINER James Turnbull <james@example.com> ---> Running in 19e30769bcd3 ---> 6efea2b07fec Removing intermediate container 19e30769bcd3 Step 3 : ENV REFRESHED_AT 2016-08-01 ---> Running in 1c55d12d766b ---> fdf17727b383 Removing intermediate container 1c55d12d766b Step 4 : ADD . /composeapp ---> 96618268b61c Removing intermediate container cd7b0ed6e203 Step 5 : WORKDIR /composeapp ---> Running in 5f04f2e5adca ---> c504ecdffde0 Removing intermediate container 5f04f2e5adca Step 6 : RUN pip install -r requirements.txt ---> Running in 7433546dcc5c Collecting flask (from -r requirements.txt (line 1)) Downloading Flask-0.12-py2.py3-none-any.whl (82kB) Collecting redis (from -r requirements.txt (line 2)) Downloading redis-2.10.5-py2.py3-none-any.whl (60kB) Collecting itsdangerous>=0.21 (from flask->-r requirements.txt (line 1)) Downloading itsdangerous-0.24.tar.gz (46kB) Collecting Jinja2>=2.4 (from flask->-r requirements.txt (line 1)) Downloading Jinja2-2.8-py2.py3-none-any.whl (263kB) Collecting Werkzeug>=0.7 (from flask->-r requirements.txt (line 1)) Downloading Werkzeug-0.11.13-py2.py3-none-any.whl (308kB) Collecting click>=2.0 (from flask->-r requirements.txt (line 1)) Downloading click-6.6-py2.py3-none-any.whl (71kB) Collecting MarkupSafe (from Jinja2>=2.4->flask->-r requirements.txt (line 1)) Downloading MarkupSafe-0.23.tar.gz Building wheels for collected packages: itsdangerous, MarkupSafe Running setup.py bdist_wheel for itsdangerous: started Running setup.py bdist_wheel for itsdangerous: finished with status 'done' Stored in directory: /root/.cache/pip/wheels/fc/a8/66/24d655233c757e178d45dea2de22a04c6d92766abfb741129a Running setup.py bdist_wheel for MarkupSafe: started Running setup.py bdist_wheel for MarkupSafe: finished with status 'done' Stored in directory: /root/.cache/pip/wheels/a3/fa/dc/0198eed9ad95489b8a4f45d14dd5d2aee3f8984e46862c5748 Successfully built itsdangerous MarkupSafe Installing collected packages: itsdangerous, MarkupSafe, Jinja2, Werkzeug, click, flask, redis Successfully installed Jinja2-2.8 MarkupSafe-0.23 Werkzeug-0.11.13 click-6.6 flask-0.12 itsdangerous-0.24 redis-2.10.5 ---> e031fec32547 Removing intermediate container 7433546dcc5c Successfully built e031fec32547

(6). 以docker run运行案例:

[root@client composeapp]# sudo docker run -d -p 5000:5000 -v .:/composeapp --link redis:redis --name jamtur01/composeapp python app.py Unable to find image 'python:latest' locally Trying to pull repository 192.168.200.11:5000/python ... Pulling repository 192.168.200.11:5000/python Error: image python not found Trying to pull repository docker.io/library/python ... latest: Pulling from docker.io/library/python 75a822cd7888: Already exists 57de64c72267: Already exists 4306be1e8943: Already exists 871436ab7225: Already exists 37c937b0ca47: Already exists 2172fb7edb3a: Pull complete dcec4e4061ab: Pull complete Digest: sha256:1fa4d0110c7093fdb1306b4b290824a37169ca6a19f091b7227967647726169a Status: Downloaded newer image for docker.io/python:latest docker: Error response from daemon: Invalid container name (jamtur01/composeapp), only [a-zA-Z0-9][a-zA-Z0-9_.-] are allowed. See '/usr/bin/docker-current run --help'.

(7). 以docker-compose运行案例: 下面构建Docker Compose yml文件:docker-compose.yml文件:

[root@server composeapp]# vi docker-compose.yml web: image: jamtur01/composeapp command: python app.py ports: - "5000:5000" volumes: - .:/composeapp links: - redis redis: image: redis 切换到composeapp目录下,以compose的方式运行 [root@server composeapp]# [root@server composeapp]# sudo docker-compose up Pulling redis (redis:latest)... Trying to pull repository 192.168.200.18:5000/redis ... Pulling repository 192.168.200.18:5000/redis Error: image redis not found Trying to pull repository docker.io/library/redis ... latest: Pulling from docker.io/library/redis 75a822cd7888: Already exists e40c2fafe648: Pull complete ce384d4aea4f: Pull complete 5e29dd684b84: Pull complete 29a3c975c335: Pull complete a405554540f9: Pull complete 4b2454731fda: Pull complete Digest: sha256:be03cb3b2e0d290c3d6670d1843b6036c1914663574bb624ffb62535344de5b4 Status: Downloaded newer image for docker.io/redis:latest Creating composeapp_redis_1 Creating composeapp_web_1 Attaching to composeapp_redis_1, composeapp_web_1 redis_1 | 1:C 01 Jan 12:35:45.005 # 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 | _._ redis_1 | _.-``__ ''-._ redis_1 | _.-`` `. `_. ''-._ Redis 3.2.6 (00000000/0) 64 bit redis_1 | .-`` .-```. ```\/ _.,_ ''-._ redis_1 | ( ' , .-` | `, ) Running in standalone mode redis_1 | |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 redis_1 | | `-._ `._ / _.-' | PID: 1 redis_1 | `-._ `-._ `-./ _.-' _.-' redis_1 | |`-._`-._ `-.__.-' _.-'_.-'| redis_1 | | `-._`-._ _.-'_.-' | http://redis.io redis_1 | `-._ `-._`-.__.-'_.-' _.-' redis_1 | |`-._`-._ `-.__.-' _.-'_.-'| redis_1 | | `-._`-._ _.-'_.-' | redis_1 | `-._ `-._`-.__.-'_.-' _.-' redis_1 | `-._ `-.__.-' _.-' redis_1 | `-._ _.-' redis_1 | `-.__.-' redis_1 | redis_1 | 1:M 01 Jan 12:35:45.008 # 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 01 Jan 12:35:45.008 # Server started, Redis version 3.2.6 redis_1 | 1:M 01 Jan 12:35:45.008 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. redis_1 | 1:M 01 Jan 12:35:45.008 # 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 01 Jan 12:35:45.008 * 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 code: 457-652-386 web_1 | 192.168.0.51 - - [01/Jan/2017 12:37:42] "GET / HTTP/1.1" 200 - web_1 | 192.168.0.51 - - [01/Jan/2017 12:37:42] "GET /favicon.ico HTTP/1.1" 404 -

最后在网页上验证效果如下:

最新回复(0)