服务和容器是相关的,但是两者是不同的。
一项服务可以由一个或多个容器运行。随着
docker你可以处理的容器,并
docker-compose可以处理服务。
例如:
假设我们有这个
docker-compose.yml文件:
web: image: example/my_web_app:latest expose: - 80 links: - dbdb: image: postgres:latest
此撰写文件定义了两个服务,
web和
db。
在运行时
docker-compose up,假设项目目录已
test1组成,将启动2个名为
myapp_db_1和的容器
myapp_web_1。
$ docker ps -aConTAINER ID IMAGE COMMAND ... NAMES1c1683e871dc test1_web "nginx -g" ... test1_web_1a41360558f96 test1_db "postgres -d" ... test1_db_1
因此,在这一点上,您有2个服务和1个容器。
但是您可以扩展名为
web5个容器的服务。
$ docker-compose scale web=5Creating and starting 2 ... doneCreating and starting 3 ... doneCreating and starting 4 ... doneCreating and starting 5 ... done
在这一点上,您有2个服务和6个容器
$ docker ps -a ConTAINER ID IMAGE COMMAND ... NAMES1bf4c939263f test1_web "nginx -g" ... test1_web_3d3033964a44b test1_web "nginx -g" ... test1_web_4649bbda4d0b0 test1_web "nginx -g" ... test1_web_5a265ea406727 test1_web "nginx -g" ... test1_web_21c1683e871dc test1_web "nginx -g" ... test1_web_1a41360558f96 test1_db "postgres -d' ... test1_db_1
此外,借助docker-compose,您可以针对一个或多个服务运行子命令。
$docker-compose stop web



