Docker Tutorial
Docker is an open-source application container engine, based on the Go language and open-sourced under the Apache 2.0 license.
Docker allows developers to package their applications and dependencies into a lightweight, portable container, which can then be published to any popular Linux machine, enabling virtualization as well.
Containers use a complete sandbox mechanism and have no interfaces with each other (similar to iPhone apps); more importantly, containers have very low performance overhead.
Since version 17.03, Docker is divided into CE (Community Edition) and EE (Enterprise Edition); the Community Edition is sufficient for us.
Who should read this tutorial?
This tutorial is suitable for operations engineers and backend developers; through it you can learn how to use Docker step by step.
What you need to know before reading this tutorial
Before reading this tutorial, you need to master common Linux commands. You can use the link on this site Linux Tutorial to learn the relevant commands.
Docker application scenarios
-
Microservices architecture:Each service is containerized independently, making it easy to manage and scale.
-
CI/CD pipeline:Integrate with Jenkins/GitLab CI to achieve automated builds and testing.
-
Standardized development environment:New members can start the full set of dependent services (such as databases and message queues) with one click.
-
Cloud-native foundation:Orchestration tools such as Kubernetes manage container clusters based on Docker.
Core Advantages
- ** Cross-platform consistency:** Solve the "it works on my machine" problem, ensuring consistency across development, testing, and production environments.
- ** Resource efficiency:** Containers share the host kernel directly, with no need to virtualize an entire operating system, saving memory and CPU.
- ** Rapid deployment:** Containers start in seconds and support automated scaling.
- ** Isolation:** Each container has its own independent filesystem, network, and process space.
Core Concepts
- ** Container:** A lightweight running instance that contains application code, the runtime environment, and dependency libraries. Created from an image, it is isolated from other containers and shares the host operating system kernel (more efficient than a virtual machine).
- ** Image:** A read-only template that defines a container's runtime environment (such as the operating system and software configuration). Layered storage (Layer) optimizes space and build speed.
- ** Dockerfile:** A text file that describes how to automatically build an image (for example, specifying a base image, installing software, copying files, etc.).
- ** Registry:** A platform for storing and distributing images, such as Docker Hub (the official public registry) or a private registry (such as Harbor).
Basic commands
Example
Pull an image (such as the official Nginx image)
docker pull nginx
Run a container (-d runs in the background, -p maps ports)
docker run
-d
-p
80
:
80
nginx
View running containers
docker
ps
Build an image (based on the Dockerfile in the current directory)
docker build
-t
my-app .
Enter a container
docker
exec
-it
<
Container ID
/
bin
/
bash
Related links
Docker official website:https://www.docker.com
GitHub Docker source code:https://github.com/docker