Docker CLI cheat sheet

The complete reference is in Docker's own documentation:

Docker CLI reference

Contents:

  1. List running and stopped containers
  2. Managing Docker images
  3. Get a shell on a running container
  4. Docker compose quick start
  5. Freeing up disk space from Docker

List running and stopped containers

See all running containers (includes port information, ID and name)

docker ps

See all containers including stopped containers

docker ps -a

Docker images

List all images on your machine

docker images

Remove an image with the image ID e9dc2cdd84f9

docker image rm e9dc2cdd84f9

Get a shell on a running container

Run docker ps to get the name or ID of the container you want a shell on

# docker ps
CONTAINER ID   IMAGE         COMMAND                 CREATED        STATUS        PORTS                                                                     NAMES
5c229f1c57d8   nginx:nginx   "/docker-entrypoint.…"  8 seconds ago  Up 3 seconds  0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp  Nginx

Run the execute command with the CONTAINER ID and the -it interactive flag.

docker exec -it 5c229f1c57d8 bash

Or you can use the NAME of the container

docker exec -it Nginx bash

You can log in as root by specifying the user

docker exec -it --user root 5c229f1c57d8 bash

If you get this error OCI runtime exec failed: exec failed: unable to start container process: exec: "bash": executable file not found in $PATH: unknown then bash isn't installed in the container. Common Docker images such as alpine don't come with bash, so use sh instead.

docker exec -it --user root 5c229f1c57d8 sh

Docker Compose

Building

Build your project

docker compose build

Build your project without using any local cache

docker compose build --no-cache

Starting and stopping containers

Start your project's existing containers in the background

docker compose start

Stop your project's containers without removing them

docker compose stop

Start the project and follow all the containers' logs in your terminal

docker compose up

Stop your project and remove its containers and networks

docker compose down

Freeing up disk space from Docker

Remove dangling images (untagged images that display <none> for the name)

docker image prune

Remove dangling images and unused images (not assigned or used in a container)

docker image prune -a

Remove all stopped containers, dangling images, networks and build cache

docker system prune

Remove all stopped containers, networks and build cache, plus dangling and unused images

docker system prune -a

If you run containers in production, our DevOps as a service team sets up pipelines that build your images and deploy them without downtime.