Reputation: 550
I frequently swap between different apps which use Docker, some of which are microservices that need to both be operational so I can't simply kill all of my containers. If they share a service like mssql or redis, I run into issues with port reservation. I could set them up to use their own ports, sure. But I'd like to reuse the same instance of the service if it already exists as my dev machine is not all that powerful. The docker files should each create their own databases, so everything is already as isolated as I'd like it to be.
Upvotes: 2
Views: 2018
Reputation: 14723
The use case looks feasible, but not really standard (as there's some risk at first sight, that the databases of the different apps get entangled!) but let's say it's OK for a dev (≠ prod) environment.
It appears you will only need to create (at least) three docker-compose.yml
files:
mysql/docker-compose.yml
app1/docker-compose.yml
The crux of the approach is that:
File mysql/docker-compose.yml
:
services:
db:
image: mysql:8
command: --default-authentication-plugin=mysql_native_password
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: root_password
volumes:
- mysql-data:/var/lib/mysql
# don't include ports:
networks:
- mysql-network
volumes:
mysql-data:
driver: local
networks:
mysql-network:
driver: bridge
# override the name
name: mysql-network
File app1/docker-compose.yml
:
services:
app1:
image: phpmyadmin
restart: unless-stopped
environment:
PMA_HOST: db
networks:
- mysql-network
ports:
- '8080:80'
networks:
mysql-network:
external:
name: mysql-network
Of course, as the backends and the common database are in separated docker-compose.yml
specifications, you won't be able to benefit from some fields such as depends_on:
, i.e., you'll need to manually run docker-compose up -d
in the mysql/
folder beforehand.
(§) even if this use case only targets a dev environment, as usual, one should avoid to expose ports in the db
services, i.e., DON'T write:
db:
image: mysql:8
ports:
- '3306:3306'
...
as (i) this is not necessary for the Compose services to communicate: they just need to belong to a common Compose network and use the Compose service hostname, and (ii) exposing one such port directly on the host would needlessly increase the attack surface…
Upvotes: 3