How to Host Wordpress Using Docker

Let’s go for our docker-compose.yml first.

 1version: '3'
 2
 3services:
 4  wp:
 5    image: wordpress:latest
 6    ports:
 7      - ${IP}:80:80
 8    volumes:
 9      - ./php.conf.ini:/usr/local/etc/php/conf.d/conf.ini
10      - ./wordpress:/var/www/html
11    environment:
12      WORDPRESS_DB_HOST: db
13      WORDPRESS_DB_NAME: "${DB_NAME}"
14      WORDPRESS_DB_USER: root
15      WORDPRESS_DB_PASSWORD: "${DB_ROOT_PASSWORD}"
16    depends_on:
17      - db
18
19  pma:
20    image: phpmyadmin/phpmyadmin
21    environment:
22      PMA_HOST: db
23      PMA_PORT: 3306
24      MYSQL_ROOT_PASSWORD: "${DB_ROOT_PASSWORD}"
25    ports:
26      - ${IP}:8080:80
27    links:
28      - db:db
29
30  db:
31    image: mysql:latest
32    ports:
33      - ${IP}:3306:3306
34    command: [
35        '--default_authentication_plugin=mysql_native_password',
36        '--character-set-server=utf8mb4',
37        '--collation-server=utf8mb4_unicode_ci'
38    ]
39    volumes:
40      - ./wp-data:/docker-entrypoint-initdb.d
41      - db_data:/var/lib/mysql
42    environment:
43      MYSQL_DATABASE: "${DB_NAME}"
44      MYSQL_ROOT_PASSWORD: "${DB_ROOT_PASSWORD}"
45
46volumes:
47  db_data:

Here comes a relavant php.conf.ini file:

1file_uploads = On
2memory_limit = 500M
3upload_max_filesize = 30M
4post_max_size = 30M
5max_execution_time = 600

Environmental variables will be needed, right? Here comes the .env file:

1IP=127.0.0.1
2DB_ROOT_PASSWORD=password
3DB_NAME=wordpress

You will have to export a SQL dump sometimes. (I don’t do that though 😉)
Here comes the export_dump.sh file:

1#!/bin/bash
2_now=$(date +"%m_%d_%Y")
3_file="wp-data/data_$_now.sql"
4
5# Export dump
6EXPORT_COMMAND='exec mysqldump "$MYSQL_DATABASE" -uroot -p"$MYSQL_ROOT_PASSWORD"'
7docker-compose exec db sh -c "$EXPORT_COMMAND" > $_file
8sed -i 1,1d $_file

All thing left is to run the container.

1docker-compose up -d

Your container is running? Done awesome!
Happy dockerizing, Gents! 🙂

comments powered by Disqus