5

In my docker-compose.yml file I have plenty of services:

  • nginx
  • mysql
  • redis
  • echo
  • php
  • ...

But also some CLI utilities that I use with docker-compose run --rm:

  • artisan
  • composer
  • npm
  • ...

When I start my system I do docker-compose up. Unfortunately this also try to start all the CLI utilities. Is there a way to separate these two categories in my docker-compose?

4
  • Does this answer your question? docker-compose up for only certain containers Commented Jul 23, 2020 at 9:37
  • @jonrsharpe Not really... I do not want the user to know what to start or even create a script only to start certain containers... Commented Jul 23, 2020 at 9:38
  • Docker compose is usually a dev tool - who is the user here? What are your constraints? What have you already considered and dismissed, and why? Commented Jul 23, 2020 at 9:39
  • 1
    Those "CLI utilities" sound like steps that should get run in your images' Dockerfiles, not things that would want separate containers. How and when would you use a standalone npm container? You can also docker-compose run commands based on your existing services, which might let you take these utility blocks out of your Compose setup. Commented Jul 23, 2020 at 10:38

1 Answer 1

11

Update

Starting with docker-compose 1.28.0 the new service profiles are just made for that! With profiles you can mark services to be only started in specific profiles:

services:
  nginx:
    # ...
  mysql:
    # ...
  composer:
    profiles: ["cli-only"]
    # ...
    # ...
  npm:
    profiles: ["cli-only"]
    # ...
docker-compose up # start main services, no composer and no npm
docker-compose run --rm composer
docker-compose run --rm npm

original answer

Unfortunately there is currently no convenient way to do this. The officially recommended way to do it is to separate your commands into its own docker-compose.yml:

# start all your services
docker-compose up

# execute a defined command in docker-compose.cli.yml
docker-compose -f docker-compose.cli.yml run npm update

# if your command depends_on a service you need to merge the configs
docker-compose -f docker-compose.yml -f docker-compose.cli.yml run npm update

Specifying multiple docker-compose.yml files with the -f flag will merge them together, see the documentation. This allows you to depend on services/networks/volumes which are defined in another file.

For an in-depth discussion on the whole issue see docker/compose#1896.

Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately with two separated files, I rely on networks and services that are declared in another file :(
@nowox see the third command then: you can specify multiple docker-compose.yml files which will be merged; I updated my answer to explain this and added a link to the relevant docs.
Wooh! I didn't get that far. Indeed this is exactly what I am looking for!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.