This article is published under a SIGNATURE 4.0 International (CC BY 4.0) license. Signature 4.0 International (CC BY 4.0)

Author: Su Yang

Creation time: April 7, 2019 statistical word count: 6261 words reading time: 13 minutes to read this article links: soulteary.com/2019/04/07/…


WordPress with Docker and Traefik

The previous article covered how to quickly build WordPress using official images, but the official default is a “fat container” app. Now I’ll talk about another option for container-based building: Nginx. Demonstrates how to transform an application into a “thin” container application.

This article will take about 10 minutes to explain how to use WordPress and Nginx with Traefik in Docker containers.

Why Nginx

NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. NGINX is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.

In addition to the official positioning of Nginx as free, open source, lightweight, and high performance, Nginx currently has more resources than Apache (the default WordPress container image tool), both in the enterprise and for individuals to learn and use.

In addition, two well-known derivative applications of Nginx are also widely available in the enterprise: Tengine and OpenResty, and this article applies to both versions of “Nginx” as well.

Container image list

This article will use the following official images as a demonstration, as mentioned above, and you can replace them with Nginx “kindred” ones.

  • Nginx: 1.15.10 - alpine
    • As a service front end to replace Apache
  • WordPress: 5.1.1 - php7.1 - FPM - alpine
    • Use a container that contains only WordPress code and the PHP runtime
  • mariadb: 10.3.14
    • Our database, if there is a cloud database, may not need to be configured

The use of Traefik

For details on Traefik, you can refer to previous articles, such as: Use service discovery to improve development experience, a more perfect Docker + Traefik usage scheme, etc., for more content, you can look at the historical content of the label, here is not more details.

It is enough for this article to focus on the Labels and Networks field configuration in the choreographer file.

For networks fields of different container services, the declaration contains the same content, so that different applications live on the same network.

networks:
  - traefik
Copy the code

The above declaration, for example, puts container services in a network environment called Traefik.

Rewrite the container choreography configuration

The following configuration was mentioned in the previous article, but I’ve simplified it to avoid getting too long.

version: '3'

services:

  wp:
    image: ${WP_IMAGE}
    restart: always
    networks:
      - traefik
    environment:
        WORDPRESS_DB_HOST: ${DB_HOST}
        WORDPRESS_TABLE_PREFIX: ${WP_DB_PREFIX}
        WORDPRESS_DB_NAME: ${DB_NAME}
        WORDPRESS_DB_USER: ${DB_USER}
        WORDPRESS_DB_PASSWORD: ${DB_PASS}
    volumes:
    # If you need to customize the upload file size
    # - ./config/php.conf.uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
      - ./wp-app:/var/www/html
    labels:
      - "traefik.enable=true"
      - "traefik.frontend.rule=Host:${WP_DOMAINS}"
      - "traefik.frontend.entryPoints=https,http"

  mariadb:
    image: ${DB_IMAGE}
    restart: always
    container_name: ${DB_HOST}
    networks:
      - traefik
    environment:
      MYSQL_DATABASE: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASS}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASS}
    volumes:
      - ./data:/var/lib/mysql

networks:
  traefik:
    external: true
Copy the code

If we are using Nginx as a “Web front end,” then some adjustments are needed here.

Rewrite the WordPress container choreography configuration

Since Nginx has replaced WordPress as the traffic entry, WordPress services can no longer bind Traefik, register the requested domain name, and remove all labels fields.

In addition, since WordPress needs to be called remotely by Nginx, we need to add a fixed container_name to the WordPress service to make it easier to call. As in the above configuration, WordPress calls Mariadb.

 services:
  wp:
    image: ${WP_IMAGE}
    restart: always
    container_name: ${WP_HOST}
    networks:
      - traefik
    environment:
        WORDPRESS_DB_HOST: ${DB_HOST}
        WORDPRESS_TABLE_PREFIX: ${WP_DB_PREFIX}
        WORDPRESS_DB_NAME: ${DB_NAME}
        WORDPRESS_DB_USER: ${DB_USER}
        WORDPRESS_DB_PASSWORD: ${DB_PASS}
    volumes:
      - ./wordpress:/var/www/html
Copy the code

There is no change to the database. Let’s skip it and start configuring Nginx.

Write Nginx container choreography configuration

services:
  nginx:
    image: ${NGX_IMAGE}
    restart: always
    networks:
      - traefik
    expose:
      - 80
    volumes:
      - ./conf:/etc/nginx/conf.d
      - ./logs:/var/log/nginx
      - ./wordpress:/var/www/html
    depends_on:
      - wp
    labels:
      - "traefik.enable=true"
      - "traefik.frontend.rule=Host:${NGX_DOMAINS}"
      - "traefik.frontend.entryPoints=https,http"
Copy the code

Because Nginx takes over the incoming traffic, it is the task of registering service discovery on Traefik, using the Labels field and adding some Traefik supported directives to register the service.

