Deleting a Container Image

You can delete unnecessary container images to free up drive space.

  • Delete a container
    1. Log in to the server as the root user.
    2. Run the following command to check the value of CONTAINER ID:
      1
      2
      3
      4
      # View running containers.
      docker ps
      # View all containers.
      docker ps -a
      

      Information similar to the following is displayed (the container ID is 5f45daf9eb8f):

      1
      2
      CONTAINER ID      IMAGE                    COMMAND       CREATED            STATUS          PORTS       NAMES
      5f45daf9eb8f      ascend-toolkit:8.0.0     "/bin/bash"   39 seconds ago     Up 28 minutes               boring_wright
      
    3. Before deleting a running container, stop it first. If the container to be deleted is stopped, skip this step.
      1
      docker stop <CONTAINER ID>
      

      Replace <CONTAINER ID> with the container ID queried in the preceding step.

    4. Run the following command to delete the container:
      1
      docker rm <CONTAINER ID>
      
  • Delete an image

    Before deleting an image, delete the containers that depend on the image. To delete an image, perform the following steps:

    1. Delete the container that depends on the image by referring to 1 to 4. If no container depends on the image, skip this step.
    2. Run the docker images command to view the IMAGE ID.

      Information similar to the following is displayed (the image ID is feb5d9fea6a5):

      1
      2
      REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
      docker.io/hello-world   latest              feb5d9fea6a5        14 months ago       13.3 kB
      
    3. Run the following command to delete the image.
      1
      docker rmi <IMAGE ID>