感觉只有自己写个demo一行行看才明白啊

Compose 项目是 Docker 官方的开源项目,负责实现对 Docker 容器集群的快速编排。

Compose 项目由 Python 编写,实现上调用了 Docker 服务提供的 API 来对容器进行管理。因此,只要所操作的平台支持 Docker API,就可以在其上利用 Compose 来进行编排管理。

  • 服务 (service):一个应用容器,实际上可以运行多个相同镜像的实例。

  • 项目 (project):由一组关联的应用容器组成的一个完整业务单元。

可见,一个项目可以由多个服务(容器)关联而成,Compose 面向项目进行管理。

写一个web应用的话,后台用py写,调用flask轻量级web框架。使用redis作为数据库

redis正常控制台使用string 在自加的时候用的是incr 里面的参数也就是操作的string,如果之前没有这个变量就自动声明了

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 World! rejuct {} \n'.format(count)

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

写Dockerfile定制镜像

FROM :指定镜像

ADD:复制(应该是操作的虚拟的环境,所以在文件下看不到copy的文件)

WORKDIR :指定工作路径

RUN:执行命令行命令

CMD:容器启动命令

FROM python:3.5-alpine
ADD . /code
WORKDIR /code
RUN pip install redis flask
CMD ["python", "app.py"]

 docker-compose.yml 模板文件(YAML 格式)来定义一组相关联的应用容器为一个项目(project)。

version: '3'
services:

  web:
    build: .
    ports:
     - "5000:5000"

  redis:
    image: "redis:alpine"

执行docker-compose up

将尝试自动完成包括构建镜像,(重新)创建服务,启动服务,并关联服务相关容器的一系列操作。

链接的服务都将会被自动启动,除非已经处于运行状态。

可以说,大部分时候都可以直接通过该命令来启动一个项目。

默认情况,docker-compose up 启动的容器都在前台,控制台将会同时打印所有容器的输出信息,可以很方便进行调试。

当通过 Ctrl-C 停止命令时,所有容器将会停止。

如果使用 docker-compose up -d,将会在后台启动并运行所有的容器。一般推荐生产环境下使用该选项。

默认情况,如果服务容器已经存在,docker-compose up 将会尝试停止容器,然后重新创建(保持使用 volumes-from 挂载的卷),以保证新启动的服务匹配 docker-compose.yml 文件的最新内容。如果用户不希望容器被停止并重新创建,可以使用 docker-compose up --no-recreate。这样将只会启动处于停止状态的容器,而忽略已经运行的服务。如果用户只想重新部署某个服务,可以使用 docker-compose up --no-deps -d <SERVICE_NAME> 来重新创建服务并后台停止旧服务,启动新服务,并不会影响到其所依赖的服务。

选项:

  • -d 在后台运行服务容器。

  • --no-color 不使用颜色来区分不同的服务的控制台输出。

  • --no-deps 不启动服务所链接的容器。

  • --force-recreate 强制重新创建容器,不能与 --no-recreate 同时使用。

  • --no-recreate 如果容器已经存在了,则不重新创建,不能与 --force-recreate 同时使用。

  • --no-build 不自动构建缺失的服务镜像。

  • -t, --timeout TIMEOUT 停止容器时候的超时(默认为 10 秒)。

网页:

zyj@zyj:~/文档/web$ sudo docker-compose up
[sudo] zyj 的密码: 
Creating network "web_default" with the default driver
Building web
Step 1/5 : FROM python:3.5-alpine
3.5-alpine: Pulling from library/python
4fe2ade4980c: Pulling fs layer
4fe2ade4980c: Downloading [>                                                  ]  22.52kB/2.207MBlling fs layer
7cf6a1d62200: Downloading [=====>                                             ] 4fe2ade4980c: Downloading [=>                                                 ]   45.8kB/2.207MB
4fe2ade4980c: Downloading [==>                                                ]   93.9kB/2.207MB
4fe2ade4980c: Downloading [===>                                               ]  142.5kB/2.207MB
4fe2ade4980c: Downloading [====>                                              ]  192.2kB/2.207MB
4fe2ade4980c: Pull complete
7cf6a1d62200: Pull complete
df173b05a323: Pull complete
c71fddf9cbd2: Pull complete
34f27273daa3: Pull complete
Digest: sha256:9bdf2fc50c677655a077c973dba1655b68eacdb2b2e47008339371b8a8a591db
Status: Downloaded newer image for python:3.5-alpine
 ---> bd9fe274bae1
