Stack consists of WordPress (PHP-FPM) / Nginx / Mysql / WPCLI.

Again, I will eventually get around to a longer opening, but till then here we go.

Expectations are you have a Docker Host already, can install and run “Hello World” via docker-compose. If you can’t do that, you won’t be able to do this. Sorry I don’t have a guide to suggest yet, but, the creators of docker-compose have done a good job at making it approachable. Generally get Python 3, and docker installed, then follow their guide for docker-compose.

When I do one of these, either I run it on my Docker Host from the Git repo I track my code in, or, well, that is about how I do it now. That said, I have a folder for each “project” or Stack/Container purpose. I navigate to the folder that has the docker-compose.yml and various other bits, and I run my “run” script that is written to execute (generally ‘docker-compose up -d …’) the run environment details (build/execute/update) for that project.

WordPress in Docker requires a DB, for this I choose MySQL because the Maria DB docker container doesn’t natively support the (older) method of authentication that Docker uses as it fabricates your container. That said, once they weave that into the container rollout, we will move over to MariaDB.

cat deploy_container.sh
#!/bin/bash

docker-compose up -d --force-recreate --always-recreate-deps --build --remove-orphans

There’s the run script, in this I tell ‘docker-compose’ to do a number of things, but I don’t tell it to make my “macvlan” driver type “core01” network. This network uses a /26 of my whole network, and allows me to carve out IPs to specific services that aren’t being channeled through the Docker hosts’ network IP.

To create a “core01” network (you don’t have to do this, please read all the rest, get to the end where I talk about how to do ‘frontend’ instead of this ‘core01’ network.

cat build_core01.sh
#!/bin/bash
# dockerhostname
docker network create core01 \
    --driver=macvlan \
    --subnet=192.168.34.64/26 \
    --gateway=192.168.34.65 \
    --ip-range=192.168.34.80/28 \
    --attachable \
    -o parent=eth0 \
    -o com.docker.network.mtu=9000

Now that we have a way to attach a unique element in this docker-compose.yml file, let’s get into that.

cat docker-compose.yml
version: "3"

services:

  wordpress:
    container_name: wordpress
    depends_on:
      - mysql
    image: wordpress:fpm
    restart: unless-stopped
    ports:
      - "9000:9000"
    expose:
      - 9000
    networks:
      backend:
    environment:
      - WORDPRESS_DB_HOST=mysqlhostname
      - WORDPRESS_DB_NAME=wordpress_databasename
      - WORDPRESS_DB_USER=database_username
      - WORDPRESS_DB_PASSWORD=database_password
      - WORDPRESS_TABLE_PREFIX=wp_
    links:
      - mysql
    volumes:
      - wordpress:/var/www/html:rw
      - mysqldrun:/var/run/mysqld

  mysql:
    container_name: mysql
    image: mysql:latest
    restart: unless-stopped
    ports:
      - "3306:3306"
      - "33060:33060"
    expose:
      - 3306
    networks:
      backend:
    environment:
      - MYSQL_DATABASE=wordpress_databasename
      - MYSQL_USER=database_username
      - MYSQL_PASSWORD=database_password
      - MYSQL_RANDOM_ROOT_PASSWORD=1
    command: ['mysqld', '--default-authentication-plugin=mysql_native_password']
    volumes:
      - mysql:/var/lib/mysql
      - mysqldrun:/var/run/mysqld

  nginx:
    container_name: nginx
    image: nginx:latest
    restart: unless-stopped
    links:
      - wordpress
    volumes:
      - ./nginx:/etc/nginx/conf.d
      - wordpress:/var/www/html
    ports:
      - "80:80"
    expose:
      - 80
    networks:
      dockercore01:
        ipv4_address: 192.168.34.82
        aliases:
         - wordpress.innerdomain.home
      backend:

  wpcli:
    container_name: wpcli
    depends_on:
      - mysql
      - wordpress
    image: wordpress:cli
    links:
      - mysql
      - wordpress
    networks:
      backend:
    environment:
      - WORDPRESS_DB_HOST=mysqlhostname
      - WORDPRESS_DB_NAME=wordpress_databasename
      - WORDPRESS_DB_USER=database_username
      - WORDPRESS_DB_PASSWORD=database_password
      - WORDPRESS_TABLE_PREFIX=wp_
    volumes:
      - wordpress:/var/www/html:rw
      - mysqldrun:/var/run/mysqld

networks:
  core01:
    external: true
  backend:

volumes:
  wordpress:
  mysqldrun:
  mysql:

Now you will need to create an “nginx” folder for the one map/bind that happens for a local target. All the rest of the storage areas use native Docker spaces that are a part of the modern Docker environment. Generally these are in “/var/lib/docker/volumes/…” and you can see more if you inspect the volume. In the “nginx” folder make this file or one customized to your needs.

cat nginx/default.conf
server {
	listen 80;
	listen [::]:80;
	server_name wordpress.innerdomain.home;
	index index.php index.html index.htm;
	root /var/www/html;

	location ~ /.well-known/acme-challenge {
		allow all;
		root /var/www/html;
	}
	location / {
		try_files $uri $uri/ /index.php$is_args$args;
	}
	location ~ \.php$ {
		try_files $uri =404;
		fastcgi_split_path_info ^(.+\.php)(/.+)$;
		fastcgi_pass wordpress:9000;
		fastcgi_index index.php;
		include fastcgi_params;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		fastcgi_param PATH_INFO $fastcgi_path_info;
	}
	location ~ /\.ht {
		deny all;
	}

	location = /favicon.ico { 
		log_not_found off;
    access_log off; 
	}
	location = /robots.txt { 
		log_not_found off;
    access_log off;
    allow all; 
	}
	location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
		expires max;
		log_not_found off;
	}
}

If you added a “frontend:” network to the bottom of the “docker-compose.yml” file and ran all of this on the same Desktop/Laptop host you were using, then, everything should resolve for you naturally.

You wouldn’t need to manually create a “macvlan” network, and you could remove the whole “core01” network part from “docker-compose.yml” (nginx service and networks at the bottom), adding a new listing just like “backend:” in the “nginx” service, named “frontend”, and at the bottom under “networks:”, again, listed just like “backend:”.

That’s it, now, “chmod +x deploy_container.sh” and then “./deploy_container.sh” and watch your environment deploy! Enjoy!

Things I haven’t done yet:

  • Tried to use the WPCLI
  • Get Lets Encrypt working for the Stack
  • Deploy desired plug-ins “slipstream” style, so the environment doesn’t have to post mod it in

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.