Skip to content

Docker Command Cheat Sheet

Docker is an open-source application container engine that allows developers to pack their applications and dependencies into a portable image, which can then be published to any popular Linux or Windows machine, achieving virtualization. Containers use a sandbox mechanism and have no interfaces between them.

General Usage

Start a container in the background

docker run -d jenkins

Start an interactive container

docker run -it ubuntu bash

Start a container that is automatically removed when it stops

docker run --rm ubuntu bash 

Start a container with mapped ports

docker run -p 80:80 -d nginx 

Start a named container

docker run --name mydb redis 

Start a stopped container

docker start mydb 

Stop a container

docker stop mydb

Start a container and add metadata

docker run -d \ label=traefik.backend=jenkins jenkins

Build Images

Build an image from the Dockerfile in the current directory

docker build --tag myimage . 

Force rebuild an image

docker build --no-cache . 

Convert a container to an image

docker commit c7337 myimage 

Remove all unused images

docker rmi $(docker images -q -f "dangling=true")

Debug

Log into a running container

docker exec -it c7337 docker

Show logs of a container running as a daemon

docker logs -f c7337

Show exposed ports of a container

docker port c7337

Volumes & Networking

Create a local volume

docker volume create --name myvol 

Mount a volume when starting a container

docker run -v myvol:/data redis

Destroy a volume

docker volume rm myvol 

List all volumes

docker volume ls

Create a local network

docker network create mynet 

Connect a container to a network at startup

docker run -d --net mynet redis

Connect a container to a specific network

docker network connect mynet c7337 

Disconnect a container from a network

docker network disconnect mynet c7337

Container Management

List running containers

docker ps

List all containers (including running and stopped)

docker ps -a

Inspect container metadata

docker inspect c7337

List all available local images

docker images

Remove all stopped containers

docker rm $(docker ps --filter status=exited -q) 

List all containers with a specific label

docker ps --filter label=traefik.backend

Query the IP address of a running container

docker inspect -f '{{ .NetworkSettings.IPAddress }}' c7337

Description

Image Name

redis, jenkins, nginx

Container Name or Commit ID

mydb  # Container Name 
c7337 # Commit ID