Can not connect docker container with external MySQL Database

Hi,

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

that “localhost” is the container itself. not the host where docker is running.

and your mysql must be configured to accept connections from other networks.

Thanks for the reply.

MySQL has a socket only but no IP, how can add the socket in my docker-compose.myl?

if you use a mysql on your host, you can remove this from your docker compose file. it would launch another mariadb as a container. just eating some resources. it could be also a problem because if the mysql on the host uses /var/lib/mysql/ as it’s database dir both dbs would use the same files.

if your mysql is using /var/run/mysqld/mysqld.sock as the socket file you have to bind it to your nextcloud container.

something like this. not sure if it will work.
maybe it easier to use the mariadb container. :wink: