Connecting to localhost mysql server from inside docker container on macOS - mysql

How can I connect to localhost mysql server from a docker container on macOS ?
One way to do it using --add-host but that requires me to pass some name like "myhost".
Is there any way in macOS so that references to localhost from inside docker container actually refer to docker host ?

On MacOS docker provide special DNS name docker.for.mac.localhost which will resolve to the internal IP address used by the host.

use host.docker.internal
to connect to the host running the Docker.
this works From docker version 18.03 onwards only and This is for development purposes and will not work in a production environment outside of Docker Desktop for Mac.
( refer the page https://docs.docker.com/docker-for-mac/networking/ for more info )
sample connection string for oracle, jdbc:oracle:thin:#host.docker.internal:1521/orcl
From inside of a Docker container, how do I connect to the localhost of the machine?

You should be able to connect to MySql running on host machine using host machine actual IP address. In MacOS, try to find your ip by command ifconfig. Mostly using IP assigned to en0 i.e. your ethernet interface should work. Just call that IP from within your container.
*localhost or 127.0.0.1 or 0.0.0.0 doesnt call host machine as they are local to container itself.

Related

Node RED on Docker writing on MYSQL database

I am running Node-RED on Docker, and I am trying to write data to MySQL on my localhost.
As host, I am using localhost (see picture)
I receive the error: connection refused.
Which Host address should I use here? I don't quite understand the communication between the container and the local host.
localhost always points to the TCP/IP stack that the program opening the connection is bound to.
Every Docker container has it's own TCP/IP stack so unless the database is running in the same container as Node-RED localhost will not be the correct hostname for the database.
You have 3 choices
If you are using docker-compose then you can use the container name as the hostname and it will connect to the right container.
You can use the docker inspect [container-instance-name] to find the IP address assigned to the database container and use that
If you have mapped the container port to your host machine (with the -p option) then you can use the IP address of the host which normally defaults to 172.17.0.1
Edit:
To connect to the host machine then you can use host.docker.internal on Docker for Windows and Docker for Mac (unfortunately the issue to fix this on Linux has been open for over 3 years).
You should also be able to use the IP address from point 3 above.

MariaDB Docker cannot connect using local domain as host

I have an issue with my dockerized MariaDB. It happens when I try to connect to MariaDB.
Example with PhpMyAdmin: If I put MariaDB docker server's IP & port in the PHPMyAdmin config file (see picture), PhpMyAdmin can connect without any issue. But if I put the server hostname and the port, it doesn't connect. MariaDB doesn't accept the hostname. One of the problems I have is that the server change the IP, so it's an issue if I am not using the hostname. I checked that etc/mysql/my.cnf has #Bind 127.0.0.0 in docker container with #.
Also, the hostname works well in the browser (windows or linux).
Does anyone know how to fix this?
EDIT
I am also attaching the container info from Portainer.
Thanks

Can't reach MySQL hosted DB from Docker .NET Core

I’ve got a DigitalOcean Droplet that run Ubuntu and I’ve installed MySQL to it. I can reach it from my computer with SSH connection from MySQL workbench, so the remote access is Ok.
The hostname is 127.0.0.1
The bind address in my mysqld.cnf: 127.0.0.1
I’ve got a .NET Core API and I would like to use Docker to run it. I made the container push to docker hub and pulled it to the droplet. When I try to run I get this error:
An error occurred using the connection to database “ on server
‘127.0.0.1’.
My connection string:
"Server=127.0.0.1;Port=3306;Database=db;Uid=user;Pwd=pass”
I tried to server: localhost
IP address for docker0: 172.17.0.1 so I tried this to connectionString as well.
I don’t understand why can I connect to DB from MySQL workbench and cannot from .NET Core Web API.
The .NET Core version is 3.1.
Your .NET Core is inside container. Thus if you try to connect localhost or 127.0.0.1, it will try to connect the container itself, not the host. Since your MySQL is on your host, not inside the container, you can access it with other IP.
For example you have eth0 or enps0 or something like that, then that interface have IP 192.168.1.2, then you can use this IP as connection string.
Or, the alternate way, is using --network host when creating container. In this way, if you use 127.0.0.1, it will try to connect the host.

How to access remotely a dockerised database container in Ubuntu OS?

Current Situation :
In my company we are using a windows server, in which we have installed Docker Quickstart Terminal. We have made a mysql-container and made a DB inside. We access the DB with python apps in remote pcs by using the server's IP in the code (host argument):
connx = mysql.connector.connect(user='root', password='somepass', host='192.XXX.XX.XX', port=3306, database='db_name', auth_plugin='mysql_native_password')
but we used port-forward in the server-side to access the dockerised DB, thus the packets are forwarded to 192.168.99.100 (default docker IP)
Future Situation:
The company has decided to change the server and use Ubuntu instead (v18.04 i think). Unfortunately i have very little experience with linux and could not find a simple answer as the following online :
'Which IP should we use on the host argument above ?'
Docker installation on linux does not seem to install a VM, so will the new server's IP be enough to access the dockerised IP remotely ?
PS : we will probably do a 'docker run -p 3306:3306 mysql:latest' command on the server to expose the ports
You should be able to access the database the say way you were able to access in Windows Server, i.e., using the IP of the Ubuntu host machine and port forwarding the containerized database port with the host port.

Howto link a prestashop docker to an existing mysql server

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.