将docker官网的compose实现了一下:
https://docs.docker.com/compose/
安装docker-compose
对于compose的安装,
sudo apt-get install python-pip -y
sudo pip install -U docker-compose
创建需要的各个脚本
app.py //用flask和redis创建简单的app运行文件
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 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
flask
redis
创建dockerfile来制作image
Dockerfile
FROM python:2.7
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
定义服务
docker-compose.yml
web:
build: .
command: python app.py
ports:
- "5050:5000"
volumes:
- .:/code
links:
- redis
redis:
image: redis
编译并运行服务
root@docker-registry:/home/ubuntu/composetest# docker-compose up
Creating composetest_redis_1...
Pulling image redis:latest...
e977d53b9210: Pull complete
c9fa20ecce88: Pull complete
d3b4674dd536: Pull complete
8615bfb42a84: Pull complete
d1fae67f7a3f: Pull complete
51b576738805: Pull complete
748a0c3bc585: Pull complete
b92fb9da44c7: Pull complete
e050e4489700: Pull complete
815faf24012e: Pull complete
1e75af93ef31: Pull complete
d52169358d63: Pull complete
e0ae4a4caac8: Pull complete
87083227c8ed: Pull complete
e1935d9fea6d: Pull complete
2d23d535ec5e: Pull complete
e03cdbcf651a: Pull complete
511136ea3c5a: Already exists
redis:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Status: Downloaded newer image for redis:latest
Creating composetest_web_1...
Building web...
Step 0 : FROM python:2.7
3b3a4796eef1: Pull complete
50ec2d202fe8: Pull complete
1073b544a1cb: Pull complete
6a7520a7fd8c: Pull complete
8d2c59617a00: Pull complete
cc18c784a952: Pull complete
474c0f2985f0: Pull complete
c6a6476d79dd: Pull complete
bbf61f5fa3d4: Pull complete
5b274b7410f2: Pull complete
d0b9d0c12bbf: Pull complete
6916a19e3366: Pull complete
511136ea3c5a: Already exists
python:2.7: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Status: Downloaded newer image for python:2.7
---> 6916a19e3366
Step 1 : ADD . /code
---> 7444153e8ef9
Removing intermediate container 7cd11c2f0bb9
Step 2 : WORKDIR /code
---> Running in 9051ef655aa6
---> 82be4b37ab80
Removing intermediate container 9051ef655aa6
Step 3 : RUN pip install -r requirements.txt
---> Running in b7c2bb52ae60
Collecting flask (from -r requirements.txt (line 1))
Downloading Flask-0.10.1.tar.gz (544kB)
Collecting redis (from -r requirements.txt (line 2))
Downloading redis-2.10.3.tar.gz (86kB)
Collecting Werkzeug>=0.7 (from flask->-r requirements.txt (line 1))
Downloading Werkzeug-0.10.4-py2.py3-none-any.whl (293kB)
Collecting Jinja2>=2.4 (from flask->-r requirements.txt (line 1))
Downloading Jinja2-2.7.3.tar.gz (378kB)
Collecting itsdangerous>=0.21 (from flask->-r requirements.txt (line 1))
Downloading itsdangerous-0.24.tar.gz (46kB)
Collecting markupsafe (from Jinja2>=2.4->flask->-r requirements.txt (line 1))
Downloading MarkupSafe-0.23.tar.gz
Installing collected packages: markupsafe, itsdangerous, Jinja2, Werkzeug, redis, flask
Running setup.py install for markupsafe
building 'markupsafe._speedups' extension
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/include/python2.7 -c markupsafe/_speedups.c -o build/temp.linux-x86_64-2.7/markupsafe/_speedups.o
gcc -pthread -shared build/temp.linux-x86_64-2.7/markupsafe/_speedups.o -L/usr/local/lib -lpython2.7 -o build/lib.linux-x86_64-2.7/markupsafe/_speedups.so
Running setup.py install for itsdangerous
Running setup.py install for Jinja2
Running setup.py install for redis
Running setup.py install for flask
Successfully installed Jinja2-2.7.3 Werkzeug-0.10.4 flask-0.10.1 itsdangerous-0.24 markupsafe-0.23 redis-2.10.3
---> c91f2d574c37
Removing intermediate container b7c2bb52ae60
Successfully built c91f2d574c37
Attaching to composetest_redis_1, composetest_web_1
redis_1 | [1] 27 Mar 04:43:48.190 # 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 2.8.19 (00000000/0) 64 bit
redis_1 | .-`` .-```. ```\/ _.,_ ''-._
redis_1 | ( ' , .-` | `, ) Running in stand alone 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] 27 Mar 04:43:48.192 # Server started, Redis version 2.8.19
redis_1 | [1] 27 Mar 04:43:48.192 # 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] 27 Mar 04:43:48.192 # 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] 27 Mar 04:43:48.192 # 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] 27 Mar 04:43:48.192 * 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 | 202.120.58.98 - - [27/Mar/2015 04:47:50] "GET / HTTP/1.1" 200 -
web_1 | 202.120.58.98 - - [27/Mar/2015 04:47:50] "GET /favicon.ico HTTP/1.1" 404 -
web_1 | 202.120.58.98 - - [27/Mar/2015 04:47:53] "GET / HTTP/1.1" 200 -
web_1 | 202.120.58.98 - - [27/Mar/2015 04:47:55] "GET / HTTP/1.1" 200 -
web_1 | 202.120.58.98 - - [27/Mar/2015 04:47:57] "GET / HTTP/1.1" 200 -
可以用docker ps来查看
root@docker-registry:/home/ubuntu# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da7561b2d02b composetest_web:latest "python app.py" 19 minutes ago Up 19 minutes 0.0.0.0:5050->5000/tcp composetest_web_1
08698509575f redis:latest "/entrypoint.sh redi 22 minutes ago Up 22 minutes 6379/tcp composetest_redis_1
也可以用docker-compose ps来查看
root@docker-registry:/home/ubuntu/composetest# docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------
composetest_redis_1 /entrypoint.sh redis-server Up 6379/tcp
composetest_web_1 python app.py Up 0.0.0.0:5050->5000/tcp
然后我们可以从该机器ip网页查看5050端口