Andy
Andy

Reputation: 2593

Is it possible to bind port XXXX on a docker container A to port XXXX or YYYY on docker container B

Let's say I have a docker-compose file as below:

version: '2'
services:

  # The php-fpm container
  app:
    build:
      context: ./
      dockerfile: app.dev.dockerfile
    working_dir: /var/www
    volumes:
      - ./:/var/www

    expose:
      - 8003

  # The Web Server
  web:
    build:
      context: ./
      dockerfile: web.dockerfile
    working_dir: /var/www
    volumes_from:
      - app
    links:
       - app:app

    ports:
      - 80:80
      - 8004:8003

So I like to serve the port 8003 on App container from port 8004 in Web container. But I am not able to get the result. So my guess is that Web port mapping of 8004:8003 on Web container is limited to Web container only. Or is it actually mapped to exposed 8003 port on the App container?

If so how do I test that Web port 8004 is in fact mapped to port 8003 on App container?

Is there a way for me to create a one directional binding (or even better bidirectional binding) or mapping of port 8003 on App container to port 8004 on Web container ?

If my host is running some App and docker services (that includes these two container), and App is trying to listen on port 8004 in Web container he is actually communicating to the App container.

Upvotes: 0

Views: 593

Answers (1)

Matt
Matt

Reputation: 74630

Containers are designed to keep each process seperate. Each process/container has it's own seperate network stack

The only way "mapping" as you describe from one container to another is achieved is if there is a user space process that forwards requests. Like Apache http or Nginx does to forward HTTP requests to a FastCGI PHP process.

For debug, map the port directly to the app container. If you are not able to connect there is likely a problem with the debug setup in the app container.

  # The php-fpm container
  app:
    build:
      context: ./
      dockerfile: app.dev.dockerfile
    working_dir: /var/www
    volumes:
      - './:/var/www'
    ports:
      - '8003:8003'

  # The Web Server
  web:
    build:
      context: ./
      dockerfile: web.dockerfile
    working_dir: /var/www
    volumes_from:
      - app    
    ports:
      - 80:80

By the way, links are not required for version 2+ connectivity. Compose will create a user defined network by default which allows access between your containers.

Upvotes: 1

Related Questions