Cloud Native 12 min read

Why Docker? A Practical Guide to Containerizing Development Environments

This article explains why Docker is essential for modern development, compares it with traditional VM approaches, introduces core concepts such as images, containers, Dockerfiles, and Docker Compose, and provides practical commands and examples for building, running, and orchestrating services efficiently.

21CTO
21CTO
21CTO
Why Docker? A Practical Guide to Containerizing Development Environments

Why Docker?

Traditional web services often depend on many programs (PHP, MySQL, Redis, Node, etc.). Installing them manually leads to version conflicts, shared configuration files, and high manual effort for deployment across multiple machines. Virtual machines solve some issues but introduce large image sizes, slow startup, and lack automation.

Docker addresses these problems with lightweight images, fast startup, sandbox isolation, automated image builds via Dockerfiles, and a shared hub for image 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 installing Docker, the daemon must be started before using docker commands.

Key concepts: Dockerfile, Image, Container, Repository. An image is built from a Dockerfile and can be pushed to Docker Hub. A container is an instance of an image. Example to run an image: $ docker run [organization]/[image_name]:[tag] If the organization is omitted, it defaults to library; if the tag is omitted, it defaults to latest. Running the official hello-world image pulls it from Docker Hub and executes its entrypoint.

Running an Nginx Container

$ 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

This maps host port 8080 to container port 80 and mounts the workspace directory into the container. Access the service at http://hello.world:8080.

Note: Do not store data inside a container; use volumes or external storage to persist data.

Dockerfile

A Dockerfile defines how to build an image. Benefits include version‑controlled, incremental layers that reduce image size.

Use # for comments. FROM specifies the base image. RUN executes commands during build. COPY copies files into the image. WORKDIR sets the working directory. CMD or ENTRYPOINT defines the container’s startup command (only one may be present).

Example Dockerfile for a 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 After building, push the image to Docker Hub.

Docker Compose

When a project requires multiple services, Docker Compose orchestrates them via a YAML file, eliminating the need to run many separate docker run commands.

Install with pip install docker-compose, then use docker-compose up and docker-compose stop to manage the stack.

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 automatically resolves container dependencies and injects service names into each container’s /etc/hosts, allowing Nginx to proxy to php:9000 directly.

Docker‑Related Benefits

Facilitates CI/CD pipelines for agile development.

Integrates with Kubernetes and Docker Swarm for elastic scaling and resource optimization.

Learning Resources

What is Docker?

Docker from Beginner to Practice

Docker Compose Detailed Guide

Deep Dive into Docker

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DevOpsDockerfileDocker Compose
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.