Establish database connection from docker wordpress to external database server

Hi All, lately i try to learn the docker technology by dockerize my existing wordpress website. However, i found out all the tutorial out there are creating the database in the docker rather then connect to external database. I Done some research on docker network host, but still no ideal how to adapt it into my scenerio. For learning purpose, could anyone guide me on how to configure my docker-compose.yml to establish database connection to my wordpress website? below is my docker-compose.yml

version: ‘3.1’

services:

wordpress:
image: wordpress
restart: always
ports:
- 8080:80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
volumes:
- ./wordpress:/var/www/html

Hi

The variable you need to change is the WORDPRESS_DB_HOST one, as you state, the current configuration is that there would be a container named “db” in the same network that it can use.

If you have an external, like, not the same server as docker is running on, you can simply change WORDPRESS_DB_HOST to an IP or host of the database server.

If the database is hosted on the same server, you have a couple of options:

  1. Set WORDPRESS_DB_HOST to the IP of the server
  2. Get the gateway IP of your docker network, and use that (the gateway ip will be the host)
  3. set “network_mode” to “host”, in short, this means that the container will run as if it was a normal service on your server, meaning that you then can set WORDPRESS_DB_HOST to 127.0.0.1

Hi,

thx for your solutions, it work for me however i set my docker network to “bridge” in order to connect my database through the private IP address.

1 Like

Can you share your docker stack (compose) so that I can replicate the same. Mine is in bridge mode but somehow Wordpress can’t reach the db :frowning:

version: '3.1'

services:

  wordpress:
    image: wordpress:latest
    restart: always
    ports:
      - 8085:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
      WORDPRESS_CONFIG_EXTRA: |
        define('AUTOMATIC_UPDATER_DISABLED', false);
    volumes:
      - wordpress:/var/www/html

  db:
    image: mysql:latest
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql

volumes:
  wordpress:
  db:

Based my modification on your code, i create a network call wordpress-vpn in bridge method, after that, i adding

networks:
- wordpress-vpc

in each of your container to let them know you gonna apply this network for them.

you may try this and see is it work or not

hmm somehow still did not solve. I can see that they are in a bridge network. Ip’s are all good. But still Wordpress can’t connect to database of MySQL somehow.

version: '3.1'

services:

  wordpress:
    image: wordpress:latest
    restart: always
    ports:
      - 8085:80
    links:
      - db
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
      WORDPRESS_CONFIG_EXTRA: |
        define('AUTOMATIC_UPDATER_DISABLED', false);
    volumes:
      - wordpress:/var/www/html
    networks:
      - wordpress-vpc

  db:
    image: mysql:latest
    restart: always
    expose:
     - 3306
     - 33060
#    ports:
#      - 3306:3306
#      - 33060:33060
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql
    networks:
      - wordpress-vpc

volumes:
  wordpress:
  db:
  
networks:
  wordpress-vpc:
    driver: bridge
version: '3'
services:
  mysql:
    image: 'mariadb:latest'
    container_name : wordpress-sql
    volumes:
      - './data/mysql:/var/lib/mysql'
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wpadmin
      MYSQL_PASSWORD: wpadminpass
    restart: unless-stopped
    networks:
      - wordpress-vpc
  wordpress:
    image: 'wordpress:5.9.3-php8.1-fpm-alpine'
    container_name : wordpress-php
    volumes:
      - './data/html:/var/www/html'
    depends_on:
      - mysql
    environment:
      WORDPRESS_DB_HOST: mysql
      MYSQL_ROOT_PASSWORD: root
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: wpadmin
      WORDPRESS_DB_PASSWORD: wpadminpass
      WORDPRESS_TABLE_PREFIX: wp_
    links:
      - mysql
    restart: unless-stopped
    networks:
      - wordpress-vpc
  nginx:
    image: 'nginx:1.21.6-alpine'
    container_name : wordpress-nginx
    volumes:
      - './data/nginx:/etc/nginx/conf.d'
      - './data/html:/var/www/html'
    ports:
      - '80'
    links:
      - wordpress
    restart: unless-stopped
    networks:
      - wordpress-vpc
networks:
  wordpress-vpc:
    driver: bridge

let me share my docker compose, hopefully it can help you to resolve your issue