'How can I configure 2 docker compose files such that they use an existing service if it's already been pulled?
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.
Solution 1:[1]
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.
TL;DR:
It appears you will only need to create (at least) three docker-compose.yml
files:
- That of the common database, say
mysql/docker-compose.yml
- That of the 1st webapp, say
app1/docker-compose.yml
- That of the 2nd webapp, and so on…
The crux of the approach is that:
- We rely on the so-called docker-compose feature of external networks,
- and we DON'T expose more ports than required… (see below for details: §)
Minimal working example
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
Side notes
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 asdepends_on:
, i.e., you'll need to manually rundocker-compose up -d
in themysql/
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…
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |