Skip to content

docker

Docker commands categorized for managing containers, images, networks, volumes and more.

Containers

Commands for managing running and stopped containers.

List all containers

Shows running containers by default, use `-a` to show all.

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

Start a container

Starts a stopped container.

Terminal window
docker start <container_id>
Replace `<container_id>` with the actual container ID or name.

Stop a container

Stops a running container.

Terminal window
docker stop <container_id>
Gracefully stops a container. Use `docker kill` to force stop.

Remove a container

Deletes a stopped container.

Terminal window
docker rm <container_id>
Use `-f` to force remove a running container.

Images

Commands for managing Docker images.

List images

Displays a list of all locally stored images.

Terminal window
docker images
Shows repository, tag, and image ID.

Remove an image

Deletes a Docker image from local storage.

Terminal window
docker rmi <image_id>
Use `docker image prune` to remove all unused images.

Pull an image

Downloads an image from a Docker registry.

Terminal window
docker pull <image_name>
Use `docker pull <image>:<tag>` to specify a version.

Build an image

Builds a Docker image from a Dockerfile.

Terminal window
docker build -t <image_name> .
The `-t` flag assigns a name to the image.

Volumes

Commands for managing Docker volumes.

List volumes

Shows all Docker volumes.

Terminal window
docker volume ls
Volumes persist data independently of containers.

Create a volume

Creates a new Docker volume.

Terminal window
docker volume create <volume_name>
Useful for persistent storage.

Remove a volume

Deletes a Docker volume.

Terminal window
docker volume rm <volume_name>
Ensure the volume is not in use before deleting.

Networks

Commands for managing Docker networks.

List networks

Displays all Docker networks.

Terminal window
docker network ls
Lists bridge, host, and custom networks.

Create a network

Creates a custom Docker network.

Terminal window
docker network create <network_name>
Use `--driver` to specify the network type.

Connect a container to a network

Attaches a container to a specified network.

Terminal window
docker network connect <network_name> <container_id>
Allows inter-container communication.

Disconnect a container from a network

Detaches a container from a network.

Terminal window
docker network disconnect <network_name> <container_id>
Useful for network isolation.

Docker Compose

Commands for managing multi-container applications using Docker Compose.

Start services

Starts all services defined in the `docker-compose.yml` file.

Terminal window
docker compose up -d
Use `-d` to run in detached mode.

Stop services

Stops all running services.

Terminal window
docker compose down
Stops and removes containers, networks, and volumes if specified.

Restart services

Restarts all running services.

Terminal window
docker compose restart
Useful for applying config changes without stopping everything.

View service logs

Displays logs for all services.

Terminal window
docker compose logs -f
Use `-f` to follow logs in real time.

List running services

Shows all active services.

Terminal window
docker compose ps
Displays container names, status, and ports.

Execute a command in a service container

Runs a command inside a running service.

Terminal window
docker compose exec <service_name> <command>
Example: `docker compose exec app bash` for interactive shell.

Build images

Builds images for services defined in the `docker-compose.yml` file.

Terminal window
docker compose build
Use `--no-cache` to force a fresh build.

Docker Build

Commands for building and managing Docker images.

Build an image from a Dockerfile

Creates a Docker image from a Dockerfile in the current directory.

Terminal window
docker build -t <image_name> .
The `-t` flag assigns a name to the image.

Build an image with no cache

Forces a fresh build without using cached layers.

Terminal window
docker build --no-cache -t <image_name> .
Useful when ensuring all layers are rebuilt.

Build an image with a specific Dockerfile

Specifies a custom Dockerfile for the build.

Terminal window
docker build -f <Dockerfile> -t <image_name> .
Allows using a different Dockerfile than the default.

Build and push an image in one command

Builds and directly pushes the image to a registry.

Terminal window
docker buildx build --push --tag <registry>/<image_name>:<tag> .
Requires BuildKit and authentication to the registry.

Show build cache usage

Displays information about the build cache.

Terminal window
docker system df
Use `docker builder prune` to remove unused cache.

System Cleanup

Commands for cleaning up unused Docker resources.

Remove all stopped containers

Deletes all stopped containers.

Terminal window
docker container prune
Frees up disk space.

Remove all unused images

Deletes all dangling images.

Terminal window
docker image prune
Use `docker image prune -a` to remove all unused images.

Remove all unused volumes

Deletes all unused volumes.

Terminal window
docker volume prune
Useful for clearing orphaned volumes.

Remove all unused networks

Deletes all unused networks.

Terminal window
docker network prune
Cleans up unused networks to free space.

Miscellaneous

Various useful Docker commands that don't fit into other categories.

Run a container interactively

Starts a container and opens an interactive shell session.

Terminal window
docker run -it <image_name> /bin/bash
Use `--rm` to automatically remove the container after exit.

Execute a command as a specific user

Runs a command inside a running container.

Terminal window
docker exec -u 1000:1000 -it <container_id> /bin/bash
Replace `1000:1000` with the desired UID:GID.

Copy files from container to host

Copies files from a running container to the local filesystem.

Terminal window
docker cp <container_id>:/path/to/file /destination/path
Use `docker cp /local/path <container_id>:/path/to/file` to copy to the container.

Get detailed container information

Displays low-level information about a container.

Terminal window
docker inspect <container_id>
Provides detailed metadata, including network settings and mounts.

Get container resource usage stats

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

Terminal window
docker stats
Useful for monitoring container performance.

Get container logs in real-time

Shows logs from a running container.

Terminal window
docker logs -f <container_id>
Use `-f` to follow logs live, or `--tail <number>` to show last N lines.