Skip to main content
  1. Posts/
  2. Docker/

·555 words·3 mins·
Jaume Sabater
Author
Jaume Sabater
CTO and systems engineer

Resource management with Docker
#

Cleaning up resources
#

The general rule of thumb is to make sure nothing depends on the resource before removing it:

  • Containers depend on images, volumes, and networks.
  • Volumes may contain persistent data (databases, user uploads, etc.).
  • Networks may connect multiple containers.

Therefore, always stop and remove containers first, then prune other resources if you are sure they are unused.

Containers
#

To remove a specific container, use the following command:

docker rm <container_name_or_id>

Multiple containers can be removed in a single command by providing a list of arguments.

To remove a container and its anonymous volumes, use the following command:

docker rm --volumes <container_name_or_id>

An anoymous volume is a volume that was not named explicitly.

To remove all stopped containers use the following command:

docker container prune --force

The --force option skips confirmation.

Volumes
#

To remove a specific volume, use the following command:

docker volume rm <volume_name>

You cannot remove a volume that is still attached to a container.

To rmove all unused volumes, use the following command:

docker volume prune --force

The --force option skips confirmation.

Multiple volumes can be removed in a single command by providing a list of arguments.

Networks
#

To remove an unused network, use the following command:

docker network rm <network_name>

Multiple networks can be removed in a single command by providing a list of arguments.

To remove all unused networks, use the following command:

docker network prune

Default networks bridge, host, and none cannot be removed.

Images
#

To remove specific image, use the following command:

docker rmi <image_name_or_id>

Multiple images can be removed in a single command by providing a list of arguments.

To remove all unused images, use the following command:

docker image prune --all --force

The --force option skips confirmation.

To remove dangling images, i.e., intermediate layers without tags, use the following command:

docker image prune --force

The --force option skips confirmation.

Global cleanup
#

Docker includes a command to clean up all unused resources, globally. This includes stopped contaienrs, unused networks, dangling images and build cache:

docker system prune

Furthermore, there is a more aggressive version, which includes unused volumes and unused images (not just dangling ones):

docker system prune --all --volumes

Resource limits
#

Resource limits can be set at run time (with docker run), but not in Dockerfiles, as they define the build environment, not runtime constraints.

To limit a container to a specific number of CPU cores, use the --cpus parametre:

docker run --cpus="1.5" <image_name>

This would restricts the container to use at most 1.5 CPU cores.

To pin a container to specific CPU cores, use the --cpuset-cpus argument:

docker run --cpuset-cpus="0,2" <image_name>

This container will only run on CPU cores 0 and 2.

To limit the container memory usage, use the --memory argument:

docker run --memory 512m <image_name>

If you would like to prevent a container from swapping excessively, use the --memory-swap argument:

docker run --memory="512m" --memory-swap="512m" <image_name>

CPU and RAM resource limits can be combined. For example,

docker run -d \
  --name myapp \
  --cpus="2" \
  --memory="512m" \
  --restart unless-stopped \
  myimage:latest

Finally, you can update resource limits for a running or stopped container using the docker update command. For example:

docker update --cpus=4 --memory=1g myapp

At any moment, you can show the resource usage of a container:

docker stats --no-stream myapp

Related

·1503 words·8 mins

·1287 words·7 mins

·514 words·3 mins