Docker is a powerful containerization platform that simplifies application deployement and management. It allows you to package your application and its dependencies into a single, lightweight container that can be easily transported and run on any system.
What is Docker?
Docker containers act as self-contained environments, isolating applications from the host system. Unlike traditional virtualization, Docker containers share the host's kernel, making them lighter and faster. The three core components of Docker are:
- Images: Read-only templates used to create containers. Think of them as blueprints.
- Containers: Running instances of Docker images. They host your application processes.
- Repositories: Storage locations for Docker images, accessible publicly or privately.
Installation Across Operating Systems
macOS
Install Docker using Homebrew with brew cask install docker. Configure a Chinese mirror for faster downloads by editing the Docker preferences. Example configuration:
{
"registry-mirrors": ["https://your_mirror_address.mirror.aliyuncs.com"]
}
Ubuntu
Install Docker with:
sudo apt-get install docker-ce docker-ce-cli containerd.io
Add your user to the Docker group:
sudo usermod -aG docker $USER
Windows 10
Download Docker Desktop from the official Docker website and install it. Configure a Chinese mirror in the settings for improved performance.
Working with Docker Images
Basic Operations
Search for images using docker search. For example:
docker search mysql
Pull an image with docker pull. Specify a tag for a specific version:
docker pull mysql:5.7
List your local images with docker images. Delete an image using docker rmi.
Creating and Committing Containers
Run a container interactively:
docker run -it --name mycontainer ubuntu:22.04 /bin/bash
Make changes to the container and commit them as a new image:
docker commit mycontainer your_username/myimage:1.0
Advanced Docker Concepts
UnionFS and Image Layers
Docker image are built using a layered filesystem (UnionFS). Each layer represents a change made to the image. This allows Docker to efficiently share layers between images, saving disk space and bandwidth.
Network Configuration
Connect containers to the host network using --network host. Map container ports to host ports with -p host\_port:container\_port.
Volumes for Data Persistence
Use volumes to persist data outside of containers. Mount a volume with docker run -v /host/path:/container/path.
Privileged Containers
Run containers with full host privileges using --privileged. This allows access to device files and kernel features, but use it cautiously as it reduces security isolation.
Using Docker Compose
Docker Compose lets you define multi-container applications with a YAML file. Example docker-compose.yml:
version: "3.8"
services:
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
networks:
- appnet
db:
image: postgres:13
environment:
POSTGRES_PASSWORD: example
networks:
- appnet
networks:
appnet:
driver: bridge
Common commands:
docker-compose up -d # Start services in detached mode
docker-compose ps # List running services
docker-compose down # Stop and remove services
Challenges
Build a Dockerized Spring Boot application that exposes a /hello endpoint on port 8080. Create a Docker image that includes your application and necessary dependencies.