Master Docker: From Basics to Building Your Own WordPress Container
This comprehensive tutorial explains Docker's concepts, compares virtual machines with Linux containers, guides you through installation, image and container management, Dockerfile creation, building images, publishing them, and demonstrates three practical methods to deploy WordPress using Docker, including custom builds, official images, and Docker Compose.
Docker Overview
Docker, released in 2013, has attracted attention as a technology that may change the software industry. Many people are unclear about what Docker is, what problems it solves, and its benefits. This article explains Docker in detail, provides simple examples, and shows how to use it in daily development.
Environment Configuration Challenges
One of the biggest problems in software development is environment configuration. Users have different OS settings and library installations, making it hard to ensure software runs on all machines. Issues like "It works on my machine" arise when modules are incompatible. Changing machines requires reconfiguration, which is time‑consuming. A solution is to package the environment with the software.
Virtual Machines
Virtual machines (VMs) provide a solution by running a full OS inside another OS. VMs appear identical to real systems for applications, but have drawbacks: high resource consumption, redundant steps, and slow startup.
High resource usage: VMs reserve memory and disk space even for small applications.
Redundant steps: full OS requires login and other system‑level operations.
Slow startup: booting the OS takes minutes.
Linux Containers (LXC)
Linux containers address VM drawbacks by isolating processes rather than whole OSes. Containers start quickly, use fewer resources, and have smaller size. They act like lightweight VMs.
Fast start: containers are just processes.
Low resource usage: only needed resources are used.
Small size: only required components are included.
What is Docker?
Docker is a packaging of Linux containers. It provides a simple interface to use containers and is the most popular solution today. Docker bundles an application and its dependencies into an image file; running the image creates a virtual container that behaves like a real machine.
Docker Uses
Docker is mainly used for three purposes: providing disposable environments for testing or CI, offering elastic cloud services, and building microservice architectures by running multiple containers on a single host.
Docker Installation
Docker has Community Edition (CE) and Enterprise Edition (EE). Most developers use CE. Installation instructions are in the official docs. After installing, verify with: docker version Docker requires sudo; you can add your user to the docker group: sudo usermod -aG docker $USER Start the Docker service with service or systemctl commands.
# Using service
sudo service docker start
# Using systemctl
sudo systemctl start dockerImage Files
Images are binary files that serve as templates for containers. Multiple containers can be created from the same image. Images can be shared via registries like Docker Hub.
Dockerfile
A Dockerfile is a text file that defines how to build an image. Example Dockerfile for a Node app:
FROM node:8.4
COPY . /app
WORKDIR /app
RUN npm install --registry=https://registry.np.taobao.org
EXPOSE 3000Explanation of each line follows.
FROM node:8.4 – use official Node image version 8.4.
COPY . /app – copy source files (excluding .dockerignore paths) into the image.
WORKDIR /app – set working directory.
RUN npm install – install dependencies inside the image.
EXPOSE 3000 – expose port 3000.
Building the Image
docker image build -t koa-demo .
# or with tag
docker image build -t koa-demo:0.0.1 .Running a Container
docker container run -p 8000:3000 -it koa-demo /bin/bashExplanation of -p (port mapping), -it (interactive terminal), image name, and command.
Publishing Images
Login to Docker Hub, tag the image, and push:
docker login
docker image tag koa-demos:0.0.1 username/koa-demos:0.0.1
docker image push username/koa-demos:0.0.1Useful Docker Commands
docker container start – start a stopped container.
docker container stop – send SIGTERM then SIGKILL.
docker container logs – view container output.
docker container exec – run a command inside a running container.
docker container cp – copy files between container and host.
WordPress Deployment Examples
The article demonstrates three ways to run WordPress with Docker.
Method A: Build Your Own WordPress Container
Use the official PHP image, mount a host directory, and install WordPress files. Also create a MySQL container and link them.
# PHP container
docker container run --rm --name wordpress \
--volume "$PWD/":/var/www/html php:5.6-apache
# MySQL container
docker container run -d --rm --name wordpressdb \
-e MYSQL_ROOT_PASSWORD=123456 \
-e MYSQL_DATABASE=wordpress mysql:5.7Adjust permissions, access via container IP, add a simple index.php, download WordPress, configure database connection, and complete the installation.
Method B: Official WordPress Image
Run a MySQL container and a WordPress container linked together. Map ports and volumes for persistent data.
# MySQL
docker container run -d --rm --name wordpressdb \
-e MYSQL_ROOT_PASSWORD=123456 \
-e MYSQL_DATABASE=wordpress mysql:5.7
# WordPress
docker container run -d --rm --name wordpress \
-e WORDPRESS_DB_PASSWORD=123456 \
--link wordpressdb:mysql \
-p 127.0.0.2:8080:80 \
-v "$PWD/wordpress":/var/www/html \
wordpressMethod C: Docker Compose
Create a docker-compose.yml defining mysql and web services, then start with docker-compose up and stop with docker-compose stop. Example compose file:
mysql:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=wordpress
web:
image: wordpress
links:
- mysql
environment:
- WORDPRESS_DB_PASSWORD=123456
ports:
- "127.0.0.3:8080:80"
volumes:
- wordpress:/var/www/htmlConclusion
Docker simplifies environment management, enables lightweight virtualization, and supports microservice architectures. The article provides practical steps to install Docker, manage images and containers, write Dockerfiles, and deploy a real application (WordPress) using three different approaches.
Author: 阮一峰
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.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
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.
