I was wondering if docker-compose has the ability to either start a service and down the service within the docker-compose file, or to take an existing service and restart it with more resources
Problem
The application needs to be started with one volume to initiate some stuff and then it can be started again with the other volumes and resources to be able to make a connection to the db.
This is just an example with a sonarqube docker-compose file, IT'S NOT THE REAL COMPOSE FILE
In order for the application to work with a database it needs to be started with one volume (sonar-app-extension-vol) to initiate some stuff.
At the same time, the database is going to be started.
As soon as the db is up and healthy the application can be started with all volumes, networks, ports, env vars, etc.
version: '3.8'
services:
app:
hostname: 'sonarqube'
image: 'sonarqube:10.0.0-community'
container_name: 'sonarqube-app'
restart: 'unless-stopped'
environment:
- SONAR_JDBC_URL=jdbc:postgresql://sonar-db:5432/sonarqube
- SONAR_JDBC_USERNAME=sonar-db-user
- SONAR_JDBC_PASSWORD=sonar-db-password
volumes:
- 'sonar-app-conf-vol:/opt/sonarqube/conf'
- 'sonar-app-data-vol:/opt/sonarqube/data'
- 'sonar-app-extension-vol:/opt/sonarqube/extensions'
- 'sonar-app-bundled-plugin-vol:/opt/sonarqube/lib/bundled-plugins'
networks:
- 'sonar-network'
- 'nginx-network'
ports:
- '9000:9000'
depends_on:
db:
condition: service_healthy
# This service is only for initializing in order for the
# sonarqube to work properly with the postgresql db
init:
image: 'sonarqube:10.0.0-community'
container_name: 'sonarqube-init'
restart: 'unless-stopped'
volumes:
- 'sonar-app-extension-vol:/opt/sonarqube/extensions'
networks:
- 'sonar-network'
- 'nginx-network'
db:
hostname: 'postgresql'
image: 'postgres:15'
container_name: 'sonarqube-db'
restart: 'unless-stopped'
environment:
- POSTGRES_USER=sonar-db-user
- POSTGRES_PASSWORD=sonar-db-password
- POSTGRES_DB=sonarqube
#- POSTGRES_HOST_AUTH_METHOD=trust
volumes:
- 'sonar-db-postgresql-vol:/var/lib/postgresql'
- 'sonar-db-postgresql-data-vol:/var/lib/postgresql/data'
networks:
- 'sonar-network'
healthcheck:
test: ["CMD-SHELL", "pg_isready"]
interval: 10s
timeout: 5s
retries: 5
networks:
sonar-network:
name: 'sonar-network'
nginx-network:
name: 'nginx-network'
volumes:
sonar-app-conf-vol:
name: 'sonar-app-conf-vol'
sonar-app-data-vol:
name: 'sonar-app-data-vol'
sonar-app-extension-vol:
name: 'sonar-app-extension-vol'
sonar-app-bundled-plugin-vol:
name: 'sonar-app-bundled-plugin-vol'
sonar-db-postgresql-vol:
name: 'sonar-db-postgresql-vol'
sonar-db-postgresql-data-vol:
name: 'sonar-db-postgresql-data-vol'
Tried
Inside the official documentation, I could not find something like this but I'm pretty sure there must be a workaround.
I tried to create an init service to initialize my volume (sonar-app-extension-vol) to connect to the db and it is working perfectly fine. But in the end, I have 3 containers up and running which is not the ideal case in terms of host resource management.
Expectation
The goal for me is that the docker-compose file produces two containers at the end and not three so I can use it for unattended installations.