Dockerize WordPress with themes, plugins and common configuration
Salman Sohail
Posted on February 5, 2020
This article is the continuation of WordPress with Docker, AWS (ECS, Code Pipeline, Load Balancer, RDS, EFS) Complete Series. For better understating, please start from the beginning of the series.
In this article, we will create the following files and folders
Let’s create files and folders
Creating Dockfile
touch Dockerfile
Creating development directory and docker-compose file
mkdir development && touch development/docker-compose.yml
Creating themes and plugins directories
mkdir themes && plugins
Creating config directory and uploads.ini file
mkdir config && touch config/uploads.ini
Creating entrypoint-child.sh file
touch entrypoint-child.sh
Dockerfile
Paste the following content into the Dockerfile
FROM wordpress:latest
# Copying Themes and Plugins into the wordpress image
COPY ["themes","/usr/src/wordpress/wp-content/themes"]
COPY ["plugins","/usr/src/wordpress/wp-content/plugins"]
# Updating the configuration of wordpress image with our own
COPY ./config/uploads.ini /usr/local/etc/php/conf.d/uploads.ini
# Applying the execution right on the folders for apache
COPY entrypoint-child.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint-child.sh
ENTRYPOINT ["entrypoint-child.sh"]
CMD ["apache2-foreground"]
entrypoint-child.sh
#!/bin/bash
set -euo pipefail
echo "Adding permissions"
chown -R www-data:www-data /var/www/html/wp-content &
find /var/www/html/wp-content -type d -exec chmod 0755 {} \; &
find /var/www/html/wp-content -type f -exec chmod 644 {} \; &
echo "Permissions added"
exec docker-entrypoint.sh "$@"
Our WordPress Docker image ( wordpress:latest ) already contains an entry point. which is called docker-entrypoint.sh. The reason have added our custom entrypoint file into the mix is because of AWS EFS volumes for our Plugins storage on AWS. That is why we need some permissions to be added again. This entrypoint just add those permissions and then execute the image default entrypoint. Don’t worry about this for now, I will shed more light on it as we progress.
uploads.ini config
Paste the following content into config/uploads.ini file
file_uploads = On
memory_limit = 64M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 600
Please change the above as per your wordpress website requirements
docker-compose
Paste the following content into development/docker-compose.yml file
version: '3'
services:
wordpress:
build: ../
restart: always
ports:
- "8000:80"
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: root
WORDPRESS_DB_NAME: blog_db_name
WORDPRESS_CONFIG_EXTRA: |
/* If you want to add any extra wordpress config */
# define('WP_ALLOW_MULTISITE', true );
volumes:
- wordpress:/var/www/html
db:
image: mysql:5.7
restart: always
logging:
driver: none
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USERNAME: root
MYSQL_PASSWORD: root
MYSQL_DATABASE: blog_db_name
volumes:
- db:/var/lib/mysql
volumes:
wordpress:
db:
We have created two service in the docker-compose file
- wordpress
- db
WordPress service is building a Docker image from the Dockerfile we have created in the begining
build: ../
The above line is telling the docker-compose to build the image from a Dockerfile which is on the root of this project
ports:
- "8000:80"
The above command is just mapping the 8000 port from outside world to the port 80 of this container
We are also setting some environment variables such as database credentials which are initialized in the db service
Time to see something on the browser !!!
All the required files and directories have been created. Let’s start our wordpress website by running the following commands
cd development
docker-compose build
docker-compose up
Open your web browser and type http://localhost:8000 and you should be able to see this page.
Next
Posted on February 5, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 15, 2024