Skip to content
Created: 2021-08-27 | Last update: 2022-08-12

Common Docker Commands

This is a short summary of the most commonly used Docker commands. If you're new to Docker, or even experienced Docker, it can be helpful to have a quick reference to the most commonly used Docker commands for managing the Docker environment.

Show all Containers Including Running and Stopped

docker ps -a

Show Docker Container Logs

docker logs <container_id>

Get A Container Shell

docker exec -it <container_id> /bin/bash

or

docker exec -it <container_id> /bin/sh

depending on the shells available on the Docker image.

Stoping Containers

docker stop <container_id>

foce stop with kill

docker kill <container_id>

Removing Containers

docker rm <container_id>

force remove

docker rm -f <container_id>

Find Container IP Address

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container name/id>

Copy Files into Docker Container

docker cp <local file> <container name/id>:<remote file>

Copy Files from Docker Container

docker cp <container name/id>:<remote file> <local file>

Purging

Purging All Unused or Dangling Images, Containers, Volumes, and Networks Docker provides a single command that will clean up any resources — images, containers, volumes, and networks — that are dangling (not associated with a container):

docker system prune

To additionally remove any stopped containers and all unused images (not just dangling images), add the -a flag to the command:

docker system prune -a

Monitor System Resource Utilization for Running Containers

To check the CPU, memory, and network I/O usage of a single container, you can use:

docker stats <container>

For all containers listed by ID:

docker stats $(docker ps -q)

For all containers listed by name:

docker stats $(docker ps --format '{{.Names}}')

For all containers listed by image:

docker ps -a -f ancestor=ubuntu

Remove all untagged images:

docker rmi $(docker images | grep “^” | awk '{split($0,a," "); print a[3]}')

Remove container by a regular expression:

docker ps -a | grep wildfly | awk '{print $1}' | xargs docker rm -f

Remove all exited containers:

docker rm -f $(docker ps -a | grep Exit | awk '{ print $1 }')

Credit

Thanks to @wsargent for creating this cheat sheet.

Comments