Docker Community Forums

Share and learn in the Docker community.

How connect external database with docker container service

Hi all,

i am new in docker, i face error when i tried to connect local database with container service.

please someone help me how to connect local database with docker container service

Use the IP address of the machine where the database is running (the real one, not localhost) as host address.

1 Like

i just use IP address of the machine where database is running. used this jdbc connection URL jdbc:mysql://192.168.0.1:3306/testdb

docker run --name src
-p 8081:8081
-d test
-e MYSQL_PASSWORD=12345678
-e MYSQL_DATABASE=testdb
-e MYSQL_USER=root
-e MYSQL_HOST=192.168.0.1

Open Oracle VM VirtualBox Manager.
Select the VM used by Docker.
Click Settings -> Network.
Adapter 1 should (default?) be “Attached to: NAT”
Click Advanced -> Port Forwarding.
Add rule: Protocol TCP, Host Port 8080, Guest Port 8080 (leave Host IP and Guest IP empty)
Guest is your docker container and Host is your machine.

Hello @habibsumon!

I too am rather new to Docker, however I have been a Database Engineer for over 13 years now and have administered and migrated just about every RDBMS type from one infra / arch / tech to another, to another, to current where I actively admin Multi-Region psuedo master-master (bi-directional master-slave) replicated MySQL Server stacks hosted on EC2 servers in the AWS Cloud.

I am also currently tasked with building our Dockerized / Containerized MySQL Server implementations integrated fully with our CI / CD pipeline workflows under IAC - so, I can share your frustration when first trying to learn this process and guide yourself along the way - and to that end I would like to offer you my assistance if I can do so…

That said - I do feel like I need a bit more context around the specific issue you are facing…

Firstly - what is the specific error message you get when you try to connect from your Host machine to MySQL Server running inside your Docker Guest?

Second - based on your docker run CLI command above - I see that you are publishing / “binding” your Host’s port 8081/tcp to your Docker Guest’s port 8081/tcp - yet your MySQL Server appears to be running on the default MySQL port 3306 based on the JDBC url you shared…why are you forwarding port 8081? Is that for PhpMyAdmin server? If so, where is that running, is that also running inside your Docker Guest? If so, is it configured for, and able to connect to the MySQL Server its running in parallel to in your Container? If PhpMyAdmin server is running on your Host machine, and you wish to connect it to the MySQL Server running in your Container Guest - I would also suggest adding one more published port to your docker run command: -p 3336:3306 - then, in your Host machine hosts file, add a hook to an alias like: 127.0.0.1 mysql-docker - then, finally, after confirming your Container has started and initialized successfully, try connecting with the following JDBC URL: jdbc:mysql://mysql-docker:3336/testdb

Please try suggestions above, and if you still face issues - provide some further information and context around your issue, including answers to my questions above.

Thanks, and best of luck!

Cheers,

Chris Bishop - Senior Database Solutions Architect

The command above to run your container seems incomplete. It is missing the image field (See below). Are you using a custom image? You need to provide a bit more details about your setup. What is your OS? If you are using a custom image, please share the Dockerfile. In addition, MYSQL might be connecting using a different port, probly 3306 so you might need to expose this port. It really depends on the image you are using.

One thing: After you issue the run command, you can get the name of the container via docker ps -a and then if it is up and running (as reported by docker ps) then you can connect to it via docker exec -it {container-name} /bin/bash and then run the mysql command to explore your setup.

docker run --name src
-p 8081:8081
-d test
-e MYSQL_PASSWORD=12345678
-e MYSQL_DATABASE=testdb
-e MYSQL_USER=root
-e MYSQL_HOST=192.168.0.1