1
votes

So I am new to docker and this is the first project I have tried to run using it. I am running into the issue of my php-apache container is not able to connect to my mysql container, but only in the browser. I can do:

docker exec -it <php container id> bash

and then run

composer install

and all of the

php bin/console doctrine:<>

commands just fine with no database errors or connection problems. When I try to actually load up the site in my browser I am met with the "SQLSTATE[HY000] [2002] Connection refused" error. I am not really sure why this is the case and no results I have found on the web seem to work. Here are my files:

docker-compose.yml

version: '3'
services:
  db:
    restart: always
    container_name: hcp-db
    build:
      context: ../
      dockerfile: docker/db/Dockerfile
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
      - MYSQL_DATABASE=symfony
      - MYSQL_USER=root
      - MYSQL_PASSWORD=password
    volumes:
      - db:/var/lib/mysql
    expose:
      - 3306
    ports:
      - "4306:3306"
  application:
    container_name: hcp-symfony
    build:
      context: ../
      dockerfile: docker/Dockerfile
    working_dir: /app
    expose:
      - 80
    ports:
      - 8000:80
    depends_on:
      - db
    volumes:
       - ../:/app
    environment:
      - SYMFONY_ENV=dev
      - SYMFONY_DEBUG=1

volumes:
  db:

parameters.yml

parameters:
    database_driver: pdo_mysql
    database_host: db
    database_port: 3306
    database_name: symfony
    database_user: root
    database_password: null

I am also able to connect to the database just fine through Sequel Pro while my container is running using the same credentials I provide in the parameters, but with the host as "127.0.0.1", which I find interesting.

Any help with what could be wrong would be much appreciated.

1
According to your docker-compose.yml: MYSQL_PASSWORD=password but in your application config it says: database_password: null. I would expect it to be database_password: password.dbrumann
Tried that, no dice. That would mean I wouldn't be able to connect via command line. The error would also be something related to the password and not just a "Connection refused"rwinkle4
Do you use Dotenv component? If you do, why use parameters file? If not, why do you specify SYMFONY_ENV?Jovan Perovic

1 Answers

1
votes

Sup, I can't comment yet to ask a bit more of details since I'm kind of new so I will take a wild guess and assume there is no issues on your app.

This kind of issue is usually related to firewall stuff. Docker does a lot of firewall configuration for us so we don't have to deal with the ufw or IP tables ourselves, but that also means that we need to specify how each container will interact with each other ourselves withing our docker compose file.

You have to specify that your app container links to the database container in order to allow hostnaming access since docker modify the container /etc/hosts file everytime a container starts so it maps each container current IP, like this:

services:
  ...
  application:
    ...
    depends_on:
      - db
    links:
      - db

With that done, you just need to make sure your parameters file is configured correctly as @dbrumann pointed out, the database_password should contains your password and not a null value.

It is also a good practice to create a custom network for your docker project so you can have a nice private lan between your containers without being open to any other container using docker's default network, a custom network with defaults can do nicely, like this:

services:
  db:
    ...
    networks:
      - my_project_network
  application:
    ...
    depends_on:
      - db
    links:
      - db
    networks:
      - my_project_network

networks:
  my_project_network:

You don't really need to expose your database port to the host for this to work but having it exposed makes development way easier when checking the database so just remember to remove the database container ports statement when going to production environment to avoid security issues.

Hope this helps, here is a link to docker network documentation in case you want to learn more: https://docs.docker.com/compose/compose-file/#network-configuration-reference