Connecting a Docker container to a remote MySQL Database

Asked
Active3 hr before
Viewed126 times

7 Answers

connectingdockercontainer
90%

I've set up a Docker container for the app using Centos 6.x, but can't get the MySQL connection working. The container has MySQL installed and the mysqld running.,With the container running, I can execute the following command (outside the container) to show databases on the remote DB:,The Docker works like a virtual machine. It has a local storage and a local environment. When you connect to 127.0.0.1 from the Docker it tries to connect to this Docker (not to local machine where the Docker was runned) because the localhost for the Docker is the Docker.,I'm working to containerize a Django 1.5.x application that connects to a MySQL DB on a separate server via ODBC:

I'm working to containerize a Django 1.5.x application that connects to a MySQL DB on a separate server via ODBC:

[mysql_default]
database = DB_NAME
driver = /usr/lib64 / libmyodbc5.so
server = REMOTE_DB_SERVER
user = DB_USER
password = DB_USER_PWD
port = 3306
load more v
88%

From a container perspective, localhost or 127.0.0.1 always belong to the containers virtual network interface, just as it would be use on any physical host. All containers get its own local ip and a host name that can be used to resolve the route to the container. If you expose a port, like you do with the web service, you bind the internal port (80 in your case) with the host machine port (8080). If you don’t expose it to the host machine, it will still be reachable from other services in the same stack of network using the service name or host name.,If you look at https://hub.docker.com/_/mariadb, they connect adminer to a mariadb without any other configuration, since adminer is looking for the exposed db-port on all found containers in the stack.,I can create the container with docker-compose and also access the setup page from Nextcloud, but I’m not able to connect to my existing database which I created with VESTAcp.,I was able to solve this same issue simply by adding docker backend exe to windows firewall (Linux would be similar). https://arnav.jain.se/2019/allow-docker-through-windows-firewall/ You probably need to add port to your DB service as well. I use python and Jupyter notebook to connect to MariaDB, but any compatible tool should work. Hope that helps. I hope to post a note book on the subject soon.

Thanks for helping!

version: '3'

volumes: # Macht Verzeichnis vom Host für Container verfügbar
nextcloud:
   database:

   services:
   database:
   image: mariadb
restart: always
container_name: mariadb_container
volumes:
   -/var/lib / mysql /: /var/lib / mysql /

   app:
   image: nextcloud: fpm - alpine
restart: always
volumes:
   -nextcloud: /var/www / html
container_name: app_container
links:
   -database
environment:
   -MYSQL_ROOT_PASSWORD = testrootpasswort -
   MYSQL_PASSWORD = testpasswort -
   MYSQL_DATABASE = admin_nextcloud -
   MYSQL_USER = admin_nextcloud -
   MYSQL_HOST = localhost
depends_on:
   -database

web:
   build: . / web
container_name: web_container
restart: always
ports:
   -8080: 80
volumes:
   -nextcloud: /var/www / html: ro
depends_on:
   -app
load more v
72%

2) Use a user-defined network (Best practice and recommended option). MySQL client-server example:,1) Link them together using --link. MySQL client-server example:

# First check remote access
$ telnet[MYSQL_REMOTE_HOST][MYSQL_REMOTE_PORT]

# Finally Run the container
$ docker run - it mysql - h[MYSQL_REMOTE_HOST] - P[MYSQL_REMOTE_PORT] - u[MYSQL_REMOTE_USER]--password = [MYSQL_REMOTE_PASSWORD][MYSQL_REMOTE_DATABASE]
load more v
65%

docker container run - d - p 3306: 3306--name mysql--env MYSQL_ROOT_PASSWORD = 123456 mysql

docker container exec - it mysql bash
75%

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2 “No such file or directory”),I found this by searching, check if it helps, Connecting MariaDB docker instance from MySQL workbench,How can i access mysql/mariadb in docker installatiom production mode,Once that is done, you can access mariadb on docker host on port 3306

docker exec -it <mariadb-container-name> bash

Then enter into Mariadb shell using root password

mysql - uroot - p
load more v
40%

Start the Bitnami Ghost Docker container using the same configuration.,Confirm that you can connect to this database locally. I used the mysql-client to confirm this., Confirm that you can connect to this database locally. I used the mysql-client to confirm this. , The text was updated successfully, but these errors were encountered:

mysql\
   -
   h $MARIADB_HOST\ -
   u $GHOST_DATABASE_USER\
   --password = $GHOST_DATABASE_PASSWORD\ -
   D $GHOST_DATABASE_NAME\ -
   P $MARIADB_PORT_NUMBER
load more v
22%

VPS server has mysql container runig (I dont expose 3306 port to the host). How can I can connect to that dockerized mysql server from my laptop without mounting mysql port to the host?,And then I've tried joining mysql container to it:,Reasoning: Why I want it this way? Because I dont want to make it (mysql server) visible to entire internet and thus attackers, but at same time i want easy access to mysql data from my laptop.,But I only get an error: root@vps:~$ sudo docker network connect infranet2mysql-server Error response from daemon: failed to create the macvlan port: invalid argument

What you can do is you can expose your port (3306) only on the internal ip, accessible via wireguard, such as the tunnel ip (as opposed to exposing it on all ips (0.0.0.0/0), as it happens by default). Given your tunnel ip is 192.168.0.1, this is how you would do it:

docker run--name mysql - server\ -
   p 192.168 .0 .1: 3306: 3306\ -
   e MYSQL_ROOT_PASSWORD = my - secret - pw\ -
   d mysql

Other "connecting-docker" queries related to "Connecting a Docker container to a remote MySQL Database"