Skip to content

docker

CLI commands to build, run, manage containers, images, networks, volumes, compose, security, and swarm.

Containers

Commands for managing running and stopped containers.

List All Containers

Shows running containers by default or all containers.

Terminal window
docker ps -a
Use docker ps to list only running containers.

Start Stopped Container

Starts a stopped container instance.

Terminal window
docker start <container_id>
Example
docker start web_app

Stop Running Container

Gracefully stops a running container instance.

Terminal window
docker stop <container_id>
Example
docker stop web_app
Use docker kill to force stop.

Remove Stopped Container

Deletes a stopped container from disk.

Terminal window
docker rm <container_id>
Example
docker rm web_app

Run Interactive Container

Starts a container and opens a shell session.

Terminal window
docker run -it <image_name> <command>
Example
docker run -it ubuntu bash
Use --rm to automatically remove container after exit.

Execute Container Command

Runs a command inside a running container.

Terminal window
docker exec -it <container_id> <command>
Example
docker exec -it web_app bash

Copy Container Files

Copies files between a container and host filesystem.

Terminal window
docker cp <container_id>:<path> <destination>
Example
docker cp web_app:/app/logs.txt ./logs.txt

Inspect Container Metadata

Displays low-level metadata about a container.

Terminal window
docker inspect <container_id>
Example
docker inspect web_app

View Resource Statistics

Displays real-time CPU, memory, and I/O statistics.

Terminal window
docker stats

View Container Logs

Shows logs from a running container.

Terminal window
docker logs -f <container_id>
Example
docker logs -f web_app

Images and Building

Commands for managing and building container images.

List Local Images

Displays a list of all locally stored images.

Terminal window
docker images

Pull Remote Image

Downloads an image from a container registry.

Terminal window
docker pull <image_name>
Example
docker pull nginx:latest

Build Image From Dockerfile

Creates a Docker image from a local Dockerfile.

Terminal window
docker build -t <image_name> .
Example
docker build -t my-app .

Build Without Cache

Forces a fresh build without using cached layers.

Terminal window
docker build --no-cache -t <image_name> .
Example
docker build --no-cache -t my-app .

Build Custom Dockerfile

Specifies a custom Dockerfile for the build process.

Terminal window
docker build -f <dockerfile> -t <image_name> .
Example
docker build -f Dockerfile.prod -t my-app .

Remove Local Image

Deletes a Docker image from local storage.

Terminal window
docker rmi <image_id>
Example
docker rmi my-app

Build And Push Image

Builds and directly pushes the image to a registry.

Terminal window
docker buildx build --push --tag <registry>/<image>:<tag> .
Example
docker buildx build --push --tag myregistry.com/my-app:latest .

Volumes and Persistent Storage

Commands for managing volumes and persistent storage.

List Docker Volumes

Shows all provisioned Docker volumes.

Terminal window
docker volume ls

Create Persistent Volume

Creates a new persistent data volume.

Terminal window
docker volume create <volume_name>
Example
docker volume create pg_data

Remove Docker Volume

Deletes an unused Docker data volume.

Terminal window
docker volume rm <volume_name>
Example
docker volume rm pg_data

Run With Bind Mount

Mounts a host directory into a container.

Terminal window
docker run -v <host_path>:<container_path> <image_name>
Example
docker run -v /host/data:/app/data my-app

Networks and Custom Bridges

Commands for managing networks and custom bridge configurations.

List Docker Networks

Displays all active Docker networks.

Terminal window
docker network ls

Create Custom Bridge Network

Creates a custom user-defined bridge network.

Terminal window
docker network create --driver bridge <network_name>
Example
docker network create --driver bridge app_net

Connect Container To Network

Attaches a container to a specified network.

Terminal window
docker network connect <network_name> <container_id>
Example
docker network connect app_net web_app

Disconnect Container Network

Detaches a container from a network.

Terminal window
docker network disconnect <network_name> <container_id>
Example
docker network disconnect app_net web_app

Docker Compose

Commands for managing multi-container applications using Compose.

Start Compose Services

Starts all services defined in configuration files.

Terminal window
docker compose up -d

Stop Compose Services

Stops and removes active container services.

Terminal window
docker compose down

Restart Compose Services

Restarts all running multi-container services.

Terminal window
docker compose restart

View Compose Logs

Displays real-time logs for all services.

Terminal window
docker compose logs -f

List Compose Services

Shows all active services and container statuses.

Terminal window
docker compose ps

Execute Service Command

Runs a command inside a running service container.

Terminal window
docker compose exec <service_name> <command>
Example
docker compose exec app bash

Build Compose Services

Builds images for services defined in configuration.

Terminal window
docker compose build

Docker Security Best Practices

Commands and configurations for enforcing container security hardening.

Run As Non Root

Executes container processes using a non-root user ID.

Terminal window
docker run --user <uid>:<gid> <image_name>
Example
docker run --user 1000:1000 my-app

Scan Image Vulnerabilities

Analyzes container images for known security vulnerabilities.

Terminal window
docker scout quickview <image_name>
Example
docker scout quickview nginx:latest

Check Rootless Status

Verifies if the Docker daemon runs in rootless mode.

Terminal window
docker info --format '{{.SecurityOptions}}'
Look for rootless in security options output.

Docker Secrets

Commands for securely managing sensitive configuration data.

Create Docker Secret

Creates a new encrypted secret object in Swarm.

Terminal window
echo "<secret_value>" | docker secret create <secret_name> -
Example
echo "my_password" | docker secret create db_password -

List Docker Secrets

Displays all stored secret objects.

Terminal window
docker secret ls

Inspect Docker Secret

Displays low-level metadata of a secret object.

Terminal window
docker secret inspect <secret_name>
Example
docker secret inspect db_password

Remove Docker Secret

Deletes an existing secret object.

Terminal window
docker secret rm <secret_name>
Example
docker secret rm db_password

Docker Swarm Basics

Commands for orchestrating multi-node container clusters.

Initialize Docker Swarm

Initializes the current node as a Swarm manager.

Terminal window
docker swarm init

Join Swarm Cluster

Connects a worker or manager node to a cluster.

Terminal window
docker swarm join --token <token> <manager_ip>:<port>
Example
docker swarm join --token SWMTKN-1-xx 192.168.1.10:2377

List Swarm Nodes

Displays all nodes participating in the Swarm cluster.

Terminal window
docker node ls

Create Swarm Service

Deploys a replicated service across the Swarm.

Terminal window
docker service create --name <service_name> --replicas <num> <image_name>
Example
docker service create --name web --replicas 3 nginx

List Swarm Services

Displays all active services running in the Swarm.

Terminal window
docker service ls

System Cleanup

Commands for cleaning up unused system resources.

Remove Stopped Containers

Deletes all stopped containers to reclaim space.

Terminal window
docker container prune

Remove Unused Images

Deletes unreferenced dangling and unused images.

Terminal window
docker image prune

Remove Unused Volumes

Deletes orphaned volumes not used by containers.

Terminal window
docker volume prune

Remove Unused Networks

Cleans up unused custom networks.

Terminal window
docker network prune

View System Disk Usage

Displays disk space utilization by Docker objects.

Terminal window
docker system df