I have a maria-db database docker-compose.yml:
version: '3.8'
services:
mariadb:
image: mariadb:latest
container_name: mariadb
restart: always
environment:
MYSQL_ROOT_PASSWORD: admin
MYSQL_DATABASE: "wordpress3,wordpress4"
MYSQL_USER: mohsin
MYSQL_PASSWORD: 12345678
volumes:
- mariadb_data:/var/lib/mysql
networks:
- wordpress_network
networks:
wordpress_network:
external: true
name: wordpress_network
volumes:
mariadb_data:
I am running two wordpress docker-compose files (for testing purposes). I want to connect both of them to the mariadb docker-compose. Both files have similar config:
version: '3.8'
services:
wordpress:
image: wordpress:latest
container_name: wordpress3
restart: always
environment:
WORDPRESS_DB_HOST: mariadb
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: admin
WORDPRESS_DB_NAME: wordpress3
ports:
- "8083:80"
depends_on:
- mariadb
networks:
- wordpress_network
mariadb:
extends:
file: ../mariadb/docker-compose.yml
service: mariadb
networks:
wordpress_network:
name: wordpress_network
external: true
When I run the first wordpress docker-compose, it automatically runs mariadb and connects to it. At the same time, the other instance throws an error saying mariadb already in use
. I want it to recognize that mariadb is already running, and connect to the shared instance automatically?
Two instances access the same db service type of scenario is required (condition: auto runs db service if down)
Tried using extend and .env but doesn’t work.
2
Answers
You need to make sure that the containers you want to talk to each other are on the same network.
for example:
Note: Your app’s network is given a name based on the “project name”, which is based on the name of the directory it lives in, in this case a prefix first_ was added
It is not logically correct to create two containers for one db, for maria db you have already created a container in your first file, as your two docker services are running in a single network, you can just connect to it.
mariadb already in use
this means that this container name has already taken in your network, so network has this container already, you can change container name, you don’t have to create two containers if you want them to be indentical, just use one! .So the conclusion is
mariadb_new
.