Kat
Kat

Reputation: 51

Connection refused: MariaDB in Docker

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?

Upvotes: 1

Views: 5255

Answers (1)

Kat
Kat

Reputation: 51

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.

Upvotes: 3

Related Questions