131

I have not been able to find a way to up/down just one container in a docker-compose.yml file. I can off-course start and stop a single container, but I cannot make changes to a containers configuration between restarts (environment variables, mount points etc.)

What am I missing here? What is the best practice in this case?

2
  • 2
    docker-compose build <service-name> followed by docker-compose up. This would only build the changes to a single container instead of rebuilding all the containers Commented May 12, 2021 at 14:37
  • 1
    I don't have your use case. I just have several containers running and I want to stop one. Apparently there is no way to do this. I ended up stopping everything and restarting them all minus the one. Seems mildly ridiculous.
    – Mike B
    Commented Apr 19, 2022 at 21:37

8 Answers 8

5

I had this need recently and solved it by having a separate docker-compose-production.yml file to deal with tweaks. Then remember to launch with docker-compose -f docker-compose-production.yml...

4
  • How does this work when it comes time to do docker-compose down? Wouldn't it bring down all the containers which have been brought up in all .yml files? Commented Sep 15, 2017 at 10:17
  • 2
    You can do docker-compose -f docker-compose-production.yml down
    – icarito
    Commented Sep 17, 2017 at 18:20
  • 9
    And where do you specify the container you would like to get up/down? -f is the selector of compose file, not container. Commented Dec 11, 2019 at 12:02
  • 1
    ... "by having a separate .yml file." Yes, it's not an ideal solution but it deletes not only volumes but also a network, which "docker-compose rm -s -v yourService" won't do, according to what I understand. Commented Oct 21, 2021 at 12:49
164

I found this to have the same affect as docker-compose down for a single service:

docker-compose rm -s -v yourService

docker-compose rm

Usage: rm [options] [SERVICE...]

Options:
-s, --stop Stop the containers, if required, before removing
-v Remove any anonymous volumes attached to containers

You can condense all the flags into a single - param: docker-compose rm -sv yourService

9
  • 1
    I think disposes the disk, btw Commented Sep 5, 2018 at 12:02
  • 3
    Could you please explain the flags, what they would do?
    – Semo
    Commented May 22, 2019 at 13:23
  • Jordan Morris, according to the manual -v disposes of anonymous volumes only when used with 'rm'. When used with 'down' it is stronger, also disposing of named volumes. The former is pretty safe. Commented Aug 7, 2020 at 12:47
  • 7
    This should be the answer. docker-compose down only does all or nothing...this points to the service and stops it.
    – eco
    Commented Feb 7, 2021 at 6:05
  • 1
    I was looking for this solution exactly. Only thing is I had to execute docker-compose rm -s -v yourService twice. First time it stops service's container. Second time it asks ? Going to remove name-service-1 which I responded yes and container is removed along with unnamed volumes, being able to apply fresh start recreating unnamed volumes again with docker-compose up -d yourService. This is without affecting other services being stoped, which docker-compose down does. Thanks!
    – MauricioID
    Commented Mar 22, 2023 at 17:48
62

I would suggest you check out this excellent thread on stackoverflow.com. The quick answer here to rebuild the single container and restart it is:

docker-compose up -d --build worker

This would be the ideal solution if, for example, your changes involved your Dockerfile and not just docker-compose.ymll

3
  • I wish I'd seen this before. This is the correct answer!! In any case I've created my version, which will probably get triaged and deprecated with a step by step explanation.
    – eco
    Commented Feb 7, 2021 at 9:04
  • Almost the correct answer, since this says how to bring up one container. The question is also about how to bring one container down. However, that seems to be only possible with docker rm Commented Apr 5, 2022 at 14:01
  • 1
    Mmm ran this command, replacing worker with the service name, but it just rebuilt all services... I guess worker was supposed to be literal? I am just looking for a set of commands to bring one service down, then bring one service back up... Commented Apr 5, 2022 at 14:22
14

You can use

$ docker-compose -f docker-compose.yml up yourService

to start just yourService and all dependencies required by it.

So if yourService depends on mysql container, the above command would start both the containers.

7

Others have shown how to start/up containers together, however this is how you can restart and stop them individually.

Restart a Container

# restart immediately
docker-compose restart <container_name>

# restart after a 10s delay
docker-compose restart -t 10 <container_name> 

Stop a Container

# stop immediately  
docker-compose stop <container_name>

# stop after a 10s delay  
docker-compose stop -t 10 <container_name>

For those who want to make changes without downtime, you can build a new image and make changes without any stop/start with the following command but it will build all Dockerfile in docker-compose.yml file:

docker-compose up -d --build
1
  • 1
    This is the only real answer to the original question - using "start/stop/restart <container>".
    – odigity
    Commented Nov 11, 2022 at 16:45
1

There's no need to delete anything. To address the OP's question: You need to rebuild the image then use up to replace the container with the newly configured imaged.

IMPORTANT: notice that the new image will automatically be tagged with latest.

Step 1: Edit Docker file
Step 2: docker-compose build
Step 3: docker-compose up

The docker-compose up will leave all the unchanged containers alone and replace only the containers that have a newly created image.

0

Building off of Jordan Morris' answer, this version of the command will take down your specific service and not prompt you before it removes the associated volumes:

docker-compose rm -s -v -f your_service_name

The difference is the addition of a -f flag (for "force").

0
$ docker-compose -f your-compose-file-here.yml stop only-that-service

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .