I have tomcat WAR which I want to run inside a Docker having tomcat image. There is a jdbc java Code in the war which access my local mysql from the docker container. But Docker is not able to connect to the local mysql.
I am able to run my war in tomcat locally. I have changed my my.cnf file bind-address to *.
bind-address = *
How can I configure my jdbc url so that it can connect to local mysql inside the Docker. After googling I came to know about the docker.for.mac.host.internal command map to host ip inside docker. I am not sure how to use it.
You may connect using Unix Domain Socket by mounting:
volumes:
-"/var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock"
You will also need a compatible JDBC connector, e.g. https://stackoverflow.com/a/31891263/8622794
Related
I am using vessel to run my Laravel project.
As I understand it basically simply creates a put-together docker image of my laravel project with mysql and everything.
Now since its running in docker, is it possible for me to access the mySQL databases with datagrip or tableplus on my host machine?
You just need to expose a port via docker. If you dont,you will have port 3306 which can be accessed via the local containers but if you bind port 3307:3306 for instance you can connect to mysql locally on 3307
you will need to connect to MySQL server using the docker containers IP that can be found using docker inspect.
This can also help you:
https://towardsdatascience.com/connect-to-mysql-running-in-docker-container-from-a-local-machine-6d996c574e55
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.
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.
Banging my head at 2:30 a.m.
I have three Docker containers MariaDB, Nginx and PhpFpm.
MariaDB opens up 3306/tcp - no exposed port because all container share the same Docker Network.
Symfony starts up in PhpFpm and is exposed via Nginx on port 8081.
So myhost.com:8081 shows my Symfony application.
When I docker exec bash into my PhpFpm container and execute bin/console doctrine:schema:create schema is created, MariaDB has a new table.
Of course I clear the cache and warm it up for APP_ENV=prod
var/log and var/cache have set correct permissions.
No matter what I do, when using the PDO connection within my Symfony application's controller I get an
Connection refused error
visible in my prod.log within the container.
I tried .env with several options, most prominent are:
DATABASE_URL="mysql://user:pass#hostname:8084/db" (here I set -p 8084:3306 for MariaDB Container)
and
DATABASE_URL="mysql://user:pass#mariadbcontainername:3306/db" (here I did not expose any DB port as mentioned above)
Does anyone have an idea where to look and what to test?
I can even verify that my bin/console doctrine... commands are already getting a Connection Refused error when I change the DATABASE_URL to a nonsense value.
P.S.: Due to server restrictions I don't use docker-compose but simply docker run.
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.