Creating 2 wordpress container with 1 database

Hello. I’m new to docker and trying to learn. I’m wondering if it’s possible to run 2 different wordpress containers with different contents using 1 db service, ideally by modifying docker-compose.yml example file from hub.docker.com

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

  db:
    image: mysql:8.0
    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:

Yes, of course. You just need to choose different WORDPRESS_DB_NAME for both wordpress services, but then you need to precreate those before you start the wordpresse. Check the image description and you will find WORDPRESS_TABLE_PREFIX which can be changed and you can have multiple wordpresses in the same database.

https://hub.docker.com/_/wordpress

2 Likes