Docker-Compose – Changing Port of MariaDB in Docker-Compose

docker-composemariadbWordpress

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

Best Answer

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

MYSQL_TCP_PORT: ${DB_PORT}
MYSQL_UNIX_PORT: ${DB_PORT}
Related Topic