1

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:

101010
  • 14,866
  • 30
  • 95
  • 172
  • `...but that's a little bit annoying because we have to stop/start the stack to do this.` - Why you need to stop if your stack already running, which means you would have already arranged the port correctly and started the stacks? – gohm'c Dec 25 '21 at 02:51

1 Answers1

0

You can start another container on the same network that is running something like socat to forward the ports:

docker run --rm -it -p 8001:80 --net pizza_default \
  nicolaka/netshoot \
  socat TCP6-LISTEN:80,fork TCP:nginx:80

A more automated example of this is seen with docker-publish that handles spinning up a separate network, attaching containers, and automatically stopping the forwarder when the target container exits by sharing the same pid namespace.

BMitch
  • 231,797
  • 42
  • 475
  • 450