I want to run multiple WordPress websites with one shared database using docker.
Is it possible to specify a database and set an appropriate volume to a certain sql file to initialize WordPress for each container in its docker-compose.yml file?
For example, I have three docker-compose.yml files for a shared container, siteA and siteB.
When I run docker-compose up in ./shared, two DBs will be created for the two sites (example_a and example_b).
And when I run docker-compose up in ./siteA, I want to change current DB to example_a, and initialize the site with a certain amount of data by sql volumed from ./siteA/mysql/setup.sql.
Same thing goes with siteB.
I know I can specify a database and volume like -
WORDPRESS_DB_NAME: example_a and - ./db-data/mysql.dump.sql:/docker-entrypoint-initdb.d/install_wordpress.sql in mysql section in docker-compose.yml but I only have one shared mysql and cannot specify DB and volume for each site.
I have multiple docker-compose.yml files look something like below.
./shared/docker-compose.yml
version: "2"
services:
proxy:
image: jwilder/nginx-proxy
privileged: true
container_name: proxy
ports:
- 80:80
- 443:443
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
- ./certs:/etc/nginx/certs:ro
restart: always
logging:
options:
max-size: 5m
max-file: "10"
mysql:
image: mysql:5.7
container_name: mysql
command: >
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--max-allowed-packet=128M
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=foobar
restart: always
volumes:
- db-data:/var/lib/mysql
- ./mysql/sql-setup.sql:/docker-entrypoint-initdb.d/sql-setup.sql # to create multiple databases (e.g. example_a, example_b)
logging:
options:
max-size: 5m
max-file: "10"
volumes:
db-data:
driver: local
networks:
default:
external:
name: shared
./siteA/docker-compose.yml
version: "2"
services:
example_a_wordpress:
image: wordpress
container_name: a.example
environment:
WORDPRESS_DB_NAME=example_a
WORDPRESS_DB_PASSWORD=foobar
VIRTUAL_HOST: a.example.dev
external_links:
- mysql
restart: always
volumes:
- ./dist/theme:/var/www/html/wp-content/themes/main
- ./dist/assets:/var/www/html/assets
logging:
options:
max-size: 5m
max-file: "10"
networks:
default:
external:
name: shared
./siteB/docker-compose.yml
version: "2"
services:
example_b_wordpress:
image: wordpress
container_name: b.example
environment:
WORDPRESS_DB_NAME=example_b
WORDPRESS_DB_PASSWORD=foobar
VIRTUAL_HOST: b.example.dev
external_links:
- mysql
restart: always
volumes:
- ./dist/theme:/var/www/html/wp-content/themes/main
- ./dist/assets:/var/www/html/assets
logging:
options:
max-size: 5m
max-file: "10"
networks:
default:
external:
name: shared