Step 2/5 : ADD . /code
 ---> 59a0d1787120
Step 3/5 : WORKDIR /code
 ---> Running in 74a65a0aad56
Removing intermediate container 74a65a0aad56
 ---> 133dee5a3509
Step 4/5 : RUN pip install redis flask
 ---> Running in 74d883a7ebc8
Collecting redis
  Downloading https://files.pythonhosted.org/packages/3b/f6/7a76333cf0b9251ecf49efff635015171843d9b977e4ffcf59f9c4428052/redis-2.10.6-py2.py3-none-any.whl (64kB)
Collecting flask
  Downloading https://files.pythonhosted.org/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl (91kB)
Collecting Jinja2>=2.10 (from flask)
  Downloading https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB)
Collecting click>=5.1 (from flask)
  Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
Collecting itsdangerous>=0.24 (from flask)
  Downloading https://files.pythonhosted.org/packages/c9/c3/8dadb353944803796515ce68ad3944e6e7acc934f5036c40829cb96e64a1/ItsDangerous-1.0.0-py2.py3-none-any.whl
Collecting Werkzeug>=0.14 (from flask)
  Downloading https://files.pythonhosted.org/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04fe335243/Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10->flask)
  Downloading https://files.pythonhosted.org/packages/4d/de/32d741db316d8fdb7680822dd37001ef7a448255de9699ab4bfcbdf4172b/MarkupSafe-1.0.tar.gz
Building wheels for collected packages: MarkupSafe
  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/33/56/20/ebe49a5c612fffe1c5a632146b16596f9e64676768661e4e46
Successfully built MarkupSafe
Installing collected packages: redis, MarkupSafe, Jinja2, click, itsdangerous, Werkzeug, flask
Successfully installed Jinja2-2.10 MarkupSafe-1.0 Werkzeug-0.14.1 click-7.0 flask-1.0.2 itsdangerous-1.0.0 redis-2.10.6
Removing intermediate container 74d883a7ebc8
 ---> 6b715533acb6
Step 5/5 : CMD ["python", "app.py"]
 ---> Running in c23ced308594
Removing intermediate container c23ced308594
 ---> e4722aa31d1a
Successfully built e4722aa31d1a
Successfully tagged web_web:latest
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Pulling redis (redis:alpine)...
alpine: Pulling from library/redis
4fe2ade4980c: Already exists
fb758dc2e038: Pull complete
989f7b0c858b: Pull complete
9d8bd3a94284: Pull complete
997d9e740b77: Pull complete
d76bc130a47a: Pull complete
Digest: sha256:6a9540142534c63da7aec6137b68ecc888d1505aecfc83755c579a97dc4330fe
Status: Downloaded newer image for redis:alpine
Creating web_redis_1 ... done
Creating web_web_1   ... done
Attaching to web_web_1, web_redis_1
web_1    |  * Serving Flask app "app" (lazy loading)
web_1    |  * Environment: production
web_1    |    WARNING: Do not use the development server in a production environment.
web_1    |    Use a production WSGI server instead.
web_1    |  * Debug mode: on
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: 244-056-528
redis_1  | 1:C 22 Oct 2018 06:58:22.019 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1  | 1:C 22 Oct 2018 06:58:22.019 # Redis version=5.0.0, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1  | 1:C 22 Oct 2018 06:58:22.019 # 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 22 Oct 2018 06:58:22.020 * Running mode=standalone, port=6379.
redis_1  | 1:M 22 Oct 2018 06:58:22.020 # 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 22 Oct 2018 06:58:22.020 # Server initialized
redis_1  | 1:M 22 Oct 2018 06:58:22.020 # 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 22 Oct 2018 06:58:22.020 # 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 22 Oct 2018 06:58:22.020 * Ready to accept connections