I want to run several WordPress websites with one common database using dockers.
Is it possible to specify a database and install the corresponding volume in a specific sql file to initialize WordPress for each container in the file docker-compose.yml?
For example, I have three files docker-compose.ymlfor a shared container, siteA and siteB.
When I started docker-compose upin ./shared, two databases will be created for two sites (example_a and example_b).
And when I run docker-compose upin ./siteA, I want to change the current database to example_aand initialize the site with a certain amount of sql data combined with ./siteA/mysql/setup.sql.
The same thing happens with the site.
I know that I can specify the database and volumes as -
WORDPRESS_DB_NAME: example_aand - ./db-data/mysql.dump.sql:/docker-entrypoint-initdb.d/install_wordpress.sqlunder the mysql docker-compose.yml, but I have only one common mysql and I can not specify the DB and that for each site.
I have several files that docker-compose.ymllook something like this.
./common/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