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.

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. Commented Apr 17, 2020 at 3:04
  • I was not able to get the database connection. Is these steps look ok? Commented Apr 17, 2020 at 3:06
  • 1
    That's internal IP address.What's the environment?.Did you expose or remap port 3306 when starting or running the container?. Commented Apr 17, 2020 at 3: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. Commented Apr 17, 2020 at 3: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 Commented Apr 17, 2020 at 3:14

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You saved my day. Thank you. This is what I am looking for
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/

3 Comments

I am on windows machine
Not familiar with Docker for Windows, but it looks like a VM based approach. docs.docker.com/docker-for-windows/networking/…
Also looks like this was answered in stackoverflow.com/questions/40746453/…. Please check if that works.
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/).

Comments

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.

3 Comments

(This disables normal Docker network isolation and inter-container communications, and it wouldn't be my recommended solution...but it should work on Linux.)
@DavidMaze Yes, you are right. But I would love to know your recommended solution for such type of issue. Thanks.
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?.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.