If I change the replica settings in the docker compose file to 1, then it’s basically identical to what you are looking for. You just need to change the nginx.conf accordingly.
docker-compose.yml (If not on docker swarm, then just use docker-compose command, which will ignore some of the swarm specific settings). Although the two applications are named shiny-1 and shiny-2, they are using the same docker image hence they are the same application and also they run on the same host if you use docker-compose.
version: "3.1"
services:
shiny-1:
image: keqiangli/shiny-swarm
ports:
- "3838"
deploy:
replicas: 1
restart_policy:
condition: on-failure
shiny-2:
image: keqiangli/shiny-swarm
ports:
- "3838"
deploy:
replicas: 1
restart_policy:
condition: on-failure
proxy-server:
image: keqiangli/reverseproxy
ports:
- "80:80"
deploy:
placement:
constraints: [node.role == manager]
replicas: 1
restart_policy:
condition: on-failure
visualizer:
image: dockersamples/visualizer:stable
ports:
- "8080:8080"
stop_grace_period: 1m30s
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
deploy:
placement:
constraints: [node.role == manager]
nginx.conf, I changed to use nginx to do the load balancing here and used ‘shiny-1’, ‘shiny-2’, the two service name as the server name here.
worker_processes 1;
events { worker_connections 1024; }
http {
sendfile on;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream myapp1 {
server shiny-1:3838;
server shiny-2:3838;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
proxy_redirect http://myapp1/ $scheme://$host/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 20d;
proxy_buffering off;
}
}
}
keqiangli/reverseproxy Dockerfile
FROM nginx:alpine
COPY nginx.conf /etc/nginx/nginx.conf
If all these settings are used to run the services on the same host, then it meets all you requirements, and even better since it will recover the shiny applications when fail, and can be easily adapted to a Swarm environment as well.