In addition, you need to mount the files that WordPress has mounted to the file system into Nginx, like this.

- ./wordpress:/var/www/html
Copy the code

Finally, you simply declare an Nginx configuration that describes how to call WordPress.

Create a simple Nginx service configuration

Save the following as wp.conf.

server {
    listen 80;

    root /var/www/html;
    index index.php;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        try_files $uri= 404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass wp:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME$document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info; }}Copy the code

Writing environment files

As before, we separated the environment configuration information from the application choreography file for maintainability.

WP_IMAGE = wordpress: 5.1.1 - php7.1 - FPM - alpine WP_DB_PREFIX = wp WP_HOST = wp NGX_IMAGE = nginx: 1.15.10 - alpine Ngx_domain =wp.lab.com,wp.lab. IO DB_IMAGE= Mariadb :10.3.14 DB_HOST=wp-db DB_NAME=wordpress DB_USER=wordpress DB_PASS=wordpress DB_ROOT_PASS=soultearyCopy the code

With the above file saved as.env, we can start the application.

Launch the full application

Before starting the application, we summarize the choreography files we just modified.

version: '3'

services:

  wp:
    image: ${WP_IMAGE}
    restart: always
    container_name: ${WP_HOST}
    networks:
      - traefik
    environment:
        WORDPRESS_DB_HOST: ${DB_HOST}
        WORDPRESS_TABLE_PREFIX: ${WP_DB_PREFIX}
        WORDPRESS_DB_NAME: ${DB_NAME}
        WORDPRESS_DB_USER: ${DB_USER}
        WORDPRESS_DB_PASSWORD: ${DB_PASS}
    volumes:
      - ./wordpress:/var/www/html

  mariadb:
    image: ${DB_IMAGE}
    restart: always
    container_name: ${DB_HOST}
    networks:
      - traefik
    environment:
      MYSQL_DATABASE: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASS}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASS}
    volumes:
      - ./data:/var/lib/mysql

  nginx:
    image: ${NGX_IMAGE}
    restart: always
    networks:
      - traefik
    expose:
      - 80
    volumes:
      - ./conf:/etc/nginx/conf.d
      - ./logs:/var/log/nginx
      - ./wordpress:/var/www/html
    depends_on:
      - wp
    labels:
      - "traefik.enable=true"
      - "traefik.frontend.rule=Host:${NGX_DOMAINS}"
      - "traefik.frontend.entryPoints=https,http"

networks:
  traefik:
    external: true
Copy the code

After saving the file as docker-compose. Yml, we started the application with docker-compose up to verify that the application was normal.

When you see the log below, your application is ready to access it.

wp-db      | 2019-04-07  3:42:58 0 [Note] Server socket created on IP: '... '.
wp-db      | 2019-04-07  3:42:58 0 [Warning] 'proxies_priv' entry '@% root@2ed5be12f731' ignored in --skip-name-resolve mode.
wp-db      | 2019-04-07  3:42:58 0 [Note] Reading of all Master_info entries succeded
wp-db      | 2019-04-07  3:42:58 0 [Note] Added new Master_info ' ' to hash table
wp-db      | 2019-04-07  3:42:58 0 [Note] mysqld: ready for connections.
wp-db      | Version: '10.3.14 - MariaDB - 1:10. 3.14 + maria to bionic'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution
wp         | [07-Apr-2019 03:42:59] NOTICE: fpm is running, pid 1
wp         | [07-Apr-2019 03:42:59] NOTICE: ready to handle connections
Copy the code

It’s the same old way of doing things, filling in the information, doing the famous “three minute” setup, and a new site is born.

A few extra tricks

For example, when using Compose for the first time, we recommend that you do the following:

docker-compose up
Copy the code

You can view the actual application run logs on the terminal. If an error occurs, you can press CTRL+C to interrupt the execution and return to debugging.

When your application is ready and you need to run the service for a long period of time and then use Compose, you can add a -d parameter and make the application run in daemon mode.

docker-compose up -d
Copy the code

At this time, the application will quietly execute in the background, will not output any valuable information to the terminal, if the application is abnormal, we need to debug, want to see the application log how to deal with? Just execute the following command.

docker-compose logs -f
Copy the code

For example, docker-compose Down can be used to compose the daemon. For example, docker-compose Down can be used to compose the daemon. For example, docker-compose Down can be used to compose the daemon.

The last

Thank you for your continuous attention and encouragement to my writing. It is your attention that enables me to write effectively without repeating a lot of content.

Common WordPress is usually deployed online and relies on MySQL and Mariadb to run. It is not suitable for “portable” or low-configuration machines.

The next post will cover how to build WordPress to take with you.


I now have a small toss group, which gathered some like to toss small partners.

In the case of no advertisement, we will talk about software, HomeLab and some programming problems together, and also share some technical salon information in the group from time to time.

Like to toss small partners welcome to scan code to add friends. (Please specify source and purpose, otherwise it will not be approved)

All this stuff about getting into groups