Docker compose: run stack dynamically
I use docker compose every day for my local development needs.
During the day I might turn on/off various databases or servers thus I need to do it fast and in a managed way.
Usually your docker-compose files contains the configuration for many containers, network, volumes etc.
stack.yaml
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 | version: '3.5' services: mongo: image: mongo restart: always ports: - 27017 : 27017 environment: MONGO_INITDB_ROOT_USERNAME: username MONGO_INITDB_ROOT_PASSWORD: password mongo - express: image: mongo - express restart: always ports: - 8081 : 8081 environment: ME_CONFIG_MONGODB_ADMINUSERNAME: username ME_CONFIG_MONGODB_ADMINPASSWORD: password |
This works if you always want the same services up and running.
However it does have a cost on resources, and most of the times you don’t need the full stack.
What you can do in this cases, would be to split them into files and choose what to use.
mongo.yaml
01 02 03 04 05 06 07 08 09 10 11 | version: '3.5' services: mongo: image: mongo restart: always ports: - 27017 : 27017 environment: MONGO_INITDB_ROOT_USERNAME: username MONGO_INITDB_ROOT_PASSWORD: password |
express.yaml
01 02 03 04 05 06 07 08 09 10 11 | version: '3.5' services: mongo - express: image: mongo - express restart: always ports: - 8081 : 8081 environment: ME_CONFIG_MONGODB_ADMINUSERNAME: username ME_CONFIG_MONGODB_ADMINPASSWORD: password |
Then choosing what to use becomes very easy, just omit the file
1 | docker-compose -f mongo.yaml -f express.yaml up |
Published on Web Code Geeks with permission by Emmanouil Gkatziouras, partner at our WCG program. See the original article here: Docker compose: run stack dynamically Opinions expressed by Web Code Geeks contributors are their own. |
I think same can be used with the below command if we have multiple services in single docker compose file.
docker-compose run <service-name>