[英]Kill docker-compose containers without docker-compose.yml file
I have some docker containers provisioned by docker-compose with restart: always
flag.我有一些由 docker-compose 提供的 docker 容器,并带有
restart: always
标志。 But I accidentally delete docker-compose.yml file.但是我不小心删除了 docker-compose.yml 文件。
How do I delete those containers so that they will not restart automatically?如何删除这些容器,以便它们不会自动重新启动?
Without the compose file, docker-compose can't guess the name of the containers that need to be killed.没有 compose 文件,docker-compose 无法猜测需要杀死的容器的名称。 You need to do that manually using docker command:
您需要使用 docker 命令手动执行此操作:
docker container ls
docker rm -f <container-name-or-id>
You can use docker ps -a
to list all the running containers.您可以使用
docker ps -a
列出所有正在运行的容器。 After you can use docker stop container-name
to stop individually the containers you want and after you can use docker rm $(docker ps -a -q)
to delete and remove all the stopped containers.在您可以使用
docker stop container-name
单独停止您想要的容器之后,您可以使用docker rm $(docker ps -a -q)
删除和删除所有停止的容器。
Also if you want to delete the docker images as well you can use docker images
to list the existing images and after docker rmi image-name
to delete the image.此外,如果您还想删除 docker 图像,您可以使用
docker images
列出现有图像,然后在docker rmi image-name
删除图像。
I had a similar issue - I didn't want to fetch the .yml files just to remove the containers.我有一个类似的问题 - 我不想获取 .yml 文件只是为了删除容器。 As docker-compose adds labels to the containers, we can use these to filter the matching containers and then remove them.
由于 docker-compose 为容器添加标签,我们可以使用这些标签来过滤匹配的容器,然后将其删除。
To remove all the containers that docker-compose created for project ProjectName (normally the name of the folder where the docker-compose.yml
was) in shell (bash or similar), execute the following:要删除
docker-compose.yml
在 shell(bash 或类似的)中为项目 ProjectName(通常是docker-compose.yml
所在文件夹的名称)创建的所有容器,请执行以下命令:
docker ps --filter 'label=com.docker.compose.project=ProjectName' --format {{.ID}} | xarg -n 1 docker rm --force --volumes
docker ps
with the filter provides the list of containers, the format is provided so that only container IDs are printed带有过滤器的
docker ps
提供容器列表,提供格式以便仅打印容器 ID
xargs -n 1
- takes the list of container IDs and executes the target command on each of them providing the container ID as the argument to the target xargs -n 1
- 获取容器 ID 列表并在每个容器上执行目标命令,提供容器 ID 作为目标的参数docker rm
removes existing containers docker rm
删除现有的容器--force
- to also stop running containers (otherwise running containers would not be stopped or removed) --force
- 也停止运行容器(否则不会停止或删除正在运行的容器)
--volumes
- to also remove any anonymous volumes associated with the containers --volumes
- 还删除与容器关联的任何匿名卷
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.