Scope. Docker Engine CLI. Replace example container IDs with actual values. Container shells vary by image; sh is available in many minimal images, while some images have no shell. Inspect disk usage with docker system df before pruning unused objects.
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 isolate processes but can communicate through configured networks and shared resources.
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 app=web nginx:stable
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 dangling images (use -a to include other unused images)
docker image prune
Debug
Log into a running container
docker exec -it c7337 sh
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 container prune
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 # Container ID