0

I have a docker container running a Flask application that connects to a mySQL server. The mySQL server is hosted on the host machine at port 3308 on a windows 10 machine.

When executing docker run -p 5000:5000 -p 3308:3308 -t webui

I receive the error Ports are not available: listen tcp 0.0.0.0:3308: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted. due to the port being used by the mySQL server on the host machine

How do I map the port of the mySQL to the docker container such that the Flask application can access the database?

  • Have you checked docker ps ? Is any container running ? And what close the mysql locally if you are running as shown over here https://www.mysqltutorial.org/mysql-adminsitration/stop-mysql/. – mozilla-firefox Feb 11 '20 at 05:42
  • yes? The port clash is caused by the mysql service running on the host. When I stop the service, I am able to create the container with port 3308 mapped through – Cameron Moore Feb 11 '20 at 05:43
  • If any container is running, stop it because it has occupied that port. If you are binding with host machine, then you have to stop that as well inorder to run at that port. Else you can obviously change the port for this container – mozilla-firefox Feb 11 '20 at 05:50
  • There are no containers running. If I stop the mySQL service on the host machine then the container cannot connect to the mySQL server. I am wanting to find out how to connect to a mySQL server that is hosted on the host machine from a container – Cameron Moore Feb 11 '20 at 06:06
  • mysql://host.docker.internal:3306/my_awesome_database try this in container or better read this answer, it will help you surely: https://stackoverflow.com/questions/28056522/access-host-database-from-a-docker-container – mozilla-firefox Feb 11 '20 at 06:18
  • Is the Flask image running its own MySQL server, or is it calling out to one somewhere else? You don't need a `-p` option to make outbound calls, only to accept inbound connections. – David Maze Feb 11 '20 at 11:29

2 Answers2

0

There are 2 ways to achieve this. The first approach is the recommended one.

The first is to add an entry to /etc/hosts inside the container:

docker run -p 5000:5000 -p 3308:3308 --add-host database:<HOST_IP> -t webui

You need to replace HOST_IP with the network IP of your host. Then you can reference the database inside your container using the name "database" (you can also customize this one).

The second is to bind your container to your host's interface:

docker run -p 5000:5000 -p 3308:3308 --bind 127.0.0.1 -t webui

Then you can refer to your database with 127.0.0.1 inside your container.

Mihai
  • 9,526
  • 2
  • 18
  • 40
0

The issue was caused by the host name. The mySQL database port did not need to be bound to the container as it did not need to receive any inbound calls, only outbound to the database. Resolved by adding a new entry into the container's /etc/hosts file as described here.