Home > Software design >  How to make port mapping while docker run command?
How to make port mapping while docker run command?

Time:03-04

I have 2 different service as payment and order. I want them to use different dbs.

First I pull and run mysql db image for order service by running following command;

docker run --name mysql-order -p 3306:3306 -e MYSQL_ROOT_PASSWORD=1234 -e MYSQL_ROOT_HOST=% -e MYSQL_DATABASE=orderdb --platform=linux/x86_64 -d mysql --lower_case_table_names=1 --init-connect='GRANT CREATE USER ON *.* TO 'root'@'%';FLUSH PRIVILEGES;'

It works fine and I can connect to it via spring boot app order-service.

Then I want to create another db container with same command but this time changing name and port by running:

docker run --name mysql-payment -p 3407:3407 -e MYSQL_ROOT_PASSWORD=1234 -e MYSQL_DATABASE=paymentdb --platform=linux/x86_64 -d mysql --lower_case_table_names=1 --init-connect='GRANT CREATE USER ON *.* TO 'root'@'%';FLUSH PRIVILEGES;'

On docker dashboard label it is running on port 3407 enter image description here

But when I go inside to logs of the payment container than it shows 3306 again. enter image description here

And when I check the ports with docker ps command the result is:

CONTAINER ID   IMAGE     COMMAND                  CREATED             STATUS             PORTS                                         NAMES
a113f66398c4   mysql     "docker-entrypoint.s…"   8 minutes ago       Up 8 minutes       3306/tcp, 33060/tcp, 0.0.0.0:3407->3407/tcp   mysql-payment
4b887a1d38d2   mysql     "docker-entrypoint.s…"   About an hour ago   Up About an hour   0.0.0.0:3306->3306/tcp, 33060/tcp             mysql-order

They are working but I cannot connect to payment db and the ports are weird as you can see above.

What am I missing? Where is this 3306 coming from for payment service even if I specified the port number different ? Or maybe I can ask like this: Can't I run 2 different mysql container on same machine like I have completely 2 different db?

CodePudding user response:

Because the service does not listen to port 3407, but to port 3306 inside your container. So the port mapping should be 3407:3306.

Something like

docker run --name mysql-payment -p 3407:3306 ....
  • Related