avatarDavid Toth

Summary

The article outlines the process of enabling communication between two separate Docker containers from different applications by creating and connecting them to an external Docker network.

Abstract

The article addresses a common challenge in containerized environments: inter-container communication across different Docker applications. It begins by illustrating a scenario where a Qwik frontend application needs to communicate with a NestJS REST API, both running in separate Docker containers managed by distinct docker-compose files. The author explains that while browser-based access to these containers is straightforward, server-side communication between them is not, as they operate on different networks. The solution proposed is the creation of an external Docker network, my-shared-network, to which both containers can connect. The article provides a step-by-step guide on creating this network using the Docker CLI and configuring both applications' docker-compose files to use this shared network. By doing so, the containers can communicate with each other as if they were on the same local network. The article concludes with the assurance that once the containers are rebuilt and restarted, they will be able to reach each other through the shared network.

Opinions

  • The author implies that using localhost for inter-container communication is ineffective when containers are not on the same network.
  • It is suggested that the external network approach is a straightforward and effective solution for enabling communication between containers from different Docker applications.
  • The author emphasizes the simplicity of creating and configuring an external Docker

How to communicate between two containers from different Docker App

Usually, your project in your git repository has a docker-compose file to boot up your app and the connected services for example RabbitMQ, MySql and so far. You can configure it to tell docker to expose which ports have to be exposed, thus you can easily access your web application from a browser.

For example, we have a Qwik application for “frontend” which communicates with a NestJS REST API. These two projects exist in two different repositories, with two different docker-compose files. Both are running happily in containers, you can access them from different ports via a browser. If you wanted to reach the NestJS application from the client-side code, it won’t be a problem, because the code runs in your browser.

But what if we wanted to access this NestJS API from a different container?

If you want to fetch some data from codes that run on Qwik’s server side — before the page will be rendered - Qwik’s docker network won’t reach the `localhost:8000` because this does not run in that container. It’s like two different computers want to communicate, if they don’t connect to the same network they cannot exchange messages.

The very simplified draw of the communication issue

TLDR: The solution is creating an external docker network and connecting the docker containers to that.

External network

The idea

We are going to create an external network. This will be used by the different containers.

Create an external network

Creating a new network is quite simple, just run the following command:

docker network create  my-shared-network

Now if you list your docker networks you could find your newly created network:

docker network ls
NETWORK ID     NAME                        DRIVER    SCOPE
de2bfab63d2c   my-shared-network           bridge    local

Configure network in docker-compose

In your docker-compose files, you can add the external network for each.

External network for the NestJS’s docker-compose file:

services:
  nestjs-api:
    build: .
    ports:
      - "8000:8000"
networks:
  external-shared-network:
    name: my-shared-network
    external: true

And in your Quick’s docker-compose file:

services:
  quick:
    build: .
    ports:
      - "3000:3000"
networks:
  external-shared-network:
    name: my-shared-network
    external: true

Now if you rebuild and restart your containers they can reach each other!

⭐️ If you’d like to support my work, you can always buy me a coffee!

Cover photo by Marco Bianchetti on Unsplash icons created by Pixel perfect — Flaticon

Docker
Nestjs
Docker Networking
Docker Compose
Recommended from ReadMedium