dockerapachenetwork-programmingserverbridge

Docker networks: How to get container1 to communicate with server in container2


I have 2 containers on a docker bridge network. One of them has an apache server that i am using as a reverse proxy to forward user to server on another container. The other container contains a server that is listening on port 8081. I have verified both containers are on the same network and when i log into an interactive shell on each container i tested successfully that i am able to ping the other container. The problem is, is that when i am logged into the container with the apache server, i am not able to ping the actual server in the other container.

the ip address of container with server is 172.17.0.2

How i create the docker network

docker network create -d bridge jakeypoo

How i start the containers

docker container run -p 8080:8080 --network="jakeypoo" --
name="idpproxy" idpproxy:latest

docker run -p 8081:8080 --name geoserver --network="jakeypoo" geoserver:1.1.0

wouldn't the uri to reach out to the server be

http://172.17.0.2:8081/  

?

PS: I am sure more information will be needed and i am new to stack overflow and will happily answer any other questions i can.


Solution

  • Since you started the two containers on the same --network, you can use their --name as hostnames to talk to each other. If the service inside the second container is listening on port 8080, use that port number. Remappings with docker run -p options are ignored, and you don't need a -p option to communicate between containers.

    In your Apache config, you'd set up something like

    ProxyPass "/" "http://geoserver:8080/"
    ProxyPassReverse "/" "http://geoserver:8080/"
    

    It's not usually useful to look up the container-private IP addresses: they will change whenever you recreate the container, and in most environments they can't be used outside of Docker (and inside of Docker the name-based lookup is easier).

    (Were you to run this under Docker Compose, it automatically creates a network for you, and each service is accessible under its Compose service name. You do not need to manually set networks: or container_name: options, and like the docker run -p option, Compose ports: are not required and are ignored if present. Networking in Compose in the Docker documentation describes this further.)