Nothing Special   »   [go: up one dir, main page]

DEV Community

Sami Ullah Saleem for AWS Community Builders

Posted on • Edited on

Clean Up your docker container

Introduction

As you use Docker more and more, you will accumulate more and more image layers and containers. Images can exceed gigabytes in size. Stopped containers tend not to use a significant amount of disk space, but can pollute the output from some Docker commands. You will learn how to reclaim the space from unneeded images and clear out needed space in this Lab Step.

Instructions

  1. Get an understanding of how much space is being used with the following system management command:

Copy code

docker system df

alt

This breaks down the disk usage into images and containers. Images are taking up the majority of the space.

  1. To see which images are taking up the most space, enter:

Copy code

docker images

alt

The flask-content-advisor and python images are both around 700MB. That's because the flask-content-advisor image is built on top of the python image. The difference between the two is how much additional space was added when you built the image using a Dockerfile. Notice that the output from df is smart enough to not double count the layers since they are shared between the images.

  1. You can get more informtation on the size of containers with the following command:

Copy code

docker ps -a -s

alt

The -s includes an extra column at the end showing the size of the container layer and the virtual size. The virtual size includes the image size and the container layer size.

  1. To remove the stopped web-server container, enter:

Copy code

docker rm web-server

The container must be stopped for you to be able to remove it. Confirm it has been removed with docker ps -a.

  1. The nginx image can now be removed because there are no containers depending upon it:

Copy code

docker rmi nginx:1.12

Each layer in the image is deleted. If there were image layers shared with other images that were used by containers, Docker would be smart enough to only remove the layers not being used. You will observe this now.

  1. Run a container based on the Python 3 image:

Copy code

docker run --name waiter -d -t python:3 python -c 'a = input("a")'

This will run a container that will endlessly wait for someone to input the value of a variable. This is an example of not using the default command of an image. The command in this case is everything after the image: python -c 'a = input("a")'. The Python 3 image is the base layer of the flask-content-advisor image you built.

  1. List all the containers:

Copy code

docker ps -a

alt

Notice the waiter container has a STATUS of Up, meaning it is running.

  1. Remove the advisor container:

Copy code

docker rm advisor

Now you have one container using the Python 3 image, but none using the flask-content-advisor image.

  1. Remove the flask-content-advisor image:

Copy code

docker rmi flask-content-advisor

This displays a delete output for each layer that is no longer being used. There are 8 layers deleted in total.

  1. Verify that the Python 3 image wasn't touched by the operation:

Copy code

docker images

alt

  1. To clean up everything not running, enter:

Copy code

docker system prune -a -f

alt

This prunes all containers and images that aren't used by running containers. This is a convenient way to clean up if you haven't been regularly removing unused images and containers. Verify that only the one running Python 3 container and its image are left with docker system df.

Summary

In this Lab Step, you learned how to measure the disk usage of Docker and clean up after you no longer need images and containers. You are able to delete images and containers manually or automatically using the prune system management command.

Top comments (0)