How can I use docker-compose to deploy containers in separate hosts and servers?
Is it even possible?
Yes, with docker swarm mode. Read more: https://docs.docker.com/engine/swarm/
You can schedule your containers with labels in your compose:
version: "2"
services:
foo:
image: foo
volumes_from: ["bar"]
network_mode: "service:baz"
labels:
- "constraint:node==node-1" # Schedule containers on a specific node
bar:
image: bar
labels:
- "constraint:node==node-1"
baz:
image: baz
labels:
- "constraint:node==node-1"
Related
I am trying to create a swarm stack using a docker compose file. Is it possible to specify that my service is global, and should be run one instance on each node?
My current Docker compose file:
version: '3.1'
networks:
minecraft:
external: true
services:
bungee:
image: bungee
deploy:
replicas: 1
networks:
- minecraft
ports:
- 25565:25577
Yes, you need to remove the number of replicas and set the deploy mode to global (see below) :
services:
worker:
image: bungee
deploy:
mode: global
You can check the documentation
I have docker-compose.yml which is running a simple web server. I want to create multiple instances of the container without --scaling in the start command. This is how I currently start multiple instances of the container docker-composer up -d --scale appserver=2.
Ideally, I would love to put some kind of instruction in the docker-compose.yml to do this. Below is an example of the docker-compose.yml
version: '3'
services:
appserver:
image: nimmis/apache
haproxy:
image: eeacms/haproxy
ports:
- '80:5000'
- '1936:1936'
environment:
BACKENDS: 'appserver_1:80 appserver_2:80 appserver_3:80'
DNS_ENABLED: 'true'
LOG_LEVEL: info
Note here that I am only trying to multiple instances of appserver service.
Docker compose doesn't support the deploy section, but if you switch to a single node Swarm Mode (as easy as running docker swarm init) you can deploy with:
docker stack deploy -c docker-compose.yml stack_name
using the following yaml:
version: '3'
services:
appserver:
image: nimmis/apache
deploy:
replicas: 2
haproxy:
image: eeacms/haproxy
ports:
- '80:5000'
- '1936:1936'
environment:
BACKENDS: 'appserver_1:80 appserver_2:80 appserver_3:80'
DNS_ENABLED: 'true'
LOG_LEVEL: info
I suppose, it's a stupid question but I have no idea where to find the answer. I've checked so many resources but I still didn't get it.
I have docker-compose.yml file. Is it possible to use AWS ECS cluster to run a new instance (t2.micro, for example) for each service (eurekaserver, configserver, zuulserver, database)? I saw only examples with one big instance.
version: '2'
services:
eurekaserver:
image: maxb/tracker-eurekasvr:tracker-eurekasvr
ports:
- "8761:8761"
configserver:
image: maxb/tracker-confsvr:tracker-confsvr
ports:
- "8888:8888"
environment:
EUREKASERVER_URI: "http://eurekaserver:8761/eureka/"
EUREKASERVER_PORT: "8761"
ENCRYPT_KEY: "IMSYMMETRIC"
zuulserver:
image: maxb/tracker-zuulsvr:tracker-zuulsvr
ports:
- "5555:5555"
environment:
PROFILE: "default"
SERVER_PORT: "5555"
CONFIGSERVER_URI: "http://configserver:8888"
EUREKASERVER_URI: "http://eurekaserver:8761/eureka/"
DATABASESERVER_PORT: "27017"
EUREKASERVER_PORT: "8761"
CONFIGSERVER_PORT: "8888"
database:
image: mongo
container_name: tracker-mongo
volumes:
- $HOME/tracker-data:/data/db
- $HOME/tracker-datacd:/data/bkp
restart: always
AWS ECS has Tasks Definitions but I'm not sure if it can help
I am assuming you want to run these services 24x7 and not on demand. With container orchestration it is possible. One way of doing it with Rancher is as below:
Create 5 micro instances. 4 for the services and 1 for Rancher and put all 5 in 1 VPC. Now install Rancher in the 5th instance and add the other 4 hosts in Rancher, so that all your 4 hosts show up in Rancher's infrastructure.
Now label all the 4 hosts in Rancher uniquely - for ex: 'zuulserver' , 'database' , 'configserver' , 'eurekaserver'
Now edit your docker compose to add those rancher host labels to each of your services.
io.rancher.scheduler.affinity:host_label: key1=value1
wordpress:
labels:
# Make wordpress a global service
io.rancher.scheduler.global: 'true'
# Make wordpress only run containers on hosts with a key1=value1 label
io.rancher.scheduler.affinity:host_label: key1=value1
# Make wordpress only run on hosts that do not have a key2=value2 label
io.rancher.scheduler.affinity:host_label_ne: key2=value2
image: wordpress
links:
- db: mysql
stdin_open: true
In Rancher create a stack with your docker compose and start the stack.
Rancher will deploy those services to the corresponding hosts according to the host affinity labels.
https://rancher.com/docs/rancher/v1.1/en/cattle/scheduling
https://rancher.com/docs/rancher/v1.2/en/hosts/
I need to set service mode to global while using compose files .
Any chance we can use this in compose file ?
I have a requirement where for a service there should be exactly one container on every node/host .
This doesn't happen with "spread strategy" of swarm if a node goes down & comes up , it just attains the equal number of containers on each host irrespective of services .
https://github.com/docker/compose/issues/3743
We can do this easily now with docker compose v3 (version 3) under the deploy(mode) section.
Prerequisites -
docker compose version should be 1.10.0+
docker engine version should be 1.13.0+
Example compose file -
version: "3"
services:
nginx:
image: nexus3.example.com/prd-nginx-sm:v1
ports:
- "80:80"
networks:
- cheers
volumes:
- logs:/rest/out/
deploy:
mode: global
labels:
feature.description: "Frontend"
update_config:
parallelism: 1
delay: 10s
restart_policy:
condition: any
command: "/usr/sbin/nginx"
networks:
cheers:
volumes:
logs:
data:
Deploy the compose file -
$ docker stack deploy -c sm-deploy-compose.yml --with-registry-auth CHEERS
This will deploy nginx container on all the nodes participating in the cluster .
I'm using docker 1.12.1
I have an easy docker-compose script.
version: '2'
services:
jenkins-slave:
build: ./slave
image: jenkins-slave:1.0
restart: always
ports:
- "22"
environment:
- "constraint:NODE==master1"
jenkins-master:
image: jenkins:2.7.1
container_name: jenkins-master
restart: always
ports:
- "8080:8080"
- "50000"
environment:
- "constraint:NODE==node1"
I run this script with docker-compose -p jenkins up -d.
This Creates my 2 containers but only on my master (from where I execute my command). I would expect that one would be created on the master and one on the node.
I also tried to add
networks:
jenkins_swarm:
driver: overlay
and
networks:
- jenkins_swarm
After every service but this is failing with:
Cannot create container for service jenkins-master: network jenkins_jenkins_swarm not found
While the network is created when I perform docker network ls
Someone who can help me to deploy 2 containers on my 2 nodes with docker-compose. Swarm is defenitly working on my "cluster". I followed this tutorial to verify.
Compose doesn't support Swarm Mode at the moment.
When you run docker compose up on the master node, Compose issues docker run commands for the services in the Compose file, rather than docker service create - which is why the containers all run on the master. See this answer for options.
On the second point, networks are scoped in 1.12. If you inspect your network you'll find it's been created at swarm-level, but Compose is running engine-level containers which can't see the swarm network.
We can do this with docker compose v3 now.
https://docs.docker.com/engine/swarm/#feature-highlights
https://docs.docker.com/compose/compose-file/
You have to initialize the swarm cluster using command
$ docker swarm init
You can add more nodes as worker or manager -
https://docs.docker.com/engine/swarm/join-nodes/
Once you have your both nodes added to the cluster, pass your compose v3 i.e deployment file to create a stack. Compose file should just contain predefined images, you can't give a Dockerfile for deployment in Swarm mode.
$ docker stack deploy -c dev-compose-deploy.yml --with-registry-auth PL
View your stack services status -
$ docker stack services PL
Try to use Labels & Placement constraints to put services on different nodes.
Example "dev-compose-deploy.yml" file for your reference
version: "3"
services:
nginx:
image: nexus.example.com/pl/nginx-dev:latest
extra_hosts:
- "dev-pldocker-01:10.2.0.42”
- "int-pldocker-01:10.2.100.62”
- "prd-plwebassets-01:10.2.0.62”
ports:
- "80:8003"
- "443:443"
volumes:
- logs:/app/out/
networks:
- pl
deploy:
replicas: 3
labels:
feature.description: “Frontend”
update_config:
parallelism: 1
delay: 10s
restart_policy:
condition: any
placement:
constraints: [node.role == worker]
command: "/usr/sbin/nginx"
viz:
image: dockersamples/visualizer
ports:
- "8085:8080"
networks:
- pl
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
deploy:
replicas: 1
labels:
feature.description: "Visualizer"
restart_policy:
condition: any
placement:
constraints: [node.role == manager]
networks:
pl:
volumes:
logs: