How to connect docker container with host machine's localhost mysql database?
Asked Answered
A

5

6

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.

Asha answered 17/4, 2020 at 3:0 Comment(12)
"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.Mulligan
I was not able to get the database connection. Is these steps look ok?Asha
That's internal IP address.What's the environment?.Did you expose or remap port 3306 when starting or running the container?.Hypsometry
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.Mulligan
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-tomcatAsha
I have not created any docker file. I pulled the tomcat repo from docker hub and manually deployed my war file thereAsha
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)Hypsometry
And what will be the connection string?Asha
docker run -d -p 3306:3306 -p 8082:8080 6ab907c973d2Asha
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.Asha
Does this answer your question? How to connect to docker host from container on Windows 10 (Docker for Windows)Pinsky
I am trying with these solutions, please let me check if this resolves my issue or notAsha
A
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.

Asha answered 17/4, 2020 at 7:24 Comment(1)
You saved my day. Thank you. This is what I am looking forAsha
P
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/

Pinsky answered 17/4, 2020 at 5:6 Comment(3)
I am on windows machineAsha
Not familiar with Docker for Windows, but it looks like a VM based approach. docs.docker.com/docker-for-windows/networking/…Pinsky
Also looks like this was answered in #40746953. Please check if that works.Pinsky
K
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/).

Kurys answered 24/11, 2020 at 4:15 Comment(0)
S
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.

Sola answered 17/4, 2020 at 6:4 Comment(3)
(This disables normal Docker network isolation and inter-container communications, and it wouldn't be my recommended solution...but it should work on Linux.)Brittani
@DavidMaze Yes, you are right. But I would love to know your recommended solution for such type of issue. Thanks.Sola
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?.Brittani
A
0

Here's an update on this if you are using docker compose file. Follow the steps by Sayed above to create a new network bridge

docker network create -d bridge --subnet 192.168.0.0/24 --gateway 192.168.0.1 dockernet

Then in your docker-compose you can update the network and mysql host like the sample below. your container will run under the dockernet network and will connect to the host server without issues

services:
  web:
    build: .
    networks:
      - network1
    user: "node:node"
    environment:
      - DB_URI=jdbc:mysql://host.docker.internal:3306
      - DB_HOST=host.docker.internal
      - DB_PORT=3306
      - DB_USER=username
      - DB_PASSWORD=
      - DB_DATABASE=databaseName
    volumes:
      - "./:/usr/src/app"
    ports:
      - "3000:3000"

networks:
  network1:
    name: dockernet
    external: true

just run docker compose up --build -d

Altar answered 24/8 at 19:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.