1

See this answer for forwarding a port from one Docker container to another. How can I do the same in Docker Compose?

haba713
  • 2,465
  • 1
  • 24
  • 45

1 Answers1

2

It is possible to define using network_mode key (documentation):

version: "3"
services:
  foo:
    container_name: foo

  bar:
    network_mode: container:foo

Though I'd rather avoid that; this isn't 'port forwarding', this is an instruction to use network stack of another container. Better use service names instead of localhost. I.e. to address foo container port use foo:<port> instead of localhost:<port>. This way you won't be limited in replicas and network configuration options.

anemyte
  • 17,618
  • 1
  • 24
  • 45
  • 3
    I usually use service names but in this case the application has `localhost` hard coded as database hostname and I must somehow get `localhost:5432` forwarded to `dbcontainer:5432`. – haba713 May 11 '21 at 11:00