我有Dockerfile
如下:
FROM docker/whalesay:latest
RUN apt-get update && apt-get install -y fortunes
CMD while true; do /usr/games/fortune -a | cowsay; sleep 2; done
我已将上述Dockerfile
构建为图片:docker-whale
我想为上面的图片写一个docker-compose.yml
。我的理解是你可以使用docker-compose运行多个容器。
因此,如果我要运行docker-whale
的5张图片,docker-compose.yml
的外观如何?
答案 0 :(得分:17)
您可以将此docker-compose.yaml
放在Dockerfile
旁边:
version: '2'
services:
docker-whale:
image: docker-whale
build: .
然后执行以下命令:
# build docker image
docker-compose build
# bring up one docker container
docker-compose up -d
# scale up to three containers
docker-compose scale docker-whale=3
# overview about these containers
docker-compose ps
# view combined logs of all containers
# use <ctrl-c> to stop viewing
docker-compose logs --follow
# take down all containers
docker-compose down
答案 1 :(得分:4)
version:"3"
services: docker-whale:
image:docker-whale
deploy:
replicas:5
resources:
limits:
cpus:"0.1"
memory: 50M
restart_policy:
condition: on-failure
ports: "80:80"
...
以上是你的docker-compose.yml的样子。这个docker-compose.yml告诉Dockers执行以下操作:
价:https://docs.docker.com/get-started/part3/#docker-composeyml
希望有所帮助
答案 2 :(得分:2)
我假设您要运行5个码头鲸鱼集装箱,而不是5个图像。
在你的情况下,compose.yml将没有关于容器数量的信息。您需要使用缩放命令
docker-compose scale <service-name>=5
答案 3 :(得分:-1)
<强>搬运工-compose.yml 强>
docker-whale:
image: docker-whale
然后在同一个文件夹中运行:
docker-compose up -d
docker-compose scale docker-whale=5
这假设您的应用程序具有可识别比例的功能,但您仍然可以在docker-compose.yml中编写5个条目,这是螺丝刀但有时很有用:
docker-whale-1:
image: docker-whale
docker-whale-2:
image: docker-whale
docker-whale-3:
image: docker-whale
docker-whale-4:
image: docker-whale
docker-whale-5:
image: docker-whale