I have pulled an MYSQL image from Docker Hub and ran it by setting necessary credentials, such as username, password etc. Then I have inspected the container by:
docker inspect CONTAINER_ID
where I got the IP address for the MySQL databese. Since the Docker deamon connect through bridge network, my IP addess was: 172.17.0.2. By specifying this IP I can connnect to the Database from the host computer(Fedora). But, how do I connectd to the MySQL database from another machine in the same LAN?
You need to bind the container port to a host machine port. Adding -p to the docker run command:
docker run -p HOST_PORT:CONTAINER_PORT
I.e.:
docker run -p 3306:3306 .......
So MySQL is available from the Fedora's LAN interface.
Related
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?
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.
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.
I have a Ubuntu machine with IP = 172.16.12.134. On this Ubuntu machine, I have a docker instance of MySQL image running. The IP address of docker instance is like 172.18.0.8.
I am able to access db from ubuntu machine terminal with command like mysql -h 172.18.0.8 -P 3306 -u root -p.
Is there a way to access the db from outside(any other machine)the ubuntu machine?
like mysql -h 172.16.12.134 -P 3306 -u root -p.
I exported the port of docker in yml file as -port 3306:3306.
I don't see the problem here. Your first step would be to expose the MySQL service to your host machine, which you have done using -port 3306:3306.
You verified this to be working which means MySQL 'appears' to be running at 172.16.12.134:3306. Therefore the only thing left to do is allowing connections on port 3306 in your firewall. This way people can connect using your Ubuntu machine host IP and the respective port 3306.
I have a Mysql server running on the host using default port 3306. I Want to run a MySQL docker container using network host but with a different port.
My configuration is defined in a docker-compose file. After building the image and tried running the container, it starts and shutdown with port conflict notice.
Is there a way to dynamically change the container port before starting up? I don't want to use the network bridge.
If using host networking is a hard requirement, then nothing in Docker space will be able to control or limit what ports the service does or doesn't use. You need to change a service-specific configuration file, environment variable, or command-line argument to make it listen somewhere else.
Particularly for servers that listen on a single TCP port (like most database and HTTP-based servers) the default Docker bridge/NAT setup should work fine; alternate setups like host networking and macvlan are unnecessary. If you're willing to use the standard setup, this is trivial:
version: '3'
services:
mysql:
image: mysql
ports: ['9999:3306'] # listen on host port 9999 instead
docker run --name 'dockername' -e MYSQL_ROOT_PASSWORD='password' -p 1000:3306 -d mysql
docker exec -it 'dockername' mysql -uroot -p
ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY 'password'
flush privileges;
here 1000 is the port at which you want to run your mysql docker container.
First I run mysql image:
docker run -e MYSQL_ROOT_PASSWORD=password -d -p 127.0.0.1:3308:3306 mysql
Then I use container bash:
docker exec -it my_container_name bash
In Bash I can successfully connect to MySQL server via command:
mysql -uroot -ppassword
But when I try to connect to MySQL container from Windows cmd:
mysql -uroot -ppassword -h127.0.0.1 -P3308
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (10061)
If I connect to 192.168.99.100 instead (this ip is returned by docker-machine ip), then the result is the same.
The question is: How do I correctly expose my MySQL port inside Docker to outside Windows?
The error is in your port mapping in the original docker run command, you just need to provide the ports, not the IP address:
docker run -e MYSQL_ROOT_PASSWORD=password -d -p 3308:3306 mysql
You can run docker ps -a to check for the port mapping in the running containers.
You should now be able to connect to MySQL using
mysql -uroot -ppassword -h192.168.99.100 -P3308
First, check netstat -an to make sure the port is open in Windows. If it is, also check the Windows firewall to make sure nothing is blocking connections to the port.
Most of my Docker experience is in CoreOS, so I'm not exactly sure how Windows handles routing traffic into the container. In CoreOS, it uses a proxy. If there is a proxy in Windows, make sure nothing is interfering with it.
Changing the port in which I was running the image worked.
I checked if this port was used by something else, but it was not used. so I just -desperately- run a new container in a different port '3309'. and it worked fine!
Make sure you have entered the port with -P.
I'm using the following Prestashop docker: https://hub.docker.com/r/prestashop/prestashop/
It so happens that I already have a mysql server running on the host, because that was included with the whole DirectAdmin panel.
The variable DB_SERVER does nothing if I specify localhost or the IP adres of my server. -p 3306:3306 is also not allowed, since that port is already in use.
How can a Docker container reach the host:3306 mysql server?
If I understand correctly, you have your Prestashop installation inside a container and MySQL server running on the Docker host?
Unless configured differently, both the container and the host are in the same bridge network.
Verify that your container is in the bridge network:
docker inspect <container-name>
"Networks": {
"bridge": {
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
}
}
Notice the gateway IP address - this is the address of the docker host inside the bridge network. Use it as a DB_SERVER variable to connect to the MySQL.
One year later...
Like what #NetworkMeister said, but, IPAddress instead.
docker inspect <container-name> | grep IPAddress
In other words, I used 172.17.0.2 and it worked.
I believe what you are looking to do is to connect your container process with mysql server that is running on that host. To retain using localhost as the assignment of DB_SERVER you can do the following...
docker run --net=host <YOUR_CONTAINER_OPTIONS>
By doing this your container will able to reach the mysql service that is deployed on localhost:3306.