Connect a container Docker to an external Docker network
In multiple situations as developers, when we are in the development phase, we have an application that needs to communicate with other applications in our local environment.
When using Docker, In the
docker compose file you define all the services needed by your app in a
docker-compose.yml file. At the time that you work with another application in another .yml file which network is different, you will get a network connection error like the one below:
On this little post, we will be doing a connection between two docker containers that’s on different docker networks using docker-compose files.
We have two applications
ceb-setting-api each of them defined with their respective docker-compose.yml and network.
To be able to make requests between them, we must carry out these simple steps:
- Run the
docker-compose -f docker-compose-setting-api.yml up -d
2. In the
docker-compose-roster-api, add in the section
networks general of the docker-compose file the
setting-api network like
3. After that, In the
docker-compose-roster-api , on the api.roster.com service, add the
4. Then, we proceed to replace all places where you are using
setting-api, If you are using Environment variables, you can use this example:
# api.rosters.com/.env file#use
5. Finally, Run the
docker-compose -f docker-compose-roster-api.yml up -d
To test if the configuration is working properly, we can do a
setting-api Figure 2.
That’s all folks!
Thanks for reading, please feel free leave any feedback or questions in the comments 🤘.
Follow me in my networks