Skip to content

Instantly share code, notes, and snippets.

@conkonig
Last active May 13, 2020 14:45
Show Gist options
  • Save conkonig/6328c3cc6aa08a5ecda6021eec0a418a to your computer and use it in GitHub Desktop.
Save conkonig/6328c3cc6aa08a5ecda6021eec0a418a to your computer and use it in GitHub Desktop.
How to set up wordpress to work in docker

Creating a docker container for your wordpress site for local dev

####PROJECT DIRECTORY

(dir) wp-content
      - all your wordpress theme files and plugins
(dir) db-content
      - your sql database file to import
docker-compose.yml (code example below)

docker-compose.yml example

version: '3'
services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
       - ./db-content/:/var/db-content
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: password
       MYSQL_DATABASE: wordpress
       MYSQL_USER: user
       MYSQL_PASSWORD: password
   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8090:80"
     restart: always
     volumes:
       - ./wp-content/:/var/www/html/wp-content 
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: user
       WORDPRESS_DB_PASSWORD: password
volumes:
    db_data:

start docker container

docker-compose up

see all the containers to get container name

docker ps

terminal into mysql to import the db file

docker exec -it containername_db_1 /bin/bash

mysql -u user -ppassword

show databases; show tables;

use wordpress;

exit

mysql -u user -p wordpress < database_file.sql

(password is in your dockercompose file)

You may get errors for timeout if you go over 30mb import file or so - then refer here:

docker exec -it container_name bash -c "echo 'max_allowed_packet = 512M' >> /etc/mysql/mysql.conf.d/mysqld.cnf" docker restart container_name

update the siteurl and homeurl to match your new local

UPDATE wp_options SET option_value = replace(option_value, 'https://old_domain_name.com', 'http://localhost:8090/') WHERE option_name = 'home' OR option_name = 'siteurl';

(make sure port number matches that in your dock compose .yml file).

Dont forget to refresh your wordpress permalinks afterwards

Setting up load images from production on local dev server

terminal into the docker wordpress server to change the htaccess file

docker exec -it containername_wordpress_1 /bin/bash

Add the following to your .htaccess file in /var/www/html/

nano .htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{HTTP_HOST} ^localhost:8090$
RewriteRule ^(.*\.(js|css|png|jpe?g|gif|ico)) https://example.com/$1 [L,R=302,NC]
</IfModule>

#Start wordpress
....
#End Wordpress

of if you are using nginx

sudo nano /etc/nginx/sites-available/example.com

....

location ~* \.(js|css|png|jpe?g|gif|ico)$ {
	expires 24h;
	log_not_found off;
	try_files $uri $uri/ @production;
}

location @production {
	resolver 8.8.8.8;
	proxy_pass https://whynow.co.uk/$uri;
}

enjoy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment