Home > Mobile >  How to forward 127.0.0.1 traffic from container-A to another container-B?
How to forward 127.0.0.1 traffic from container-A to another container-B?

Time:09-11

I am trying to containerize my company's app. When I deploy that app in VM, I have to install MySQL in the same VM because the application has a hardcoding 127.0.0.1:3306 in the source code. Now I am trying to separate the app into 2 containers: app-container and mysql-container. I have published mysql-container port 3306 to the host machine port 3306:

 docker run --name mysql-container -p 3306:3306 -d mysql:latest

The next step is to forward app-container 127.0.0.1:3306 to mysql-container port 3306.

I have tried --network="host" to make the app-container use the host machine's network so I can connect mysql-container's port 3306 from app-container's 127.0.0.1:3306 successfully. However, for some reason, the developer doesn't like it so I have to find another way to achieve that.

I am very new to Docker and container, any help is appreciated!

CodePudding user response:

Usually you should create a network - there are two options basically user defined network and a default bridge network.

When you create the network (this is an abstraction in docker, like you can docker network ls to see the available networks, for example) and run your two containers in the network - they'll access each other.

I've found a good tutorial about the implementation details that shows both cases enter image description here

Here you can see I define 2 services, one for the web app and one for the database, and each service will run in a separate container. I expose the port 5432 of the database container by using ports, then the api-server container can access it . If you are new to Docker you need to notice the volumes as well, volumes is a way to persist the database so that you will not lost all data when restart the container.

  • Related