如何 运行 一个 Docker 图片的多个实例?
How can I run multiple instances of a Docker image?
我想要多个 instances/replicas 同一张图片。
考虑已构建的映像 workerA
。
docker build --tag=workerA .
我通常运行它与
docker run --net=host -p 18080:8080 -t -i workerA
现在我想 运行 它的几个副本 docker container.I 查看了 swarm documentation 并创建了一个组合文件
version: "3"
services:
web:
# replace username/repo:tag with your name and image details
image: workerA:latest
deploy:
replicas: 2
resources:
limits:
cpus: "0.5"
memory: 4G
restart_policy:
condition: on-failure
ports:
- "18080:8080"
network_mode: "host"
通常工作人员会连接到主机服务器,获取数据并执行任务,但服务器从未收到任何请求。如何查看我的 workerA
终端输出的内容?我似乎可以创建副本
$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
y0sf30vqaoj1 warokerASwarm_web replicated 2/2 workerA:latest *:18080->8080/tcp
但它们似乎不在同一个网络中,也不像我在 docker 的单个实例 运行ning 时习惯的那样。
来自docs
The option network_mode is ignored when deploying a stack in swarm mode with a (version 3) Compose file.
如果您期望它的行为方式与 运行 单个容器时相同,则不会发生。
如果没有特定原因需要使用 version: '3'
,您可以使用 version:'2'
。
或者您可以让它创建自己的网络,它会处理您当前的 docker-compose 文件。
如果您想改用主机网络,请注意这些限制 (from docs):
In Docker 17.06 and higher, you can also use a host network for a swarm service, by passing --network host to the docker container create command. In this case, control traffic (traffic related to managing the swarm and the service) is still sent across an overlay network, but the individual swarm service containers send data using the Docker daemon’s host network and ports. This creates some extra limitations. For instance, if a service container binds to port 80, only one service container can run on a given swarm node.
我想要多个 instances/replicas 同一张图片。
考虑已构建的映像 workerA
。
docker build --tag=workerA .
我通常运行它与
docker run --net=host -p 18080:8080 -t -i workerA
现在我想 运行 它的几个副本 docker container.I 查看了 swarm documentation 并创建了一个组合文件
version: "3"
services:
web:
# replace username/repo:tag with your name and image details
image: workerA:latest
deploy:
replicas: 2
resources:
limits:
cpus: "0.5"
memory: 4G
restart_policy:
condition: on-failure
ports:
- "18080:8080"
network_mode: "host"
通常工作人员会连接到主机服务器,获取数据并执行任务,但服务器从未收到任何请求。如何查看我的 workerA
终端输出的内容?我似乎可以创建副本
$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
y0sf30vqaoj1 warokerASwarm_web replicated 2/2 workerA:latest *:18080->8080/tcp
但它们似乎不在同一个网络中,也不像我在 docker 的单个实例 运行ning 时习惯的那样。
来自docs
The option network_mode is ignored when deploying a stack in swarm mode with a (version 3) Compose file.
如果您期望它的行为方式与 运行 单个容器时相同,则不会发生。
如果没有特定原因需要使用 version: '3'
,您可以使用 version:'2'
。
或者您可以让它创建自己的网络,它会处理您当前的 docker-compose 文件。
如果您想改用主机网络,请注意这些限制 (from docs):
In Docker 17.06 and higher, you can also use a host network for a swarm service, by passing --network host to the docker container create command. In this case, control traffic (traffic related to managing the swarm and the service) is still sent across an overlay network, but the individual swarm service containers send data using the Docker daemon’s host network and ports. This creates some extra limitations. For instance, if a service container binds to port 80, only one service container can run on a given swarm node.