Common Docker Commands
This post was translated from Chinese by AI. If anything reads oddly, the Chinese original is authoritative. 中文原文

How to pause a container
To pause a Docker container, use the docker pause command. Here are the steps:
-
First, use
docker psto list running containers and find the Container ID or name of the container you want to pause. -
Run the following command to pause the container, specifying its Container ID or name:
docker pauseThis pauses all processes in the container, suspending their execution.
-
To verify that the container is paused, run
docker psagain to check its status.docker psA paused container will show
PAUSEDin the status column.
Note: Pausing a container suspends all processes inside it, including those currently running. To resume the container, use docker unpause.
How to restart a container
To restart a Docker container, use the docker restart command. Here are the steps:
-
First, use
docker psto list running containers and find the Container ID or name of the container you want to restart. -
Run the following command to restart the container, specifying its Container ID or name:
docker restartThis stops and restarts the container.
-
To verify that the container has restarted, run
docker psagain to check its status.docker psThe restarted container will show as running.
Note: Restarting a container preserves its state and data. If the container requires specific conditions to run properly, such as particular environment variables or mounted volumes, make sure those conditions are met before restarting.
How to stop a container
To stop a Docker container, use the docker stop command. Here are the steps:
-
First, use
docker psto list running containers and find the Container ID or name of the container you want to stop. -
Run the following command to stop the container, specifying its Container ID or name:
docker stopThis sends a stop signal to the container, causing it to stop running.
-
To verify that the container has stopped, run
docker psagain to check its status.docker psThe stopped container will no longer appear in the list.
Note: Stopping a container terminates its processes and changes its status to stopped. To start a stopped container, use docker start.
How to show all containers when stopped containers seem to be missing
To show all containers, including running and stopped containers, use the docker ps -a command. Here are the steps:
-
Open a terminal or command prompt.
-
Run the following command:
docker ps -aThis lists information about all containers, including their IDs, statuses, and creation times.
To show only stopped containers, add the
--filter "status=exited"option, as shown below:docker ps -a --filter "status=exited"This shows only stopped containers.
Note: By default, docker ps shows only running containers. Use the -a option to show all containers, including stopped ones.
How to remove a container
To remove a Docker container, use the docker rm command. Here are the steps:
-
First, use
docker ps -ato list all containers, including running and stopped containers. -
Find the Container ID or name of the container you want to remove.
-
Run the following command to remove the container, specifying its Container ID or name:
docker rmTo remove multiple containers, specify multiple Container IDs or names in the same command.
docker rm ...Once the container is successfully removed, its associated images and resources will be freed.
Note: Only stopped containers can be removed. To remove a running container, stop it first, then remove it. To also remove associated volumes when removing a container, add the -v option to docker rm, as in docker rm -v.
Last updated 2024-01-14
Comments 0