问题
I have to create containers in multiple host. I have a dockerfile for each container. I found that docker-compose can be used to run multiple containers from a single yaml file. I have to run containerA in HostA, containerB in HostB and so on.. is it possible to achieve this using docker-compose ? or what is the best way to create container in different host using the dockerfile.
回答1:
No, docker-compose alone won't achieve this. Managing containers across multiple hosts is generally the job of schedulers. The Docker Ecosystem: Scheduling and Orchestration is an older article, but should give an introduction.
The scheduler provided by Docker is called Swarm. Use Compose with Swarm is a good place to start to understand how to use them together.
This part of the answer is time limited, but during Mentor Week there are a few free courses you can take to learn more about Swarm. In your case Operations Beginner and Intermediate may be interesting.
回答2:
The answer is no.
- Docker compose is for multiple containers on a single host.
- Docker swarm is for container(s) at a cluster level.
And, you cannot decide where to run a container, only the docker swarm scheduler decides, but you can influence it. Check the link below, and focus on image affinity. i.e. you put specific images on only some nodes and configure the scheduler to use image affinity.
- Filters - Which hosts are chosen
- Strategies - ranking the nodes.
docker filters Here is a script to create a docker swarm cluster. Play with until you learn the process itself
id=$(docker run swarm create)
docker-machine create -d virtualbox --swarm --swarm-master --swarm-discovery token://$id swarm-master
docker-machine create -d virtualbox --swarm --swarm-discovery token://$id node1
docker-machine create -d virtualbox --swarm --swarm-discovery token://$id node2
docker-machine create -d virtualbox --swarm --swarm-discovery token://$id node3
docker-machine create -d virtualbox --swarm --swarm-discovery token://$id node4
Here is what happened:
- A cluster with one master and four nodes is created.
- run
eval $(docker-machine env --swarm swarm-master)
to configure your shell.
From this point on, creating and running containers is as usual. running docker ps would show you where the container is running.
回答3:
Docker-compose is exactly what you need. It is a tool for defining and running multi-container Docker applications. A good starting point is official documentation: https://docs.docker.com/compose/gettingstarted/
来源:https://stackoverflow.com/questions/40662254/is-it-possible-to-create-container-in-multiple-host-using-a-single-docker-compos