0

I have troubles understanding how to create the following setup using docker-compose.

Basically I have three containers (with separate docker-compose.yaml files, each started separately). I also have a bridge network that I created separately with docker network create my-network.

The docker-compose.yaml files would look like this:

version: "3"
services:
  service-name:
    image: image-name
    container_name: container-name
    networks:
      - my-network
networks:
  my-network:
    external: true

Now, I also would like to expose some ports of either a single container, or from my-network so that they become visible to localhost of the host system, i.e. I would like to be able to access them under 127.0.0.1:port_no on the host system. This is needed to connect to a service running on the host, and also because I'd like to be able to connect to some of the services running in the containers from outside, after creating an SSH tunnel to the host that connects to localhost:port.

The part I fail to understand is how to create these 'port connections'. My understanding is that usually (without the added network) this is done by firewall rules that open a port between the network of the container and the host's network. But here I think I don't have a network per container (or do I?), and I'd need a connection between my-network and the host's 'localhost' interface. Is this possible? And how do I specify this using docker-compose?

1 Answer 1

0

Set the ports key and the Docker engine will take care of the rest

version: "3"
services:
  service-name:
    image: image-name
    container_name: container-name
    ports:
      - <host port>:<container-port> 
    networks:
      - my-network
networks:
  my-network:
    external: true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.