1

I run a number of WordPress sites using docker-compose (and nginx-proxy). So I can use the same docker-compose file for each site I use .env. I want each of the MariaDB containers to use different ports (as they are sharing the same external docker network).

What I have is the below compose file but when I bring it up I get.

MySQL Connection Error: (2002) Connection refused

Previously I was using the same compose file without the ports: section and with the port hardcoded in the WordPress section and it worked.

Where did I go wrong?

docker-compose.yml

version: '3'

services:
  db:
    image: mariadb
    container_name: ${DB_CONTAINER}
    hostname: ${DB_CONTAINER}
    ports:
      - ${DB_PORT}:3306
    volumes:
      - ./db:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: ${DB_WP_PASSWORD}

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    restart: always
    container_name: ${WP_CONTAINER}
    hostname: ${WP_CONTAINER}
    volumes:
      - ./html:/var/www/html
    expose:
      - 80
    restart: always
    environment:
      VIRTUAL_HOST: ${DOMAINS}
      LETSENCRYPT_HOST: ${DOMAINS}
      LETSENCRYPT_EMAIL: ${EMAIL}
      WORDPRESS_DB_HOST: db:${DB_PORT}
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: ${DB_WP_PASSWORD}

networks:
  default:
    external:
            name: nginx-proxy

.env

DB_CONTAINER=test_click_db
WP_CONTAINER=test_click_wp
DB_PORT=13306
EMAIL=bene@domain.com
DOMAINS=test.click.tvpp.tv
DB_ROOT_PASSWORD=aabbcc
DB_WP_PASSWORD=xxyyzz
Ben Edwards
  • 301
  • 3
  • 12

2 Answers2

2

Actually I worked it out the db service needed the following added to environment:

MYSQL_TCP_PORT: ${DB_PORT}
MYSQL_UNIX_PORT: ${DB_PORT}
Ben Edwards
  • 301
  • 3
  • 12
  • 2
    Thanks for this. Where did you find this information? I don't think it's on the list of env vars declared on the official `mariadb` docker page > https://hub.docker.com/_/mariadb – David The Programmer Sep 16 '20 at 09:31
  • 2
    @DavidTheProgrammer I'm a little late to the party, but it's from https://mariadb.com/kb/en/mariadb-environment-variables/ - Ben, you should accept your own answer with this information added to it :) – Maritim Sep 05 '21 at 14:08
1

This line

WORDPRESS_DB_HOST: db:${DB_PORT}

should be changed to

WORDPRESS_DB_HOST: db:3306

(I guess this actually is what you did previously) The wordpress container will then connect directly to the mariadb container at port 3306 of the container. The db part is resolved by docker network's internal DNS server to the IP address of your mariadb container.

You don't need this part

ports:
  - ${DB_PORT}:3306

It means that docker will map the ${DB_PORT} on your host machine to port 3306 in the service (container), thus you can also reach the DB by connecting to the host machine at port ${DB_PORT}. It's generally only for when you intend to connect to the container from outside the host machine.

lenin
  • 71
  • 3