0

Good morning,

I have 2 docker container configured, with the same Dockerfile, but different ports, see configuration

mariadb:
  container_name: mariadb
  image: project/mariadb
  build:
    context: .
    dockerfile: ./docker/mariadb/Dockerfile
  environment:
    MYSQL_ROOT_PASSWORD: "$MYSQL_ROOT_PASSWORD"
    MYSQL_DATABASE: "$MYSQL_DATABASE"
    MYSQL_USER: "$MYSQL_USER"
    MYSQL_PASSWORD: "$MYSQL_PASSWORD"
  command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']
  ports:
    - "3306:3306"
  volumes:
    - mysql_data:/var/lib/mysql
    - ./services/mysql/utf8mb4.cnf:/etc/mysql/conf.d/utf8mb4.cnf:ro

mariadb_test:
  container_name: mariadb_test
  image: project/mariadb
  build:
    context: .
    dockerfile: ./docker/mariadb/Dockerfile
  environment:
    MYSQL_ROOT_PASSWORD: "admin"
    MYSQL_DATABASE: project_test
    MYSQL_USER: foo
    MYSQL_PASSWORD: bar
  command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']
  ports:
    - "3317:3306"
  volumes:
    - ./services/mysql/utf8mb4.cnf:/etc/mysql/conf.d/utf8mb4.cnf:ro

I can connect to mariadb (Port:3306) Container without any problems, Parameters are saved in .env:

mysql://$MYSQL_USER:$MYSQL_PASSWORD@$MYSQL_HOST:$MYSQL_PORT/$MYSQL_DATABASE

But if I try to connect to mariadb_test I get a

SQLSTATE[HY000] [2002] Connection refused

even if I hardcode the connection params to:

mysql://foo:bar@mariadb_test:3317/project_test

I'm very frustrated...What am I doing wrong?

Kat
  • 31
  • 1
  • 8
  • Why are you rebuilding the container `./docker/mariadb/Dockerfile`? What is the `mysql://...` url being used by? Is it another container on the same network? You can also pass `--port=3317` as an argument to `mariadb_test` – danblack Feb 15 '22 at 10:45
  • I thought it’s the correct way for Unit Testing to have a separate container for the test database. Isn’t it ? – Kat Feb 15 '22 at 10:55
  • The url is used by symfony to connect to the database. Where should I pass the port ? – Kat Feb 15 '22 at 10:56
  • Its decent to have a separate container for testing. Your port passing [appears to be correct](https://symfony.com/doc/current/doctrine.html) in the url. Try appending `--port=3317` on the `command:` and doing a 3317:3317 mapping. Also consider looking at the container logs for `mariadb_test`, – danblack Feb 15 '22 at 11:06
  • 4
    Connections between containers always use the "normal" ports; they ignore `ports:` remappings. Use the standard MySQL port 3306 in the database connection string. – David Maze Feb 15 '22 at 11:36
  • @DavidMaze crazy! Thank you so much. It’s working – Kat Feb 15 '22 at 11:42
  • See https://stackoverflow.com/questions/71230710/cant-connect-to-local-mariadb-running-with-docker-with-php-pdo – bolino Feb 28 '22 at 08:47

1 Answers1

2

Thanks to @David Maze who answered my question in a comment:

Connections between containers always use the "normal" ports; they ignore ports: remappings. Use the standard MySQL port 3306 in the database connection string.

Kat
  • 31
  • 1
  • 8