135

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?

8 Answers8

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

laurent
  • 5,774
66

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

15

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

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
  • 210
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
Blindspots
  • 3,472
Iman
  • 171
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.

eco
  • 304
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").

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