80

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?

Inquisitor Shm
  • 1,458
  • 2
  • 10
  • 17
  • 1
    ```docker-compose build ``` followed by ```docker-compose up```. This would only build the changes to a single container instead of rebuilding all the containers – Abhishek S May 12 '21 at 14:37

6 Answers6

108

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

laurent
  • 5,747
  • 14
  • 43
  • 68
Jordan Morris
  • 1,356
  • 1
  • 10
  • 17
45

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

gabriel1836
  • 652
  • 7
  • 13
  • 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 Feb 07 '21 at 09:04
11

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.

user674669
  • 517
  • 2
  • 10
  • 22
3

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...

icarito
  • 170
  • 3
  • 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? – Jordan Morris Sep 15 '17 at 10:17
  • 2
    You can do `docker-compose -f docker-compose-production.yml down` – icarito Sep 17 '17 at 18:20
  • 7
    And where do you specify the container you would like to get up/down? `-f` is the selector of compose file, not container. – astrowalker Dec 11 '19 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. – Nusrat Nuriyev Oct 21 '21 at 12:49
2

As far as others shows how to start/up containers this is how you can restart and stop them separately:

for restarting specific container:
docker-compose restart <container_name>
docker-compose restart -t 10 <container_name> # Container will restart after 10 seconds

for only just stop container:
docker-compose stop <container_name>
docker-compose stop -t 10 <container_name> # Container will restart after 10

and final word is for those who want make changes without downtime, you can build new image and make changes without any stop start with this command but it will build all Dockerfile in docker-compose.yml file:
docker-compose up -d --build

Iman
  • 21
  • 1
0

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.

eco
  • 284
  • 2
  • 5