MySQL Connection access denied, but only 50% of the time, in Docker with Nginx-Proxy

166 Views Asked by At

I'm trying to get a local development environment running with multiple projects. After some heavy tinkering I stumbled across this project , and started adapting it to my own projects. Each project consist of 2 containers, a mysql container and a php container (from the php-apache image).

The nginx-proxy is a separate container running on a network. I renamed the project slightly, so here it's called dev-router_default.

My project's compose.yml looks like this:

version: "3"
services:
  php:
    build: './.docker/php/'
    volumes:
      - .:/var/www/html/
    environment:
      - VIRTUAL_HOST=XXX.local
  mysql:
    build: './.docker/mysql/'
    ports:
      - "3309:3306"
    environment:
      MYSQL_DATABASE: XXX
      MYSQL_USER: XXX
      MYSQL_PASSWORD: XXX
      MYSQL_ROOT_PASSWORD: XXX
      VIRTUAL_HOST: XXX.local
networks:
  default:
    external:
      name: dev-router_default

The dockerfiles in those folders are just basic images right now. They just exist so I can scale them up later.

Everything works pretty smooth. I can access the DB, I can load html and php pages. However. Whenever I load a page. I have a 50% chance of getting this error:

SQLSTATE[HY000] [1045] Access denied for user

The other 50% of pageloads work just fine. This isn't a wrong password issue or anything. The settings all stay the same, but somehow one connection is refused and the other one goes through.

I have a gut feeling that nginx-proxy isn't redirecting my traffic to the correct mysql container half the time. But I can't seem to find where the calls are sent to. When I load a page I see in my container CLI the log popup for the php_1 load. But mysql_1 stays silent.

Does anybody have any idea what is causing this inconsistency?

Is there a simpler way to set up a development environment with docker that makes use of multiple virtual hosts/domains at the same time?

I need to be able to access multiple databases (I do this by giving every project a specific port to go through right now), and have a unique url for each project tied to a document_root.

1

There are 1 best solutions below

0
On BEST ANSWER

The issue here was the name of the service. All projects have a php and a mysql container. I access the mysql HOST through the name of the service (which in this case is mysql for all projects). The HOST "mysql" was defined multiple times in multiple projects. Naming the mysql container projectname-mysql fixed it.