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.

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 lsNETWORK ID NAME DRIVER SCOPE
de2bfab63d2c my-shared-network bridge localConfigure 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: trueAnd in your Quick’s docker-compose file:
services:
quick:
build: .
ports:
- "3000:3000"
networks:
external-shared-network:
name: my-shared-network
external: trueNow 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





