5

How do I setup the docker-compose file to connect to an internal containers network, and to the outside localhost network?

I tried to use the extra_hosts property within the docker-compose but could not get the result I need

My docker-compose contains the following:

version: '3.7'

    services:

        myapp1:
            container_name: 
                  myapp1
            hostname:
                  myapp1
            build: 
                  context: 
                       .
                  dockerfile: 
                       Dockerfile
            command: 
                  ["npm", "start"]
            networks:
                  - container_network # This is an internal container network

        myapp2:
             ... # Same idea as above
            networks:
                  - container_network # This is an internal container network

    networks: 

         container_network:
              name:
                   container_network
              driver: 
                   bridge

I also have an standalone service, and I run it with the following command:

docker run --name standalone_service --network host

There are other reasons, that are irrelevant to my question, why the standalone_service cannot be merged into the docker-compose file. Therefore, merging it into the docker-compose file and using the container_network is not an option for this service.

I want myapp1, which is part of the internal container_network, to be able to access standalone_service which is on the host network. How do I do this?

user1064089
  • 99
  • 2
  • 4
  • 10

1 Answers1

7

Given that you've run the standalone_service as --network host, it is indistinguishable from any other process running on the host, and the various answers to From inside a Docker container, how do I connect to [the host] machine should work for you.

A second preferable approach could be to create a Docker network outside of Docker Compose, and attach the other container to that network

docker network create container_network
docker run --net container_network -p 8888:8888 --name standalone_service ...

Once you do that, you can declare the same network in your docker-compose.yml as an external network. You can even make this be the default network so that you don't have to manually configure your other services to use it.

version: 3
services:
  myapp1:
    build: .
    ports: ['3000:3000']
    environment:
      MYAPP2_URL: 'http://myapp2/'
      STANDALONE_URL: 'http://standalone_service/'
    # gets `command:` from the Dockerfile
    # attaches to the `default` network
networks:
  default:
    external: true
    name: container_network

You can also "borrow" the default default network that Docker Compose creates, if you don't mind starting things in the opposite order and depending on an implementation detail.

docker-compose up -d
docker run --net dirname_default -p 8888:8888 --name standalone_service ...

There's no such thing as "connect to the outside localhost network". Each network environment (the host, and separately, each container) has its own "localhost network" 127.0.0.0/8; localhost is fixed to the IP address 127.0.0.1, and that always means "me"; in a Docker context, "specifically the container I'm calling from".

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • 2
    Thank you for the explanation. What if I want a docker container to parse an non-dockerized REST API running somewhere on the the LAN, how do I get it to access this external/outside address? – user1064089 May 06 '19 at 20:35
  • Just use the other host's DNS name as normal. You don't need to do anything special. – David Maze May 06 '19 at 22:38