Cloud Native 4 min read

How to Install and Use Docker Compose for WordPress and Nginx with Persistent Storage

This guide explains how to install Docker Compose, outlines its YAML file format, provides a complete WordPress compose file example, and demonstrates commands to start, stop, and remove containers, as well as how to configure an Nginx service with volume persistence using Docker Compose.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Install and Use Docker Compose for WordPress and Nginx with Persistent Storage

Installation of Docker Compose

If you have already successfully set up Harbor, you can skip this step.

Docker Compose File Format

Docker Compose uses YAML format, which relies on indentation to define hierarchy. Do not use Tab characters; YAML is case‑sensitive.

Example of a YAML file:

services:
  blog:
    image: wordpress
    links:
      - db:db
    ports:
      - "80:80"
    environment:
      - WORDPRESS_DB_HOST=db
      - WORDPRESS_DB_USER=root
      - WORDPRESS_DB_PASSWORD=123
      - WORDPRESS_DB_NAME=wordpress
  db:
    image: mysql
    environment:
      - MYSQL_ROOT_PASSWORD=123
      - MYSQL_DATABASE=wordpress

In the example, the ports value "80:80" is quoted because Docker expects a string; unquoted numbers cause errors. Some environment entries also need quotes when they contain numeric values.

Using Docker Compose

Save the above YAML as wordpress.yml. To start the containers:

# docker compose -f wordpress.yml up -d
[+] Running 3/3
✔ Network compose_default   Created   0.1s
✔ Container compose-db-1    Started   0.0s
✔ Container compose-blog-1  Started   0.0s

To stop and remove the containers, use the down command:

# docker compose -f wordpress.yml down
[+] Running 3/3
✔ Container compose-blog-1  Removed   1.2s
✔ Container compose-db-1    Removed   1.1s
✔ Network compose_default   Removed

Other useful commands include start, stop, and restart.

Docker Compose to Orchestrate Nginx with Persistent Storage

Example YAML for an Nginx service with a volume mapping:

services:
  web01:
    image: nginx
    ports:
      - "80:80"
    volumes:
      - "/html:/usr/share/nginx/html"

The volumes parameter mounts the host directory /html into the container.

# docker compose -f nginx.yml up -d
[+] Running 2/2
✔ Network compose_default    Created   0.1s
✔ Container compose-web01-1  Started
# curl localhost
hello

Link: https://www.cnblogs.com/fsdstudy/p/17949606

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.

YAMLcontainer orchestrationWordPressDocker Compose
MaGe Linux Operations
Written by

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.

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.