How to Remove Docker Images, Containers, and Volumes

How to Remove Docker Images, Containers, and Volumes

This cheat sheet-style guide provides a quick reference to commands that are useful for freeing disk space and keeping your system organized by removing unused Docker images, containers, and volumes. Docker gives you all the tools you need to clean up your system from the command line. Docker Image Guide: How to Remove and Delete Docker Images, Stop Containers and Remove all Volumes

Docker is widely adopted and is a great vehicle to deploy an application to the cloud or some other Docker-ready infrastructure. Additionally, it is useful for local development. You can start complex applications quickly, develop in isolation, and still have a very good performance.

Here are the most important commands to use Docker in your daily business efficiently.

List All Docker Images
docker images

In my case, I have 3 images installed:

  • MySQL, version 8.0.19, one tagged as latest version
  • and Cassandra with the latest version.

To get more information about an image, you can inspect them:

docker inspect mysql:latest

This will return a list of information. Alternatively, you can also use the image ID to get the information:

docker inspect 3a5e53f63281

The output can be overwhelming. Therefore, there is a handy option to filter certain information:

docker inspect --format='{{.RepoTags}}  {{.Config.Image}}' 3a5e53f63281

Remove Docker Images

A single image can be removed by:

docker rm mysql:latest

In my case, the image is still tagged with mysql:8.0.19. Therefore, to remove it completely, I need to also remove another version tag:

docker rm mysql:8.0.19

To remove the image directly, it is easier to delete the image by image id:

docker image rm 3a5e53f63281 -f

The option -f forces the execution, because otherwise you would get an error if the image is referenced by more than 1 tag.

Start a Docker Image

An image can be started in the foreground by:

docker run cassandra

If the image does not exist, then it will be downloaded. You can stop the execution by pressing CTRL+C. You can also run it in the background by adding the -d option:

docker run -d mysql

If the container is started in the background, then you receive the container ID.

By default, the container runs in isolation. Therefore, you won't be able do any communication with it, and no files are stored in your current directory.

Forward ports of a container

You can forward ports by using the -p option to, for example, a page that is exposed from your container:

docker run -p 8080:80 nginx

This NGINX container exposes a webserver on port 80. By using the -p 8080:80, the local port 8080 is forwarded to the container port 80.

Log in a container

Sometimes it is helpful to log into a container. This is only possible if the container has a shell installed. You will get an error if this is not the case.

First, start the container detached and give it a name:

docker run -d --name my_container nginx

This will return a container ID. Now you can execute a shell in the container and attach your input and output to it by using the options -i and -t:

docker exec -it my_container bash

Instead of the container name, you can also use the returned container ID for all following operations. Sometimes, bash is not available. Therefore, you can also try to lunch a basic shell:

docker exec -it my_container sh
List running containers

After you’ve started a container, you can see all running containers executing:

docker ps

By appending -a, exited containers will also be listed:

docker ps -a

Share a local folder with a container

Sometimes it is useful to sync files between the container and the local filesystem. You can do it by running a container and using the -v option. On Linux and macOS, you can share a local temporary folder with a container by:

docker run --name=my_container -d -v $(pwd)/tmp:/var/log/nginx -p 8080:80 nginx

On windows you can run:

docker run --name=my_container -d -v %cd%/tmp:/var/log/nginx -p 8080:80 nginx
Stop running containers

It is possible to stop a running container by:

docker stop my_container

Stopping a container stops all processes but keeps changes within the filesystem.

Start a stopped container

A stopped container can be started by:

docker start my_container
Remove a container

To remove a stopped container, you can execute:

docker rm my_container

To stop and remove the container in one command, you can add the force option -f.

docker rm -f my_container
Create a volume and share it with multiple containers

An independent volume named SharedData can be created by:

docker volume create --name SharedData

docker run --name=my_container -d -v SharedData:/var/log/nginx -p 8080:80 nginx

docker run --name=my_container_2 -d -v SharedData:/var/log/nginx -p 8080:80 nginx

Both containers will have a shared folder, and files will be synced between both containers.

Remove a volume

To remove a volume, all containers that use the volume need to be removed.

docker rm -f my_container
docker rm -f my_container_2
docker volume rm SharedData
Remove stopped containers and unused images

A safe tidy-up command is:

docker system prune -a
Remove all unused volumes

All unmounted volumes can be removed by:

docker volume prune
Conclusion

Creating container, logging into containers, forwarding ports, and sharing volumes are the most important commands of your Docker command line interface. They build the foundation of systems like Kubernetes and enable us to create and run applications in isolation.

WordPress in Docker. Part 1: Dockerization

WordPress in Docker. Part 1: Dockerization

This entry-level guide will tell you why and how to Dockerize your WordPress projects.

This entry-level guide will tell you why and how to Dockerize your WordPress projects.

List all containers in Docker(Docker command)

List all containers in Docker(Docker command)

We can get a list of all containers in docker using `docker container list` or `docker ps` commands.

We can get a list of all containers in docker using docker container list or docker ps commands.

List Docker Containers

To list down docker containers we can use below two commands

  • docker container list
  • docker ps

docker container ls command introduced in docker 1.13 version. In older versions we have to use docker ps command.

List all Containers in docker, using docker ls command

The below command returns a list of all containers in docker.

docker container list -all

or

docker container ls -all

List all containers in docker, using docker ps command

In older version of docker we can use docker ps command to list all containers in docker.

$ docker ps -all

or

$ docker ps -a

List all Running docker containers

The default docker container ls command shows all running docker containers.

$ docker container list

or

$ docker container ls

or

To get list of all running docker containers use the below command

$ docker ps

List all stopped docker containers command

To get list of all stopped containers in docker use the below commands

$ docker container list -f "status=exited"

or

$ docker container ls -f "status=exited"

or you can use docker ps command

$ docker ps -f "status=exited"

List all latest created docker containers

To list out all latest created containers in docker use the below command.

$ docker container list --latest

Show n last created docker containers

To display n last created containers in docker use the below command.

$ docker container list --last=n

What is Docker | Docker Tutorial for Beginners

What is Docker | Docker Tutorial for Beginners

This DevOps Docker Tutorial on what is docker will help you understand how to use Docker Hub, Docker Images, Docker Container & Docker Compose. This tutorial explains Docker's working Architecture and Docker Engine in detail.

This Docker tutorial also includes a Hands-On session around Docker by the end of which you will learn to pull a centos Docker Image and spin your own Docker Container. You will also see how to launch multiple docker containers using Docker Compose. Finally, it will also tell you the role Docker plays in the DevOps life-cycle.

The Hands-On session is performed on an Ubuntu-64bit machine in which Docker is installed.