0

I have a server which contains a python file that connects to two external MySQL DB's. One of those DB's can be easily reached while the other server requires that IP's be whitelisted in order to have access to that DB. That server's IP is already whitelisted and works as intended when ran.

The problem arises however when I attempt to run the docker-ized variation of the application. The first DB runs just as it does before but the second DB no longer works. When inside the container, I can ping the second DB and it works, but whenever I try to access it via the code hosted on the server, it doesn't return data within any of the functions that utilize it. I noticed that the container has a separate IP, and may be causing issues since that docker container's IP would not have been whitelisted and may be where the problem begins. I am fairly new to Docker, so any documentation links that would assist me would be extremely helpful.

  • 2
    This seems like it's a question about your remote database setup, more than a programming question _per se_. From the point of view of the remote database it should still look like the connection is coming from the same host; it doesn't have any idea the client code is in a container. – David Maze Dec 22 '21 at 16:47
  • @DavidMaze Thanks for the response. That makes sense, but I am still wondering why there is a difference between both the local and container versions? – AamirtheDev Dec 22 '21 at 17:02
  • "it doesn't return data within any of the functions that utilize it" that's a pretty poor description of failure case. It doesn't return data? So you're able to query it successfully ? Or what? Include those details when you ask on the appropriate stack exchange site – erik258 Dec 22 '21 at 17:22
  • Check the connection from inside your container with telnet (telnet host 3306) or mysql-cli (mysql -h host -u username -p). You will see if the error comes from your dockerized app or from the network. – Sylwit Dec 30 '21 at 17:05

1 Answers1

0

So for anyone who is dealing with this situation in the future, I added the line

network_mode: "host"

to my docker.compose.yaml file. Here is some docs related to this: https://docs.docker.com/network/host/

Essentially what was happening is that the container could not be recognized by the whitelist and was not being allowed access to the second DB. With this change, it allowed the container to share the same network as the server it was being hosted on, and since that server has been whitelisted prior, it all worked out of the gate.

If you are using docker, then use

--net=host

within your run command. Here is a SO link about what this addition does: What does --net=host option in Docker command really do?