Connect docker container with external MySQL Database


I’m trying to connect my Nextcloud container to an external MySQL Database.

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 guess MySQL and Docker cannot communicate together, but I have no idea why. I (hopefully) changed the MySQL bind address to the IP Range\16 to connect them together, but I have no idea if this is the right way.

I attached my docker-compose.yml.

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
     - database
      - MYSQL_ROOT_PASSWORD=testrootpasswort
      - MYSQL_PASSWORD=testpasswort
      - MYSQL_DATABASE=admin_nextcloud
      - MYSQL_USER=admin_nextcloud
      - MYSQL_HOST=localhost
      - database

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


I have the same problem that my nextcloud container cannot connect to the local mysql database. Maybe we can find a solution for both of us.

I have plesk installed on my server and created a docker container with nextcloud. I tried to bind the mysql database server IP range to but it doenst work.

How is it possible to setup the docker container the right way, that the container can communicate with the localhost.

Hopefully somewone can help us.

From the perspective of the container, your external MySQL is not at localhost, but at the IP address of the system where you installed it.


I don’t get it. You have an external database, but you do start a MariaDB instance, that your nextcloud-app depends on, but you don’t want to access it??

If you want to access the MariaDB-container instance, the MYSQL_HOST is database or mariadb_container. If you have a database running on your host-machine, it is (as already mentioned), that machine ip.

I tried localhost and but I get always this error:

“Error while trying to create admin user: Failed to connect to the database: An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused”

You didn’t read my answer, didn’t you? is localhost.

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.

So please try to use database or mariadb_container as the MYSQL_HOST.

If you look at Docker Hub, 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.

OP did you ever solve connecting the database to the Docker container?

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.