Search code examples
apachedockerdocker-composemod-proxy

Use ProxyPass configuration for Apache in docker


How do I use ProxyPass in Apache config that links to a different container? hard coding ip address works, but is there a more generic way to do this?

Container1: apache listens on port 443 proxy passes it to Container2 port 3000

Container2: Runs any Flask/Dancer app that listens on port 3000

#docker-compose.yml
version: '3'
services:
  apache:
    ...
    ports:
      - "443:443"
    container_name: Container1
    network_mode: "bridge"
    depends_on: 
      - web
  web:
    ...
    container_name: Container2
    network_mode: "bridge"

-

#apache config
<VirtualHost *:443>
  ...
  ProxyPreserveHost on
    # Works but with ip 
    #ProxyPass / http://ip-of-container2:3000/
    #ProxyPassReverse / http://ip-of-container2:3000/
  ProxyPass / http://Container2:3000/
  ProxyPassReverse / http://Container2:3000/
</VirtualHost>

Solution

  • So, network_mode is the issue.

    I remove network_mode and have a test for next:

    version: '3'
    services:
      apache:
        image: ubuntu
        container_name: Container1
        #network_mode: "bridge"
        tty: true
      web:
        image: ubuntu
        container_name: Container2
        #network_mode: "bridge"
        tty: true
    

    After docker-compose up -d, I enter into Container1 and install ping, I can ping Container2.

    root@fe69bd6d62af:/# ping Container2
    PING Container2 (172.26.0.3) 56(84) bytes of data.
    64 bytes from Container2.bb_default (172.26.0.3): icmp_seq=1 ttl=64 time=0.166 ms
    64 bytes from Container2.bb_default (172.26.0.3): icmp_seq=2 ttl=64 time=0.064 ms
    

    You may also reference to official doc to know what user-defined bridges bring to us compare to default bridge, the main difference is next:

    User-defined bridges provide automatic DNS resolution between containers.

    If you do not specify network_mode, compose will automatically setup a user-defined bridges for you, so you can use this magic. In my example, bb_default is the bridge compose setup for us, you can use docker network ls to confirm it. Also, could use docker network inspect bb_default to see details of this new bridge.