Connecting a Docker container to a remote MySQL Database

Active3 hr before
Viewed126 times

7 Answers


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 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:

database = DB_NAME
driver = /usr/lib64 /
user = DB_USER
password = DB_USER_PWD
port = 3306
load more v

From a container perspective, localhost or 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, 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). 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

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

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

   build: . / web
container_name: web_container
restart: always
   -8080: 80
   -nextcloud: /var/www / html: ro
load more v

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

# Finally Run the container
load more v

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

docker container exec - it mysql bash

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

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:

   --password = $GHOST_DATABASE_PASSWORD\ -
load more v

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 (, as it happens by default). Given your tunnel ip is, 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"