Why Docker? A Practical Guide to Containers, Dockerfile & Compose
This article explains why Docker solves traditional development environment problems, compares virtual machines with container technology, introduces Docker basics, Dockerfile syntax, Docker Compose orchestration, and highlights related benefits such as CI/CD automation and Kubernetes integration.
Why Docker?
Traditional web services often depend on many programs (PHP, MySQL, Redis, Node, etc.) that must be installed manually, leading to version conflicts, shared configuration file modifications, and high manual deployment effort.
Different services may require different versions of the same software (e.g., Python2 vs Python3, PHP7 vs PHP5.6).
Multiple services can modify the same configuration files, causing interference.
Manual installation on each machine wastes considerable labor.
Packaging an entire system into a virtual machine solves some issues but introduces new problems: large image size, slow startup, and lack of automation.
VM images contain the whole OS, making them very large.
Services must wait for the OS to boot before they can start.
The packaging process cannot be fully automated.
Docker, as a process‑level virtualization technology, offers several advantages:
Small image size because the host OS is not packaged.
Fast startup with low resource consumption.
Sandbox isolation ensures environment separation between services.
Dockerfile enables automated image building.
Docker Hub provides a convenient image sharing platform.
Below is a visual comparison of VM and Docker technologies (VM packages a guest OS, Docker virtualizes directly on the host).
Docker Basics
Docker runs on Windows, Linux, macOS, AWS, and Azure (Windows requires Win10+, macOS requires El Capitan+). It follows a client/server architecture; after installation, the Docker daemon must be started before using Docker commands.
Docker has four core concepts: Dockerfile, Image, Container, and Repository. An Image is built from a Dockerfile, can be pushed to Docker Hub, and instantiated as a Container. A simple command to run an image is:
$ docker run [organization/]<image_name>[:tag]
If the organization is omitted, it defaults to library; if the tag is omitted, it defaults to latest. For example, running the hello-world image pulls it from Docker Hub if not present locally and then executes its entrypoint.
Dockerfile
Dockerfile is the core of Docker image creation. It provides two main benefits:
Text‑based image generation enables version control and automated deployment.
Each instruction creates a new layer, allowing incremental updates and layer reuse, which reduces image size.
Key Dockerfile directives include:
Use # for comments. FROM specifies the base image. RUN executes commands during image build (e.g., installing packages). COPY copies files into the image. WORKDIR sets the working directory. CMD or ENTRYPOINT defines the container’s startup command (only one can exist; ENTRYPOINT is generally preferred).
Example Dockerfile for a simple Node script:
FROM mhart/alpine-node:8.9 LABEL maintainer="lizheming <[email protected]>" WORKDIR /wechat COPY package.json /wechat/package.json RUN npm install --production --registry=https://registry.npm.taobao.org COPY index.js /wechat/index.js ENTRYPOINT ["node", "/wechat/index.js"]
Build the image with:
$ docker build lizheming/drone-wechat:latest
Docker Compose
Complex projects often require multiple services, and a single container’s ENTRYPOINT cannot handle them all. Docker Compose, a Python‑based orchestration tool, simplifies this by defining all services in a YAML file and managing them with docker‑compose up and docker‑compose stop.
Install Compose via pip: pip install docker-compose Sample docker-compose.yaml:
version: "2" services: nginx: depends_on: - php image: "nginx:latest" volumes: - "$PWD/src/docker/conf:/etc/nginx/conf.d" - "$PWD:/home/q/system/m_look_360_cn" ports: - "8082:80" container_name: "m.look.360.cn-nginx" php: image: "lizheming/php-fpm-yaf" volumes: - "$PWD:/home/q/system/m_look_360_cn" container_name: "m.look.360.cn-php"
Compose also injects service names into each container’s /etc/hosts, allowing direct name‑based access (e.g., php:9000 in an Nginx config).
Docker Related
Beyond simplifying deployment, Docker enables:
CI/CD pipelines for continuous integration and delivery.
Elastic scaling with Kubernetes or Docker Swarm, automatically adjusting resources based on load.
Learning Resources
What is Docker? https://cloud.tencent.com/developer/article/1005172
Docker from Beginner to Practice https://yeasy.gitbooks.io/docker_practice/
Docker Compose Detailed Guide https://www.jianshu.com/p/2217cfed29d7
Deep Dive into Docker https://www.kancloud.cn/infoq/docker/79768
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
