Skip to content

Docker Commands Cheat Sheet

Docker is an open-source application container engine that allows developers to package their applications and dependencies into a portable image, which can then be released to any popular Linux or Windows machine, and virtualization can also be achieved. Containers use a sandbox mechanism entirely and do not have any interfaces between each other.

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 deleted 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 a Dockerfile in the current directory

docker build --tag myimage . 

Force rebuild an image

docker build --no-cache . 

Commit 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 active logs for a running daemon container

docker logs -f c7337

Show exposed ports of a container

docker port c7337

Volumes

Create a local volume

docker volume create --name myvol 

Mount a volume when a container starts

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 specified 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 images locally

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 a running container with specific metadata

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

Description

Image Name

redis, jenkins, nginx

Container Name or Commit ID

mydb  # container name 
c7337 # commit ID