docker-compose demo1【網頁計數器】
感覺只有自己寫個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 秒)。
網頁:
[email protected]:~/文件/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