简体   繁体   English

在没有 docker-compose.yml 文件的情况下杀死 docker-compose 容器

[英]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.

相关问题 如何在全球范围内显示由 docker-compose 创建的所有正在运行的容器,而不考虑 docker-compose.yml - How to show all running containers created by docker-compose, globally, regardless of docker-compose.yml 在docker-compose.yml中了解容器的主机名 - Understanding Hostname of Containers in docker-compose.yml docker-compose在docker-compose.yml中具有多个服务的构建 - docker-compose build with multiple services in docker-compose.yml 将我的容器启动文件翻译成 docker-compose.yml - translate my containers starter file to docker-compose.yml 使用sudo运行docker-compose时docker-compose.yml文件中的变量替换 - Variable substitution in docker-compose.yml file when running docker-compose with sudo 带有非默认 docker-compose.yml 文件的 docker-compose ps - docker-compose ps with non-default docker-compose.yml file docker-compose.yml运行docker-compose时的文件错误 - docker-compose.yml file errors on running docker-compose up docker-compose 向上抛出错误错误:在文件“./docker-compose.yml”中,服务必须是映射,而不是 NoneType - docker-compose up throwing error ERROR: In file './docker-compose.yml', service must be a mapping, not a NoneType 使用docker-compose up时访问docker-compose.yml文件内的环境变量 - Accessing environmental variable inside of docker-compose.yml file when using docker-compose up 移动 docker-compose.yml 文件时,Nginx 图像的 docker-compose 不起作用 - docker-compose of Nginx image doesn't work when the docker-compose.yml file is moved
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM