DEV Community

Salman Sohail
Salman Sohail

Posted on • Originally published at dev.to on

Dockerize WordPress with themes, plugins and common configuration

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 
Enter fullscreen mode Exit fullscreen mode

Creating development directory and docker-compose file

 mkdir development && touch development/docker-compose.yml 
Enter fullscreen mode Exit fullscreen mode

Creating themes and plugins directories

 mkdir themes && plugins 
Enter fullscreen mode Exit fullscreen mode

Creating config directory and uploads.ini file

 mkdir config && touch config/uploads.ini 
Enter fullscreen mode Exit fullscreen mode

Creating entrypoint-child.sh file

 touch entrypoint-child.sh 
Enter fullscreen mode Exit fullscreen mode

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"] 
Enter fullscreen mode Exit fullscreen mode

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 "$@" 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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: 
Enter fullscreen mode Exit fullscreen mode

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: ../ 
Enter fullscreen mode Exit fullscreen mode

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" 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Open your web browser and type http://localhost:8000 and you should be able to see this page.

Next

Prepare AWS Security Groups for WordPress project

Top comments (2)

Collapse
 
prateevmishra profile image
prateevmishra

Hi Salman,

I have followed all the above blog things to setup Wordpress in Docker. While doing Docker Up , I am getting the below error.

Kindly help me in this asap.It will help me a lot in my Project.

Thanks in Advance.

Error is:

development_wordpress_1 exited with code 2
development_wordpress_1 exited with code 2
development_wordpress_1 exited with code 2
development_wordpress_1 exited with code 2
wordpress_1 | chown: cannot access '/var/www/html/wp-content': No such file or directory
wordpress_1 | /bin/sh: 1: Syntax error: Unterminated quoted string
wordpress_1 | Adding permissions
wordpress_1 | Permissions added
development_wordpress_1 exited with code 2
development_wordpress_1 exited with code 2
development_wordpress_1 exited with code 2
development_wordpress_1 exited with code 2

Collapse
 
brunomarks7 profile image
Bruno Saraiva • Edited

Change your entrypoint, like this:

dev-to-uploads.s3.amazonaws.com/i/...