Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to docker-compose up only for services?

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?

like image 637
nowox Avatar asked Jul 23 '20 09:07

nowox


People also ask

Does docker compose create services?

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration.

Are docker compose services containers?

Services and container are related but both are different things. A service can be run by one or multiple containers. With docker you can handle containers and with docker-compose you can handle services. This compose file defines two services, web and db .

How can we control the start up order of services in docker compose?

You can control the order of service startup and shutdown with the depends_on option. Compose always starts and stops containers in dependency order, where dependencies are determined by depends_on , links , volumes_from , and network_mode: "service:..." .


1 Answers

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.

like image 67
acran Avatar answered Nov 01 '22 09:11

acran