dockerdocker-composedevopsdocker-containerdocker-image

How to "docker compose down" for certain service?


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?


Solution

  • 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