Skip to content

Docker Quick Start

This chapter will quickly introduce you to Docker's core features through hands-on operations, running your first container, and understanding basic Docker commands.

Running Your First Container

Hello World Container

Let's start with the simplest example:

bash
# Run Hello World container
docker run hello-world

Sample Output:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:7d91b69e04a9029b99f3585aaaccae2baa80bcf318f4a5d2165a9898cd2dc0a1
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.
...

What happened in this process?

  1. Docker client contacts Docker daemon
  2. Daemon pulls "hello-world" image from Docker Hub
  3. Daemon creates new container from image and runs it
  4. Container outputs message and exits

Running Interactive Container

bash
# Run Ubuntu container and enter interactive mode
docker run -it ubuntu:20.04 /bin/bash

Parameter Description:

  • -i: Keep STDIN open
  • -t: Allocate a pseudo-terminal
  • ubuntu:20.04: Image name and tag
  • /bin/bash: Command to execute

Operations inside container:

bash
# You are now inside Ubuntu container
root@container_id:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

# View system information
root@container_id:/# cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
...

# Install packages
root@container_id:/# apt update && apt install -y curl

# Exit container
root@container_id:/# exit

Basic Docker Commands

bash
# List local images
docker images
# or
docker image ls

# Search images
docker search nginx

# Pull image
docker pull nginx:latest

# Remove image
docker rmi hello-world

# View detailed image information
docker image inspect nginx
bash
# List running containers
docker ps

# List all containers (including stopped ones)
docker ps -a

# Start container
docker start container_name

# Stop container
docker stop container_name

# Restart container
docker restart container_name

# Remove container
docker rm container_name

# View container logs
docker logs container_name

# Enter running container
docker exec -it container_name /bin/bash

Practical Example: Running Web Server

Running Nginx Server

bash
# Run Nginx container
docker run -d -p 8080:80 --name my-nginx nginx:latest

Parameter Description:

  • -d: Run in background (daemon mode)
  • -p 8080:80: Port mapping, map host's port 8080 to container's port 80
  • --name my-nginx: Specify container name

Verify Service:

bash
# Check container status
docker ps

# Access web service
curl http://localhost:8080
# Or visit http://localhost:8080 in browser

Custom Nginx Content

bash
# Create custom HTML file
mkdir -p ~/docker-demo
echo "<h1>Hello Docker!</h1>" > ~/docker-demo/index.html

# Run Nginx and mount custom content
docker run -d -p 8081:80 -v ~/docker-demo:/usr/share/nginx/html --name custom-nginx nginx:latest

# Access custom content
curl http://localhost:8081

Container Lifecycle Management

Creating and Managing Containers

bash
# Create but don't start container
docker create --name my-ubuntu ubuntu:20.04

# Start created container
docker start my-ubuntu

# Stop running container
docker stop my-ubuntu

# Restart container
docker restart my-ubuntu

# Pause container
docker pause my-ubuntu

# Resume paused container
docker unpause my-ubuntu

# Remove container
docker rm my-ubuntu

Container Status Viewing

bash
# View detailed container information
docker inspect container_name

# View container resource usage
docker stats container_name

# View container processes
docker top container_name

# View container port mappings
docker port container_name

Data Management Basics

Data Volumes

bash
# Create data volume
docker volume create my-volume

# List data volumes
docker volume ls

# Run container using data volume
docker run -d -v my-volume:/data --name data-container ubuntu:20.04

# View detailed data volume information
docker volume inspect my-volume

Bind Mounts

bash
# Mount host directory to container
docker run -d -v /host/path:/container/path ubuntu:20.04

# Read-only mount
docker run -d -v /host/path:/container/path:ro ubuntu:20.04

Network Basics

Port Mapping

bash
# Map single port
docker run -p 8080:80 nginx

# Map multiple ports
docker run -p 8080:80 -p 8443:443 nginx

# Map to random port
docker run -P nginx

# View port mapping
docker port container_name

Container Networks

