0

I am trying to bind port 9000 on docker container 1 (A) to port 9000 on docker container 2 (B).

I tried the --net host option but this does not help me. I need both containers to be on the same network (172.17.0.x) and have their ports bound similar to how the -p port flag is used to bind one container to the host. I have tried setting up listening ports on both A and B using the --expose flag but I have been unable to bind them.

docker run -p 8080:8080\ # to bind port 8080 on A to port 8080 on the host
    -it -d \
    --cap-add NET_ADMIN \
    --cap-add NET_RAW \
    --expose 9000 \ # listening port to communicate with B
    <image file>

docker run -p 8081:8081\ # to bind port 8081 on B to port 8081 on the host
    -it -d \
    --cap-add NET_ADMIN \
    --cap-add NET_RAW \
    --expose 9000 \ # listening port to communicate with B
    <image file>

I would like to map A's port 9000 to B's port 9000 (or any other port) Thanks!

  • Can you describe the use case where you would need to do this? – BMitch Apr 17 '19 at 19:57
  • @BMitch my use case is described in this question: https://stackoverflow.com/questions/55698728/how-can-i-create-a-tunnel-between-multiple-docker-containers-and-the-host I want to map their ports to serve as a tunnel. – new_docker_user Apr 18 '19 at 01:23

2 Answers2

0

That is not possible to do. But is not need as well. If both containers are on the same network they can see each other, just reference them by IP or name.

To add a significant name use --name my_memorable_name as parameter to docker run and you are set to go. With that from within any other docker container you may reference that specific container by name, ex:

curl http://my_memorable_name:8080

This will work as expected. Just remember that container names must be unique.

Pedro
  • 850
  • 1
  • 10
  • 19
0

What you're describing is not possible to do with the expose parameter. And the --net=host tells your container to run on the host's network exposing all ports from all your containers started with that flag.

You could create a docker network and connect both containers to it.

Example:

$ docker network create mynetwork --driver bridge
$ docker run -d --name containerA -p 8080:8080 --net=mynetwork <image>:tag
$ docker run -d --name containerB -p 8081:8081 --net=mynetwork <image>:tag

This will create a DNS record for containerA and containerB with the same name of the container that will be resolvable from any container within your user-defined network.

So now if you want to access containerB from within containerA you just use the name of the container, for example:

curl http://containerB:8080

Another solution will be to use docker-compose:

version: '3'
services:
  containerA:
    image: <image>:<tag>
  containerB:
    image: <image>:<tag>

As docker-compose creates a user-defined network as well, you can access the containers in the same way I described before.

Read more about docker-compose and user-defined networks here: https://docs.docker.com/compose/overview/

https://docs.docker.com/network/bridge/

Esteban Garcia
  • 2,171
  • 16
  • 24