Accessing Docker Containers from Another Network

Accessing Docker Containers from Another Network

I have been working with ddev for a few months. It is a tool for quickly starting PHP development environments in Docker containers. The tool uses docker-compose in the background.

For a demo, I now needed to start Node-RED, a piece of software written in NodeJS. Since ddev is designed for PHP environments, I quickly started the software using a docker-compose.yml I had written myself.

At first glance, everything was running perfectly, until I noticed that the containers could not see each other!

My goal was to access the ddev container "ddev-webhooks-magento2-web" from Node-RED. For this, docker-compose provides a way to make a container known through the external_links configuration.

services:
   node-red:
      # ....
      external_links:
         - ddev-webhooks-magento2-web

However, that alone is not enough to make access work. The containers are on different Docker networks. And that is a good thing, since docker-compose creates a separate Docker network for each environment by default.

You can easily list Docker networks with the following command:

docker network ls

This is what it looks like on my machine:

NETWORK ID          NAME                     DRIVER              SCOPE
dd99c270ded1        bridge                   bridge              local
f42267f388c3        ddev_default             bridge              local
7147ccb3758a        host                     host                local
809ffae8da58        node-red_default         bridge              local
7a02fa1d3936        none                     null                local

To access the container on the ddev_default network, you also need to declare an external network.

networks:
  ddev_default:
    external: true

After that, you can make the network available to the service. The network automatically generated by docker-compose is always named default in docker-compose.yml.

Putting it all together, my demo now looks like this:

version: "3.7"

services:
  node-red:
    container_name: node-red
    image: nodered/node-red:latest
    environment:
      - TZ=Europe/Berlin
    user: root:root
    restart: unless-stopped
    external_links:
      - ddev-webhooks-magento2-web
    networks:
      - default
      - ddev_default
    ports:
      - "1880:1880"
    volumes:
      - ./volumes/data:/data:rw

networks:
  ddev_default:
    external: true

Within my node-red workflow, I was now able to access the container directly over HTTP.

Goal achieved.