Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make port mapping while docker run command?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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 ....
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading