Wood Chen

Common Docker Commands

0 comments78 views561 words

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:

  1. First, use docker ps to list running containers and find the Container ID or name of the container you want to pause.

  2. Run the following command to pause the container, specifying its Container ID or name:

    docker pause 
    

    This pauses all processes in the container, suspending their execution.

  3. To verify that the container is paused, run docker ps again to check its status.

    docker ps
    

    A paused container will show PAUSED in 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:

  1. First, use docker ps to list running containers and find the Container ID or name of the container you want to restart.

  2. Run the following command to restart the container, specifying its Container ID or name:

    docker restart 
    

    This stops and restarts the container.

  3. To verify that the container has restarted, run docker ps again to check its status.

    docker ps
    

    The 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:

  1. First, use docker ps to list running containers and find the Container ID or name of the container you want to stop.

  2. Run the following command to stop the container, specifying its Container ID or name:

    docker stop 
    

    This sends a stop signal to the container, causing it to stop running.

  3. To verify that the container has stopped, run docker ps again to check its status.

    docker ps
    

    The 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:

  1. Open a terminal or command prompt.

  2. Run the following command:

    docker ps -a
    

    This 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:

  1. First, use docker ps -a to list all containers, including running and stopped containers.

  2. Find the Container ID or name of the container you want to remove.

  3. Run the following command to remove the container, specifying its Container ID or name:

    docker rm 
    

    To 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

Related posts

Comments 0