Horje
How to get wordpress multisite working with subdomains in docker-compose.yml - Wordpress Solution
I use docker-compose.yml to spin up my local wordpress projects on localhost. (on mac) It has been a while since I've had to create multisite project since the days of creating wordpress sites directly on the server. Ideally I would like to use the official docker hub wordpress image... https://hub.docker.com/_/wordpress Here is what I did first, created this docker-compose.yml...

version: '3.7'

networks:
  wordpress:
    ipam:
      config:
        - subnet: 172.25.0.0/16

services:

  # here is out mysql database
  db:
    image: mysql:5.7
    volumes:
      - ./db:/var/lib/mysql:delegated
    ports:
      - "3306:3306"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    networks:
      - wordpress

  # here is out wordpress server
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    volumes:
      # our persistent local data re routing
      - ./.htaccess:/var/www/html/.htaccess
      - ./testing:/var/www/html/wp-content/themes/testing:delegated
      - ./plugins:/var/www/html/wp-content/plugins
      - ./uploads:/var/www/html/wp-content/uploads
      - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
    ports:
      - "80:80"
    restart: always
    networks:
      - wordpress
    environment:

      # our local dev environment
      WORDPRESS_DEBUG: 1
      DEVELOPMENT: 1

      # docker wp config settings
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_AUTH_KEY: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_SECURE_KEY: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_LOGGED_IN_KEY: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_NONCE_KEY: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_SECURE_AUTH_SALT: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_LOGGED_IN_SALT: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_NONCE_SALT: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb

      # our local dev environment
      WORDPRESS_CONFIG_EXTRA: |

        /* development parameters */
        define('WP_CACHE', false);
        define('ENVIRONMENT', 'local');
        define('WP_DEBUG', true);

        /* configure mail server */
        define('WORDPRESS_SMTP_AUTH', false);
        define('WORDPRESS_SMTP_SECURE', '');
        define('WORDPRESS_SMTP_HOST', 'mailhog');
        define('WORDPRESS_SMTP_PORT', '1025');
        define('WORDPRESS_SMTP_USERNAME', null);
        define('WORDPRESS_SMTP_PASSWORD', null);
        define('WORDPRESS_SMTP_FROM', '<a rel="nofollow" href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6b1aea9a3b0a3b486a3bea7abb6aaa3e8a5a9ab">[email protected]</a>');
        define('WORDPRESS_SMTP_FROM_NAME', 'Whoever');

  # here is our mail hog server
  mailhog:
    image: mailhog/mailhog:latest
    ports:
      - "8025:8025"
    networks:
      - wordpress
I then run this command to install...

docker-compose up -d
I then covert the generated uploads.ini folder to a true uploads.ini file containing the below code...

file_uploads = On
memory_limit = 2000M
upload_max_filesize = 2000M
post_max_size = 2000M
max_execution_time = 600
I then covert the generated .htaccess folder to a true .htaccess file and leave it empty as wordpress will automatically generate write to this. I then go to http://localhost to install wordpress normally. I then stop the environment using...

docker-compose down
docker-compose rm
I then modify the docker-compose.yml with these multisite options...

version: '3.7'

networks:
  wordpress:
    ipam:
      config:
        - subnet: 172.25.0.0/16

services:

  # here is out mysql database
  db:
    image: mysql:5.7
    volumes:
      - ./db:/var/lib/mysql:delegated
    ports:
      - "3306:3306"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    networks:
      - wordpress

  # here is out wordpress server
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    volumes:
      # our persistent local data re routing
      - ./.htaccess:/var/www/html/.htaccess
      - ./testing:/var/www/html/wp-content/themes/testing:delegated
      - ./plugins:/var/www/html/wp-content/plugins
      - ./uploads:/var/www/html/wp-content/uploads
      - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
    ports:
      - "80:80"
    restart: always
    networks:
      - wordpress
    environment:

      # our local dev environment
      WORDPRESS_DEBUG: 1
      DEVELOPMENT: 1

      # docker wp config settings
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_AUTH_KEY: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_SECURE_KEY: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_LOGGED_IN_KEY: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_NONCE_KEY: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_SECURE_AUTH_SALT: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_LOGGED_IN_SALT: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_NONCE_SALT: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb

      # our local dev environment
      WORDPRESS_CONFIG_EXTRA: |

        /* allow multisite */
        define('WP_ALLOW_MULTISITE', true );
        define('MULTISITE', true);
        define('SUBDOMAIN_INSTALL', true);
        define('DOMAIN_CURRENT_SITE', 'localhost');
        define('PATH_CURRENT_SITE', '/');
        define('SITE_ID_CURRENT_SITE', 1);
        define('BLOG_ID_CURRENT_SITE', 1);

        /* development parameters */
        define('WP_CACHE', false);
        define('ENVIRONMENT', 'local');
        define('WP_DEBUG', true);

        /* configure mail server */
        define('WORDPRESS_SMTP_AUTH', false);
        define('WORDPRESS_SMTP_SECURE', '');
        define('WORDPRESS_SMTP_HOST', 'mailhog');
        define('WORDPRESS_SMTP_PORT', '1025');
        define('WORDPRESS_SMTP_USERNAME', null);
        define('WORDPRESS_SMTP_PASSWORD', null);
        define('WORDPRESS_SMTP_FROM', '<a rel="nofollow" href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f4857505a495a4d7f5a475e524f535a115c5052">[email protected]</a>');
        define('WORDPRESS_SMTP_FROM_NAME', 'Whoever');

        if (!defined('WP_HOME')) {
          /* force our home url */
          define('WP_HOME', 'http://www.domain.com');
          define('WP_SITEURL', WP_HOME);
        }

  # here is our mail hog server
  mailhog:
    image: mailhog/mailhog:latest
    ports:
      - "8025:8025"
    networks:
      - wordpress
When I run this docker-compose.yml I get error establishing connection? Can anyone please help me to get this working. I would like to be able to modify my hosts using sudo nano /etc/hosts to control the subdomains...

127.0.0.1 www.domain.com
127.0.0.1 admin.domain.com
127.0.0.1 stock.domain.com
Many Thanks

Solution - 1

If you are facing a database connection error then replace

WORDPRESS_CONFIG_EXTRA: |

/* allow multisite */
define('WP_ALLOW_MULTISITE', true );
define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', true);
define('DOMAIN_CURRENT_SITE', 'localhost');
define('PATH_CURRENT_SITE', '/');
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);
with
WORDPRESS_CONFIG_EXTRA: |
        /* Multisite */
        define('WP_ALLOW_MULTISITE', true );
and then later set up multisite after installing from Tools >> Network setup.





Wordpress

Related
Update post meta in another post after draft -> publish transition - Wordpress Solution Update post meta in another post after draft -> publish transition - Wordpress Solution
Display custom post group by taxonomy current term and sub-term - Wordpress Solution Display custom post group by taxonomy current term and sub-term - Wordpress Solution
custom menu html structure - Wordpress Solution custom menu html structure - Wordpress Solution
Trigger event script after product added to cart via Ajax - Woocommerce - Wordpress Solution Trigger event script after product added to cart via Ajax - Woocommerce - Wordpress Solution
Order posts by ACF checkbox - Wordpress Solution Order posts by ACF checkbox - Wordpress Solution

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
35