1.databaseという名のNetworkを新規作成する:
docker network create \ --driver=bridge \ --attachable \ database
2.MariaDB データコンテナと管理コンテナ phpMyAdminを作成する:
version: "2.1"
# Creates MariaDB container and phpMyAdmin service to manage it
services:
db:
image: mariadb:latest
container_name: db
hostname: db
restart: always
networks:
database: {}
environment:
MYSQL_ROOT_PASSWORD: YOURPASSWORD
volumes:
# Will use db volume and inside to path /var/lib/mysql
- db:/var/lib/mysql
phpmyadmin:
# Depends on only works within same stack, but not between remote
containers (even on user-defined network)
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
networks:
database: {}
container_name: phpmyadmin
hostname: phpmyadmin
ports:
# Exposes external port 7000 mapped to internal of 80
# Database required no external port of its own
- 7000:80
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: YOURPASSWORD
# Variable to increase upload limit for importing sql db files
UPLOAD_LIMIT: 200000K
volumes:
# Creates persistent storage volume for database
# Specify name so it does not append stack name in front
db:
name: db
networks:
database:
external: true
3. データベースコンテナを共有するDockerコンテナを作成する:
3-1:コンテナ作成前PhpAdmin経由でそれぞれYAML記述と同名のデータベースを作成する必要がある。
3-2:それぞれのdocker composeファイルに下記コード追記する:
networks:
database: {}
networks:
database:
external: true
例:
- Nextcloud&Onlyoffice
- WordPress
- Photoprism
- NginxProxyManager