乐闻世界logo
搜索文章和话题

How do you remove a Docker container?

1个答案

1

To delete containers in Docker, you can use the following commands:

  1. Stop the container: Before deleting a container, ensure it is stopped if it is running. To stop the container, use:

    bash
    docker stop [container ID or container name]

    For example, if the container ID is abc123, the command is:

    bash
    docker stop abc123
  2. Delete the container: Once the container is stopped, use the docker rm command to delete it:

    bash
    docker rm [container ID or container name]

    Similarly, for container ID abc123, the command is:

    bash
    docker rm abc123

If you want to stop and delete the container in one step, use:

bash
docker rm -f [container ID or container name]

The -f parameter stands for 'force', meaning it forcibly stops the container even if it is running, then deletes it.

Batch delete containers: To delete all stopped containers, use:

bash
docker container prune

To delete all containers (regardless of their state), use:

bash
docker rm -f $(docker ps -aq)

Here, docker ps -aq lists all container IDs.

These are common methods for deleting containers in Docker. They are highly useful in practical scenarios, especially when cleaning up resources or redeploying containers.

2024年8月9日 13:46 回复

你的答案