4

I have a a script which builds docker image, loads it to docker, updates docker-compose.yml then it calls docker compose down and docker compose up. After the Docker compose up services are recreated and service which i'm interesting for is running with correct (new) version.

I don't want to stop all services, which are stopped by docker compose down, i want to stop the only target one. I found that both docker compose up and docker compose down supports specifying service name

down: https://forums.docker.com/t/docker-compose-down-specific-service/125914/8 https://docs.docker.com/reference/cli/docker/compose/down/ up: https://docs.docker.com/reference/cli/docker/compose/up/

But when i call docker compose down myservicename in Docker version 25.0.3 i receive error: unknown command "myservicename" for "docker compose down"

How to stop (and remove) only one container ? May be there is another way to "update image" for only one container?

4
  • 2
    You're doing something wrong, but from the little info you provide it's impossible to tell what. Please provide a minimal reproducible example with detailed instructions what you are doing. Commented Apr 4, 2024 at 8:22
  • Just stop the container you want with Docker container stop <containername>, then run docker-compose up again, which will recreate the ones not running. Commented Apr 4, 2024 at 8:28
  • @UlrichEckhardt what i'm doing wrong? I create a script to automate building image transferring it to server where docker runs, and re-creating container with this new image version. Commented Apr 4, 2024 at 9:32
  • If your script makes changes to the docker-compose.yml file, re-running docker-compose up -d will automatically delete and recreate the containers that have changed; you don't manually need a down or stop command. Commented Apr 4, 2024 at 10:07

2 Answers 2

9

You should use a combination of docker compose stop and docker compose rm commands, which do allow specifying a service name.

Stop the specific service: First, you need to stop the service if it's running

docker compose stop myservicename

Remove the stopped service container: After stopping the service, you can remove its container.

docker compose rm -f myservicename

Update and restart the specific service: Now that the specific service is stopped and removed, you can proceed to update its image and restart it.

docker compose up -d --no-deps --build myservicename
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! What if just call doclker compose up service name without stoping and removing it? Looks like it will recreate container as well. Do i really nee to stop and remove container?
Stopping and removing the container ensures a clean restart, free from residues of the previous instance. It's a matter of preference and the need for control over the process
0

I found the following solution just docker compose up -d servicename, without anydocker compose down and docker compose rm.

In response i receive:

 Container postgressvc  Running
 Container rabbitmqsvc_3_9_27  Running
 Container eventstoredishessvc_23_10_0  Running
 Container youmenu_dishes_run  Recreate
 Container youmenu_dishes_run  Recreated
 Container youmenu_dishes_run  Starting
 Container youmenu_dishes_run  Started
Operation completed successfully.

So, according to the output i can conclude container was recreated. Other containers were running without restart. Containers in output are containers from 'depends' section in docker-compose.yml

Despite others say it will be better to "stop and remove" old container, before recreating, i decided to post this answer, may be it will be useful for some one. In general i agree, - "stop and remove" the container ensures a clean restart.

Comments

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.