bash
# List networks
docker network ls

# Create custom network
docker network create my-network

# Run container in specified network
docker run --network my-network nginx

# Connect container to network
docker network connect my-network container_name

Practical Exercises: Setting Up Development Environment

Exercise 1: Run Database

bash
# Run MySQL database
docker run -d \
  --name mysql-db \
  -e MYSQL_ROOT_PASSWORD=mypassword \
  -e MYSQL_DATABASE=testdb \
  -p 3306:3306 \
  mysql:8.0

# Connect to database
docker exec -it mysql-db mysql -u root -p

Exercise 2: Run Redis Cache

bash
# Run Redis
docker run -d --name redis-cache -p 6379:6379 redis:latest

# Connect to Redis
docker exec -it redis-cache redis-cli

Exercise 3: Run Node.js Application

bash
# Create simple Node.js application
mkdir node-app && cd node-app

# Create package.json
cat > package.json << EOF
{
  "name": "docker-node-app",
  "version": "1.0.0",
  "main": "app.js",
  "dependencies": {
    "express": "^4.18.0"
  }
}
EOF

# Create app.js
cat > app.js << EOF
const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello Docker from Node.js!');
});

app.listen(port, () => {
  console.log(\`App listening at http://localhost:\${port}\`);
});
EOF

# Run Node.js container
docker run -d \
  --name node-app \
  -p 3000:3000 \
  -v $(pwd):/usr/src/app \
  -w /usr/src/app \
  node:16 \
  sh -c "npm install && node app.js"

Common Commands Quick Reference

Image Operations

bash
docker images                    # List images
docker pull <image>             # Pull image
docker rmi <image>              # Remove image
docker build -t <name> .        # Build image
docker tag <image> <new_name>   # Tag image

Container Operations

bash
docker run <image>              # Run container
docker ps                       # List running containers
docker ps -a                    # List all containers
docker stop <container>         # Stop container
docker start <container>        # Start container
docker rm <container>           # Remove container
docker exec -it <container> bash # Enter container
docker logs <container>         # View logs

System Operations

bash
docker system info             # System information
docker system df               # Disk usage
docker system prune            # Clean unused resources
docker version                 # Version information

Troubleshooting

Common Issues and Solutions

  1. Container Won't Start

    bash
    # View container logs
    docker logs container_name
    
    # View detailed container information
    docker inspect container_name
  2. Port Conflicts

    bash
    # Check port usage
    netstat -tulpn | grep :8080
    
    # Use different port
    docker run -p 8081:80 nginx
  3. Image Pull Fails

    bash
    # Check network connection
    ping docker.io
    
    # Use image accelerator
    docker pull registry.cn-hangzhou.aliyuncs.com/library/nginx:latest
  4. Container Out of Memory

    bash
    # Limit container memory usage
    docker run -m 512m nginx
    
    # View container resource usage
    docker stats

Best Practice Tips

  1. Use Specific Image Tags

    bash
    # Good practice
    docker run nginx:1.21.6
    
    # Avoid using
    docker run nginx:latest
  2. Clean Up Resources Promptly

    bash
    # Clean stopped containers
    docker container prune
    
    # Clean unused images
    docker image prune
    
    # Clean all unused resources
    docker system prune -a
  3. Use Meaningful Container Names

    bash
    # Good practice
    docker run --name web-server nginx
    
    # Avoid random names
    docker run nginx

Chapter Summary

Through this chapter, you have mastered Docker's basic operations:

Key Points:

  • Learned to run your first Docker container
  • Mastered basic Docker commands
  • Understood container lifecycle management
  • Learned port mapping and data mounting
  • Experienced Docker's convenience through practical examples

You can now:

  • Run various pre-built containers
  • Manage container startup, stop, and deletion
  • Perform basic network and storage configuration
  • Troubleshoot common container issues

In the next chapter, we will dive deep into Docker's core concepts, including detailed principles of important concepts like images, containers, and repositories.

Further Reading

Content is for learning and research only.