cover-img

How to communicate two Docker containers? 🐳

My solution was simple: embedding an API.

24 October, 2021

17

17

2

When we have a distributed application in different servers, based on microservices, we normally use APIs to communicate the different parts of the architecture. But in case of having some Docker containers within the same subnet: How we handle communication between them?
That was one of my first questions to solve when developing my VPN, which is based on WireGuard.
Basically, I have a container based on linuxserver/wireguard to which my Telegram bot sends commands to create VPN configurations, and I have some WireGuard nodes located in other servers, controlled by a master process.
When trying to communicate my bot container with the WireGuard container, I thought on many options, but the one that felt right was creating an API inside WireGuard and expose it in the container and to the local subnet, allowing others to send requests and commands.
In this schema, we have two containers. What we do is creating an API on the container that's going to receive instructions, with optional authentication/authorization checks. Te requests will be sent via the HTTP protocol, that's why we need a web server (like Django, FastAPI, Flask, whatever...) and a port, in this example I used 8008, but you can use any other.

Show me the code 👨‍💻

In this example, we create the API using Fast API, allowing to send requests with a token authentication to check if the commands being sent are authorized. We save the token in an environment variable and only execute the commands if the token matches, otherwise we return a "forbidden" response. To run the commands over the container, we use subprocess.
Then we create the Dockerfile, which will be based on the linuxserver/wireguard image and over which we install Python, Fast API and other dependencies.
When the container is up, the WireGuard and API processes will be executed, the first exposed via the 51820 port ant the API via the 8008.
Then, we create the docker-compose.yml to run the service in production.
I just open sourced the repository, sharing it here in case you want to look into it. Inside, you'll see the dependencies in requirements.txt. Don't forget to create the .env file to specify the token and other environment variables needed to run the container.
Beyond this specific example, what I tried to explain was a way to communicate Docker containers. Surely there are better ways to do it, but this is the one I found. Following this example, you can create something like this for your application.

containers

docker

fast api

wireguard

17

17

2

containers

docker

fast api

wireguard

Carlos Lugones
Serial Tech Entrepreneur

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2025. Showcase Creators Inc. All rights reserved.