552

I have a docker-compose.yml which contain several containers. Three of them are for my app (client, server and database) and the rest are for various dev tools (e.g. psql, npm, manage.py, etc). When I do docker-compose up all of them are started, but I only want the three main ones to start. Because of the links I've specified, I can start just those three with docker-compose up client but then the output is only from that one container. So, is there a way to do one of the following:

  1. Tell docker-compose which containers should by started by docker-compose up
  2. Get output from all linked containers from docker-compose up client
1
  • 7
    According to the docs: docker-compose up [options] [SERVICE...] lets you start whatever subset of services you care to list. Commented Apr 24, 2020 at 21:28

11 Answers 11

717

You can start containers by using:

$ docker-compose up -d client

This will run containers in the background and output will be avaiable from

$ docker-compose logs

and it will consist of all your started containers

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

5 Comments

Perfect. Just drop the -d flag if you want to see the logs echoed to stdout
Is it possible to start a dependant container, if the depency container is already running and I don't want to restart it?
Beware depends_on flag on docker-compose.yml
this helped me: docker-compose logs , for detected bugs in a container
To start a service without dependent containers, add the --no-deps flag. Example: docker-compose --file docker-compose.test.yml up --no-deps --detach api-test
180

To start a particular service defined in your docker-compose file. for example if your have a docker-compose.yml

docker-compose start db  

given a compose file like as:

version: '3.3'

services:
   db:
     image: mysql:5.7
     ports:
       - "3306:3306"
     volumes:
       - ./db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: yourPassword
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: yourPassword

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "80:80"
     volumes:
       - ./l3html:/var/www/html
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: yourPassword
volumes:
    db_data:
    l3html:

Some times you want to start mySQL only (sometimes you just want to populate a database) before you start your entire suite.

4 Comments

Why the sudo before docker-compose ...? That isn't necessary, right?
if you don't have your user in the docker group yes, it would be necessary.
what the difference if i use sudo docker-compose up db instead of sudo docker-compose start db
@LukAron - start assumes the container already exists and just starts it, up will pull images if necessary, create containers if necessary, then start the container.
148

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:
  client:
    # ...
  db:
    # ...
  npm:
    profiles: ["cli-only"]
    # ...
docker-compose up # start main services, no npm
docker-compose run --rm npm # run npm service
docker-compose --profile cli-only up # start main and all "cli-only" services

original answer

Since docker-compose v1.5 it is possible to pass multiple docker-compose.yml files with the -f flag. This allows you to split your dev tools into a separate docker-compose.yml which you then only include on-demand:

# start and attach to all your essential services
docker-compose up

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

# if your command depends_on a service you need to include both configs
docker-compose -f docker-compose.yml -f docker-compose.dev.yml run npm update

For an in-depth discussion on this see docker/compose#1896.

2 Comments

Very useful! missing a "up" there after docker-compose --profile cli-only
If you have at least one service without profile, if you run --profile profile, that service without profile will also be run. Thus, you should add profile directive to all your services. 1.29.1
104

Oh, just with this:

$ docker-compose up client server database

2 Comments

I think this answer is the most useful as it directly answers my question how to start multiple services.
This should be the accepted answer - why is this 80% down the page?
19

One good solution is to run only desired services like this:

docker-compose up --build $(<services.txt)

and services.txt file look like this:

services1 services2, etc

of course if dependancy (depends_on), need to run related services together.

--build is optional, just for example.

Comments

8

You can use docker compose up --build ${service-name} -d. Where the flag -d is for running in detached mode.

Comments

3

I actually had a very similar challenge on my current project. That broght me to the idea of writing a small script which I called docker-compose-profile (or short: dcp). I published this today on GitLab as docker-compose-profile. So in short: I now can start several predefined docker-compose profiles using a command like dcp -p some-services "up -d". Feel free to try it out and give some feedback or suggestions for further improvements.

Comments

1

You can use the run command and specify your services to run. Be careful, the run command does not expose ports to the host. You should use the flag --service-ports to do that if needed.

docker-compose run --service-ports client server database

Comments

1

If you have multiple compose files, you should at first add them after at remember each services from compose files that you wanna up. look at below:

docker-compose -f $(COMPOSE_FILES) -f $(ARM_64_COMPOSE_FILES) up -d app mysql_db_arm64

Comments

-1

You can simply just use docker-compose up ccc and it will run only container ccc and its dependencies.

Comments

-2

You usually don't want to do this. With Docker Compose you define services that compose your app. npm and manage.py are just management commands. You don't need a container for them. If you need to, say create your database tables with manage.py, all you have to do is:

docker-compose run client python manage.py create_db

Think of it as the one-off dynos Heroku uses.

If you really need to treat these management commands as separate containers (and also use Docker Compose for these), you could create a separate .yml file and start Docker Compose with the following command:

docker-compose up -f my_custom_docker_compose.yml

4 Comments

A lot of people want to do this.
@msrd0 You usually don't want to do this? Let people decide for themselves!
There should be a rule on SO that you can't downvote without explaining why. Because this (or at least the first part) makes most sense, having an additional container for every possible management command is super weird.
I would like to do this :) Why? E.g. in dev environment where I would start multiple service in containers, and another outside - the one I'm currently working on (for easier dev/debugging)

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.