Docker Command Manual
# Docker Command Manual: Complete Reference Guide
Docker is an open-source platform designed to help developers build, ship, run, and manage applications within lightweight, isolated environments called containers.
This comprehensive manual serves as a quick-reference guide for the most frequently used Docker commands, categorized by their functional areas: container lifecycle management, image operations, networking, volumes, and Docker Compose.
---
## 1. Container Lifecycle Management
These commands control the state transitions of a container, from creation and execution to pausing, stopping, and deletion.
| Command | Description | Common Syntax |
| :--- | :--- | :--- |
| **`run`** | Creates and starts a new container from an image. | `docker run image ` |
| **`start`** | Starts one or more stopped containers. | `docker start container [container...]` |
| **`stop`** | Gracefully stops one or more running containers. | `docker stop container [container...]` |
| **`restart`** | Restarts one or more running containers. | `docker restart container [container...]` |
| **`kill`** | Immediately terminates (SIGKILL) one or more running containers. | `docker kill container [container...]` |
| **`rm`** | Removes one or more stopped containers. | `docker rm container [container...]` |
| **`pause`** | Suspends all processes within one or more containers. | `docker pause container [container...]` |
| **`unpause`** | Resumes all processes within one or more paused containers. | `docker unpause container [container...]` |
| **`create`** | Creates a new container but does not start it. | `docker create image ` |
| **`exec`** | Runs a new command inside an active, running container. | `docker exec container command [args...]` |
| **`rename`** | Renames an existing container. | `docker rename container new_name` |
### Code Examples
```bash
# Run an Nginx container in the background (detached mode) and map port 80
docker run -d -p 8080:80 --name my-nginx nginx
# Execute an interactive bash shell inside the running Nginx container
docker exec -it my-nginx /bin/bash
# Stop and remove the container
docker stop my-nginx
docker rm my-nginx
```
---
## 2. Container Operations & Monitoring
These commands are used to inspect, monitor, and interact with active or inactive containers.
| Command | Description | Common Syntax |
| :--- | :--- | :--- |
| **`ps`** | Lists containers (defaults to running containers only). | `docker ps ` |
| **`inspect`** | Returns low-level, detailed JSON information on Docker objects. | `docker inspect name|id [name|id...]` |
| **`top`** | Displays the running processes of a container. | `docker top container ` |
| **`attach`** | Attaches local standard input, output, and error streams to a running container. | `docker attach container` |
| **`events`** | Gets real-time events from the Docker daemon. | `docker events ` |
| **`logs`** | Fetches and views the log output of a container. | `docker logs container` |
| **`wait`** | Blocks until one or more containers stop, then prints their exit codes. | `docker wait container [container...]` |
| **`export`** | Exports a container's filesystem as a tar archive. | `docker export container` |
| **`port`** | Lists port mappings or a specific mapping for the container. | `docker port container [private_port]` |
| **`stats`** | Displays a live stream of container resource usage statistics (CPU, Memory, Network). | `docker stats [container...]` |
| **`update`** | Dynamically updates resource configurations (CPU, memory limits) of a container. | `docker update container [container...]` |
### Code Examples
```bash
# List all containers, including stopped ones
docker ps -a
# View the last 50 lines of a container's logs and follow the output in real-time
docker logs --tail 50 -f my-nginx
# Monitor CPU and memory usage of all running containers
docker stats
```
---
## 3. Container Root Filesystem (rootfs) Operations
These commands manage the files and directories inside a container's filesystem layer.
| Command | Description | Common Syntax |
| :--- | :--- | :--- |
| **`commit`** | Creates a new image from a container's current changes. | `docker commit container [repository[:tag]]` |
| **`cp`** | Copies files/folders between a container and the local filesystem. | `docker cp container:src_path dest_path` |
| **`diff`** | Inspects changes to files or directories on a container's filesystem. | `docker diff container` |
### Code Examples
```bash
# Copy a local configuration file into a container
docker cp ./nginx.conf my-nginx:/etc/nginx/nginx.conf
# Check what files have changed in the container since it was started
docker diff my-nginx
```
---
## 4. Image Registry Operations
These commands manage authentication and image transfers between your local Docker engine and remote registries (such as Docker Hub or private registries).
| Command | Description | Common Syntax |
| :--- | :--- | :--- |
| **`login`** | Log in to a Docker registry. | `docker login ` |
| **`logout`** | Log out from a Docker registry. | `docker logout ` |
| **`pull`** | Pulls (downloads) an image or a repository from a registry. | `docker pull name[:tag|@digest]` |
| **`push`** | Pushes (uploads) an image or a repository to a registry. | `docker push name[:tag]` |
| **`search`** | Searches Docker Hub for images. | `docker search term` |
### Code Examples
```bash
# Log in to a private registry
docker login registry.example.com
# Pull the latest Ubuntu image from Docker Hub
docker pull ubuntu:latest
# Push a tagged image to your repository
docker push username/my-app:v1.0
```
---
## 5. Local Image Management
These commands manage the lifecycle of Docker images stored locally on your host machine.
| Command | Description | Common Syntax |
| :--- | :--- | :--- |
| **`images`** | Lists locally stored Docker images. | `docker images [repository[:tag]]` |
| **`rmi`** | Removes one or more local images. | `docker rmi image [image...]` |
| **`tag`** | Creates a tag (alias) pointing to a target image. | `docker tag source_image[:tag] target_image[:tag]` |
| **`build`** | Builds a Docker image from a Dockerfile. | `docker build path | url | -` |
| **`history`** | Shows the history of an image, including its layers. | `docker history image` |
| **`save`** | Saves one or more images to a tar archive (useful for offline transfers). | `docker save image [image...]` |
| **`load`** | Loads an image from a tar archive or STDIN. | `docker load ` |
| **`import`** | Imports the contents from a tarball to create a filesystem image. | `docker import file|URL|- [repository[:tag]]` |
### Code Examples
```bash
# Build an image from a Dockerfile in the current directory
docker build -t my-custom-app:1.0 .
# Save an image to a tar file
docker save -o my-image.tar my-custom-app:1.0
# Load the image on another machine
docker load -i my-image.tar
```
---
## 6. System Information & Versioning
These commands provide diagnostic information about your Docker environment.
| Command | Description | Common Syntax |
| :--- | :--- | :--- |
| **`info`** | Displays system-wide information regarding the Docker installation. | `docker info` |
| **`version`** | Shows the Docker version information for both client and server. | `docker version` |
---
## 7. Docker Compose Commands
Docker Compose is a tool for defining and running multi-container Docker applications. Below are the essential commands for managing multi-container environments.
| Command | Description |
| :--- | :--- |
| **`docker compose run`** | Runs a one-off command on a service defined in your compose file. |
| **`docker compose rm`** | Removes stopped service containers. |
| **`docker compose ps`** | Lists containers associated with the compose project and their states. |
| **`docker compose build`** | Builds or rebuilds services defined in the compose file. |
| **`docker compose up`** | Builds, (re)creates, starts, and attaches to containers for a service. |
| **`docker compose ls`** | Lists running compose projects. |
| **`docker compose start`** | Starts existing containers for a service. |
| **`docker compose restart`** | Restarts service containers. |
| **`docker compose down`** | Stops and removes containers, networks, volumes, and images created by `up`. |
### Code Examples
```bash
# Start all services defined in docker-compose.yml in the background
docker compose up -d
# Check the status of the services
docker compose ps
# Stop and clean up containers and networks
docker compose down
```
---
## 8. Network Management
Docker networks enable containers to communicate with each other and with the host machine or external networks.
| Command | Description |
| :--- | :--- |
| **`docker network ls`** | Lists all networks managed by Docker. |
| **`docker network create `** | Creates a new user-defined network (e.g., bridge, overlay). |
| **`docker network rm `** | Removes one or more networks. |
| **`docker network connect `** | Connects a running container to a network. |
| **`docker network disconnect `** | Disconnects a container from a network. |
### Code Examples
```bash
# Create a custom bridge network
docker network create my-custom-net
# Connect an existing container to the new network
docker network connect my-custom-net my-nginx
```
---
## 9. Volume Management
Volumes are the preferred mechanism for persisting data generated by and used by Docker containers.
| Command | Description |
| :--- | :--- |
| **`docker volume ls`** | Lists all local volumes. |
| **`docker volume create `** | Creates a new named volume. |
| **`docker volume rm `** | Removes one or more volumes (must not be in use by any container). |
| **`docker volume inspect `** | Displays detailed information about a specific volume. |
### Code Examples
```bash
# Create a persistent volume for database storage
docker volume create db-data
# Inspect the volume to find its mount point on the host
docker volume inspect db-data
```
---
## Considerations & Best Practices
1. **Container Ephemerality**: Containers should be treated as ephemeral. Never store critical application data inside a container's writable layer; always use **Docker Volumes** or **Bind Mounts** to persist data.
2. **Resource Limits**: When running containers in production, always use resource limit flags (e.g., `--memory` and `--cpus` in `docker run` or `docker update`) to prevent a single container from consuming all host resources.
3. **Clean Up Unused Resources**: Docker can consume significant disk space over time. Use the system prune command regularly to clean up dangling resources:
```bash
# Remove stopped containers, unused networks, dangling images, and build caches
docker system prune -f
# To also clean up unused volumes:
docker system prune --volumes -f
```
YouTip