從零開始學習docker(十三)Docker Compose--scale
阿新 • • 發佈:2019-07-05
環境準備,上一節中的flask-redis專案,將專案啟動:
docker-compose up -d
Starting flask-redis_web_1 ... done
Starting flask-redis_redis_1 ... done
檢視狀態:
docker-compose ps Name Command State Ports ------------------------------------------------------------------------------------- flask-redis_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp flask-redis_web_1 python app.py Up 0.0.0.0:8080->5000/tcp
docker-compose scale
我們目前的docker-compose 中的service容器中各自只有一個,通過scale可以去擴充套件service。
docker-compose up --help
--scale SERVICE=NUM Scale SERVICE to NUM instances. Overrides the
`scale` setting in the Compose file if present.
例如將web服務的數量變為三個:
docker-compose up --scale web=3 -d WARNING: The "web" service specifies a port on the host. If multiple containers for this service are created on a single host, the port will clash. Starting flask-redis_web_1 ... Starting flask-redis_web_1 ... done Creating flask-redis_web_2 ... error Creating flask-redis_web_3 ... error ERROR: for flask-redis_web_2 Cannot start service web: driver failed programming external connectivity on endpoint flask-redis_web_2 (952f3eea8bdf14f0a94845fb6ef5039285f7ffe40620faca32f40643b802fa97): Bind for 0.0.0.0:8080 failed: port is already allocated ERROR: for flask-redis_web_3 Cannot start service web: driver failed programming external connectivity on endpoint flask-redis_web_3 (50c776deb73b272d04181a8ab385f27cc31689aeeed804436fd2b92836cf25b9): Bind for 0.0.0.0:8080 failed: port is already allocated ERROR: for web Cannot start service web: driver failed programming external connectivity on endpoint flask-redis_web_2 (952f3eea8bdf14f0a94845fb6ef5039285f7ffe40620faca32f40643b802fa97): Bind for 0.0.0.0:8080 failed: port is already allocated ERROR: Encountered errors while bringing up the project.
發現報錯了,原因是介面已經被分配了。每個容器的埠都想繫結到8080上面,顯然是不可能的。
將容器刪除:
docker-compose down Stopping flask-redis_web_1 ... done Stopping flask-redis_redis_1 ... done Removing flask-redis_web_3 ... done Removing flask-redis_web_2 ... done Removing flask-redis_web_1 ... done Removing flask-redis_redis_1 ... done Removing network flask-redis_default
將docker-compose.yml中埠繫結刪除:
version: "3"
services:
redis:
image: redis
web:
build:
context: .
dockerfile: Dockerfile
environment:
REDIS_HOST: redis
執行命令docker-compose up --scale web=3 -d
docker-compose up --scale web=3 -d
Creating network "flask-redis_default" with the default driver
Creating flask-redis_web_1 ... done
Creating flask-redis_web_2 ... done
Creating flask-redis_web_3 ... done
Creating flask-redis_redis_1 ... done
可以成功執行。
docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------
flask-redis_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
flask-redis_web_1 python app.py Up 5000/tcp
flask-redis_web_2 python app.py Up 5000/tcp
flask-redis_web_3 python app.py Up 5000/tcp
發現web埠並沒有對映到主機的埠,全