Docker User Manual
This manual will help you get started quickly with Docker and master common operations for daily use.
Prerequisites
Before you begin, make sure you have:
- Docker installed (Docker Desktop or Docker Engine)
- Command line terminal access
- Basic command line operation knowledge
Basic Concepts Review
Images and Containers
- Image: A read-only template used to create containers
- Container: A running instance of an image
Common Commands
| Command | Function |
|---|---|
docker run | Create and run a container |
docker ps | View running containers |
docker images | View local images |
docker pull | Pull an image |
docker build | Build an image |
docker stop | Stop a container |
docker rm | Remove a container |
docker rmi | Remove an image |
Your First Container
Running Hello World
The simplest test is to run the hello-world image:
docker run hello-world
This command will:
- Check if hello-world image exists locally
- If not, pull from Docker Hub
- Create and run a container
- Display a welcome message and exit
Running an Interactive Container
Run an interactive Ubuntu container:
docker run -it ubuntu bash
Parameter explanation:
-i: Interactive mode-t: Allocate a pseudo-terminal
Inside the container, you can operate like using a normal Linux system. Type exit to leave the container.
Running Containers in Background
Run a container in background mode:
docker docker run -d nginx
Parameter explanation:
-d: Run in background (detached mode)
View running containers:
docker ps
Stop a background container:
docker stop <container ID or name>
Container Management
Viewing Containers
View running containers:
docker ps
View all containers (including stopped):
docker ps -a
View container details:
docker inspect <container name>
Starting and Stopping Containers
Start a stopped container:
docker start <container name>
Stop a running container:
docker stop <container name>
Restart a container:
docker restart <container name>
Removing Containers
Remove a stopped container:
docker rm <container name>
Force remove a running container:
docker rm -f <container name>
Remove all stopped containers:
docker container prune
Image Management
Viewing Images
List local images:
docker images
Or:
docker image ls
View image details:
docker image inspect <image name>
Pulling Images
Pull an image from Docker Hub:
docker pull ubuntu:latest
Pull a specific version:
docker pull nginx:1.25
Removing Images
Remove a local image:
docker rmi <image name>
Or:
docker image rm <image name>
Remove unused images:
docker image prune
Port Mapping
Containers are isolated by default. You need to map ports to access services inside containers.
Mapping Ports
Map host port 8080 to container port 80:
docker run -d -p 8080:80 nginx
Now visiting http://localhost:8080 will show the Nginx welcome page.
Common Port Mapping Formats
-p 8080:80: Map host 8080 to container 80-p 127.0.0.1:8080:80: Host-local access only-p 8080-8090:80-90: Port range mapping
Volumes
Volumes are used to persist data generated by containers, or share host files with containers.
Creating a Volume
docker volume create mydata
Using a Volume
docker run -d -v mydata:/app/data nginx
Mounting Host Directories
docker run -d -v /home/user/data:/app/data nginx
Viewing Volumes
docker volume ls
docker volume inspect mydata
Removing Volumes
docker volume rm mydata
docker volume prune # Remove unused volumes
Building Images
Dockerfile Basics
Create a simple Dockerfile:
# Specify base image
FROM node:18-alpine
# Set working directory
WORKDIR /app
# Copy files
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy source code
COPY . .
# Expose port
EXPOSE 3000
# Start command
CMD ["node", "index.js"]
Building an Image
docker build -t myapp:latest .
Parameter explanation:
-t: Specify image name and tag
Using .dockerignore
Create a .dockerignore file to exclude files:
node_modules
npm-debug.log
.git
README.md
Docker Compose
Docker Compose is used to define and run multi-container applications.
docker-compose.yml Example
version: '3.8'
services:
web:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=production
depends_on:
- db
db:
image: postgres:15
volumes:
- db-data:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=secret
volumes:
db-data:
Common Commands
Start all services:
docker-compose up
Run in background:
docker-compose up -d
Stop all services:
docker-compose down
View logs:
docker-compose logs -f
Rebuild and start:
docker-compose up --build
Networking
Viewing Networks
docker network ls
Creating a Network
docker docker network create mynetwork
Container Network Connection
Connect container to network:
docker network connect mynetwork mycontainer
Container Communication
When using Docker Compose, containers can communicate with each other using service names. For example:
services:
web:
build: .
depends_on:
- db
db:
image: postgres:15
In the web container, you can access the database using the hostname db.
Common Techniques
Enter a Running Container
docker exec -it <container name> bash
View Container Logs
docker logs -f <container name>
Copy Files
From container to host:
docker cp container:/path/in/container /local/path
From host to container:
docker cp /local/path container:/path/in/container
View Resource Usage
docker stats
View Container Processes
docker top <container name>
Cleanup
Clean up stopped containers, unused images, and build cache:
docker system prune
Complete cleanup:
docker system prune -a
Best Practices
Image Build Optimization
- Reduce image layers: Combine related commands
- Use multi-stage builds: Reduce final image size
- Use .dockerignore properly: Exclude unnecessary files
- Use specific version tags: Avoid using latest tag
Security Recommendations
- Don't run containers as root
- Use official verified images
- Regularly update base images
- Use Docker secrets for sensitive information (Swarm mode) or Kubernetes secrets
Resource Limits
Set resource limits for containers:
docker run -d --memory="512m" --cpus="1.5" nginx
Common Issues
Docker Permission Issues
If you encounter permission errors, add current user to docker group:
sudo usermod -aG docker $USER
Then log out and log back in.
Containers Exit Automatically
Containers will exit if there's no continuously running process. To keep running:
docker run -d nginx tail -f /dev/null
Or use a process manager like supervisor.
Port Conflicts
If a port is already in use, modify the mapped port:
docker run -d -p 8081:80 nginx
Getting Help
- Docker Official Documentation: https://docs.docker.com/
- Docker Hub: https://hub.docker.com/
- View command help:
docker --helpordocker <command> --help
