5

I have a war file that uses the MySQL database in the backend. I have deployed my war file in a docker container and I am able to ping this from my browser. I want to connect my app with the MySQL database. This database exists on my host machine's localhost:3306

As I am unable to connect this from inside container's localhost, what I tried is, I run a command docker inspect --format '{{ .NetworkSettings.IPAddress }}' 213be777a837 This command gave me an IP address 172.17.0.2. I went to MySQL server options and put this IP address in the bind field and restarted the server. After that, I have updated my projects database connection string with 172.17.0.2:3306

But it is not working. Could anyone please tell what I am missing? I have also tried adding a new DB user with root@% and then run command allow all permission to 'root@%' but nothing worked.

Lin Du
  • 88,126
  • 95
  • 281
  • 483
Sayed Uz Zaman
  • 606
  • 1
  • 9
  • 25
  • "not working?" not a useful term. So what is the error message? What was your results? What did you expect? Show your dockerfile/how you are starting it. – danblack Apr 17 '20 at 03:04
  • I was not able to get the database connection. Is these steps look ok? – Sayed Uz Zaman Apr 17 '20 at 03:06
  • 1
    That's internal IP address.What's the environment?.Did you expose or remap port 3306 when starting or running the container?. –  Apr 17 '20 at 03:11
  • No. 'get the connection' is also ambiguous if its a network or a permission issue. Be specific on error messages especially. This is why I asked for your dockerfile and how you started it. – danblack Apr 17 '20 at 03:13
  • running docker container ls i can see CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 213be777a837 6ab907c973d2 "catalina.sh run" 44 hours ago Up 20 minutes 0.0.0.0:8082->8080/tcp my-tomcat – Sayed Uz Zaman Apr 17 '20 at 03:14
  • I have not created any docker file. I pulled the tomcat repo from docker hub and manually deployed my war file there – Sayed Uz Zaman Apr 17 '20 at 03:18
  • You didn't expose or remap port 3306,stop the container and run with docker container option -p 3306:3306(use different port if you have other mysql instances running on host) –  Apr 17 '20 at 03:21
  • And what will be the connection string? – Sayed Uz Zaman Apr 17 '20 at 03:36
  • docker run -d -p 3306:3306 -p 8082:8080 6ab907c973d2 – Sayed Uz Zaman Apr 17 '20 at 03:51
  • C:\Program Files\Docker\Docker\resources\bin\docker.exe: Error response from daemon: Ports are not available: listen tcp 0.0.0.0:3306: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted. – Sayed Uz Zaman Apr 17 '20 at 03:52
  • Does this answer your question? [How to connect to docker host from container on Windows 10 (Docker for Windows)](https://stackoverflow.com/questions/40746453/how-to-connect-to-docker-host-from-container-on-windows-10-docker-for-windows) – chamilad Apr 17 '20 at 05:36
  • I am trying with these solutions, please let me check if this resolves my issue or not – Sayed Uz Zaman Apr 17 '20 at 05:44

4 Answers4

2

Follow the steps:-

docker network create -d bridge --subnet 192.168.0.0/24 --gateway 192.168.0.1 dockernet
docker run -p 8082:8080 --network dockernet -d 6ab907c973d2
in your project set connection string : jdbc:mysql://host.docker.internal:3306/....

And then deploy.

Sayed Uz Zaman
  • 606
  • 1
  • 9
  • 25
1

tl;dr: Use 172.17.0.1:3306 if you're on Linux.

Longer description:

As I understand what you need to do is to connect from your Docker container to a host port. But what you have done is to try to bind the host process (MySQL) to the container networking interface. Not sure what the implications of a host process trying to bind to another host process network namespace, but IIUC your MySQL process should not be able to bind to that address.

When you start MySQL with default settings that bind it to 0.0.0.0 it's available for Docker containers through the Docker virtual bridge. Therefore, what you should do is to route your requests from the WAR process to the host process through that virtual bridge (if this is the networking mode you're using. If you have not changed any Docker networking settings, it should be). This is done by specifying the bridge gateway address as the MySQL address and the port it's started with.

You can get the bridge IP address by checking your network interfaces. When Docker is installed, it configures the virtual bridge by default, and that should show up as docker0 if you're on Linux. The IP address for this will most probably be 172.17.0.1. So your MySQL address from the container's point of view is jdbc:mysql://172.17.0.1:3306/....

enter image description here

1 - https://docs.docker.com/network/

2 - https://docs.docker.com/network/bridge/

chamilad
  • 1,619
  • 3
  • 23
  • 40
  • I am on windows machine – Sayed Uz Zaman Apr 17 '20 at 05:26
  • Not familiar with Docker for Windows, but it looks like a VM based approach. https://docs.docker.com/docker-for-windows/networking/#there-is-no-docker0-bridge-on-windows – chamilad Apr 17 '20 at 05:35
  • Also looks like this was answered in https://stackoverflow.com/questions/40746453/how-to-connect-to-docker-host-from-container-on-windows-10-docker-for-windows. Please check if that works. – chamilad Apr 17 '20 at 05:36
1

From your question, I am assuming you both your war file and MySQL is deployed locally, and you want to connect them. One way to allow both containers that are locally deployed to talk to each other is by:

  1. Create your own network docker network create <network-name>

  2. Then when you run your war file and MySQL, deploy both of them using the --network. E.g.

    • War File: docker run --name war-file --network <network-name> <war file image>
    • MySQL: docker run --name mysql --network <network-name> <MySQL image>
  3. After that, if you should be able to connect to your MySQL using mysql:3306 from inside your war file docker container, since they are both on the same custom network.

If you want to read up more about this, can take a look at docker documentation on network. (https://docs.docker.com/network/bridge/).

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

Your setup is fine. You just need to do this one change.

While running the application container (the one in which you are deploying your war file), you need to add following argument in its docker run command.

--net=host

Example:

docker run -itd -p 8082:8080 --net=host --name myapp myimage

With this change, you need not to change connection string as well. localhost:3306 would work fine. And you will be able to set up a connection with MySQL.

Kapil Khandelwal
  • 1,096
  • 12
  • 19
  • (This disables normal Docker network isolation and inter-container communications, and it wouldn't be my recommended solution...but it should work on Linux.) – David Maze Apr 17 '20 at 11:20
  • @DavidMaze Yes, you are right. But I would love to know your recommended solution for such type of issue. Thanks. – Kapil Khandelwal Apr 17 '20 at 12:12
  • Either of the other answers here, or the more detailed explanations in [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach). – David Maze Apr 17 '20 at 13:28