Let's say you have a long docker-compose file with a lot of containers that speak to one another inside of a docker network. Let's call this a "stack". You want to launch this stack 3 times, each with a slightly different config. To do that you might say:
docker-compose -p pizza up
docker-compose -p pie up
docker-compose -p soda up
But this would fail if you have any ports exposed to the host:
nginx:
image: nginx:alpine
ports:
- "80:80"
networks:
- my_app_net
It would fail, because the host can only expose one thing on port 80.
One alternative is to define that port declaration in different files and use different ports:
$ cat pizza.yml
services:
nginx:
ports:
- "8001:80"
$ cat pie.yml
services:
nginx:
ports:
- "8002:80"
$ cat soda.yml
services:
nginx:
ports:
- "8003:80"
docker-compose -f docker-compose.yml -f pizza.yml -p pizza up
docker-compose -f docker-compose.yml -f pie.yml -p pie up
docker-compose -f docker-compose.yml -f soda.yml -p soda up
That works because each stack is publishing port 80 to a different port. That's fine, but that's a little bit annoying because we have to stop/start the stack to do this.
How do we do this without publishing the port or stopping/starting the stack?
If this were a kubernetes cluster, we could use kubectl
to do this with a port-forward like so:
kubectl port-forward replicaset/nginx-75f59d57f4 8001:80
That way fits my situation a little better because we don't want to stop the stack to see what's going on in there. We can start the port-forward, see what's going on and then go away.
Is there an equivalent for docker?
Related Questions: