Why Docker? A Practical Guide to Containers, Dockerfile, and Compose
This article explains why Docker solves traditional development environment problems, compares it with virtual machines, introduces Docker basics, Dockerfile syntax, Docker Compose for multi‑service orchestration, and highlights related CI/CD and cloud‑native benefits, providing useful resources and examples.
Why Docker?
Traditional web projects often require many server‑side programs (PHP, MySQL, Redis, Node, etc.) that must be installed manually, leading to version conflicts, configuration clashes, and high manual deployment effort. Virtual machines solve some issues but introduce large images, slow startup, and lack automation. Docker provides lightweight, fast‑starting, isolated containers with automated image building and a shared hub for distribution.
Docker Basics
Docker runs on Windows, Linux, macOS, AWS, and Azure (Windows requires Win10+, macOS requires EI Captain+). It follows a client‑server architecture; after installation, the Docker daemon must be started before using Docker commands.
Docker core concepts include Dockerfile, Image, Container, and Repository. An image is built from a Dockerfile and can be pushed to Docker Hub or pulled from it. A container is an instance of an image.
$ docker run [organization/]<image_name>[:tag]
Parameters are optional; the default organization is library and the default tag is latest. Running docker run hello-world pulls the image if absent and executes its entrypoint.
Running an Nginx container with volume and port mappings:
$ docker run \ -d \ --rm \ -p 8080:80 \ -v "$PWD/workspace":/var/www/hello.world \ -v "$PWD/hello.world.conf":/etc/nginx/conf.d/hello.world.conf \ nginx
Note: data inside a container is lost when the container is removed; use mounted volumes or external storage for persistence.
Dockerfile
A Dockerfile defines how to build an image. Benefits include text‑based version control, layer reuse, and automated builds.
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 be present).
Example Dockerfile for a Node application:
FROM mhart/alpine-node:8.9 LABEL maintainer="lizheming <[email protected]>" LABEL org.label-schema.name="Drone Wechat Notification" LABEL org.label-schema.vendor="lizheming" LABEL org.label-schema.version="1.1.0" 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:
$ docker build lizheming/drone-wechat:latest
After building, push the image to Docker Hub.
Docker Compose
Docker Compose orchestrates multiple containers defined in a YAML file, eliminating the need to run many docker run commands manually.
Install with pip install docker-compose, then use docker-compose up and docker-compose stop to manage services.
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 handles service name resolution via hosts entries, allowing Nginx to proxy to php:9000 directly.
server { listen 80; server_name dev.m.look.360.cn; root /home/q/system/m_look_360_cn/public; location ~ \.php$ { fastcgi_pass php:9000; fastcgi_index index.php; } }
Docker Related
Beyond deployment, Docker enables CI/CD pipelines, and together with Kubernetes or Docker Swarm provides elastic scaling and resource optimization.
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.
