Develop Locally A Custom WordPress Using Bitnami Containers

Introduction

Bitnami containers provide you with a ready-to-go environment for the development framework of your choice backed by Bitnami. By selecting a Bitnami container for local development, you can save a lot of time in coding as well as you benefit from having always the latest and more secure application image. As a developer, you may probably want to add some customizations to the default configuration that Bitnami delivers in its containers.

In this tutorial, you will learn how to use a Bitnami container to have a boilerplate from which you can implement all the customizations that your project demands. This guide uses the Bitnami WordPress Docker container as an example and will show you, step by step, how to add and activate both a custom plugin and a theme from the first run.

Assumptions and prerequisites

This guide makes the following assumptions:

The following are the steps you will complete in this guide:

  • Step 1: Create a script to add a plugin and a theme
  • Step 2: Edit the Dockerfile to add the custom script
  • Step 3: Activate the plugin and theme at initialization time
  • Step 4: Edit the docker-compose.yml file to point to your image
  • Step 5: Build the Docker image
  • Step 6: Test the Docker image and access locally to your custom application
  • Step 7: Publish the Docker image

Step 1: Create a script to add a plugin and a theme

To begin the process, you need to obtain the Bitnami Docker image for WordPress. To do so, clone the Bitnami Docker WordPress repository as shown below. Then, change to the directory that contains all files:

git clone https://github.com/bitnami/bitnami-docker-wordpress.git
cd bitnami-docker-wordpress

The first step is to create a script to specify which are the plugins/themes you want to install in your WordPress. This script will contain the instructions to download and uncompress the plugin and the theme files as well as the path where they should be installed (/opt/bitnami/wordpress/wp-content/):

  • Create a file in the rootfs/ directory named download-extra.sh with the following content:
#!/bin/bash
curl -o /tmp/bbpress2.5.14.zip https://downloads.wordpress.org/plugin/bbpress.2.5.14.zip
curl -o /tmp/gambit.1.5.3.zip https://downloads.wordpress.org/theme/gambit.1.5.3.zip
unzip /tmp/bbpress.2.5.14.zip -d /opt/bitnami/wordpress/wp-content/plugins
unzip /tmp/gambit.1.5.3.zip -d /opt/bitnami/wordpress/wp-content/themes
Tip

For the sake of simplicity, this guide shows how to install an official WordPress plugin and theme. Replace these examples with the path where the plugins you want to add are located.

Step 2: Edit the Dockerfile to add the custom script

To install the plugins and themes you have selected in the first run, it is necessary to indicate in the Dockerfile where are the instructions to perform that action. Follow the steps below:

  • Open the Dockerfile with your favorite editor:
vim 5/debian-9/Dockerfile
  • Install the unzip package by adding it in the RUN install_packages command list:
# Install required system packages and dependencies
RUN install_packages unzip libbz2-1.0 libc6 libcomerr2 libcurl3 libexpat1 libffi6 libfreetype6 libgcc1 libgcrypt20 libgmp10 libgnutls30 libgpg-error0 libgssapi-krb5-2 libhogweed4 libicu57 libidn11 libidn2-0 libjpeg62-turbo libk5crypto3 libkeyutils1 libkrb5-3 libkrb5support0 libldap-2.4-2 liblzma5 libmemcached11 libmemcachedutil2 libncurses5 libnettle6 libnghttp2-14 libp11-kit0 libpcre3 libpng16-16 libpq5 libpsl5 libreadline7 librtmp1 libsasl2-2 libsqlite3-0 libssh2-1 libssl1.0.2 libssl1.1 libstdc++6 libsybdb5 libtasn1-6 libtidy5 libtinfo5 libunistring0 libxml2 libxslt1.1 libzip4 zlib1g
  • Add the RUN bash download-extra.sh command under the COPY rootfs / line:
COPY rootfs /
RUN bash download-extra.sh

Step 3: Activate the plugin and theme at initialization time

To initialize the container with the installed plugin and theme already activated, it is necessary to add this action in the application entrypoint script. To do so:

  • Open the app-entrypoint.sh file with your favorite editor:
vim 5/debian-9/rootfs/app-entrypoint.sh
  • Add the following lines:
nami_initialize apache php mysql-client wordpress
su daemon -s /bin/bash -c '/opt/bitnami/wp-cli/bin/wp plugin activate bbpress'
su daemon -s /bin/bash -c '/opt/bitnami/wp-cli/bin/wp theme activate gambit'
info "Starting wordpress... "

Step 4: Edit the docker-compose.yml file to point to your image

By default, the docker-compose.yml file is pointing to the bitnami/wordpress:X.Y.Z image. Being bitnami the Docker username and "X.Y.Z" the latest version of the WordPress Docker image packaged by Bitnami. Replace these values with the following (remember to replace DOCKER_USERNAME placeholder with the username of your Docker account):

wordpress:
   image: 'DOCKER_USERNAME/my-custom-wordpress:latest'
Tip

Using latest as the tag of the image prevents you to the need of updating that tag any time you perform changes in the container.

Step 5: Build the Docker image

The next step is to build the Docker image using the docker build command. This should be executed within the directory that contains the Dockerfile. Replace the DOCKER_USERNAME placeholder with the username of your Docker Hub account:

cd 5/debian-9/
docker build -t DOCKER_USERNAME/my-custom-wordpress:latest .

When the build finishes you can check that the image has been added to the local repository by executing:

docker images | grep my-custom-wordpress

Step 6: Test the Docker image and access locally to your custom application

The last step is to test locally your custom image by running the container. After that, you will be able to access the application by browsing http://localhost. To start running the container, execute the docker-compose up command:

docker-compose up

If everything went well, you should see an output similar to this:

Container up
  • Enter http://localhost in your web browser and check that the theme is already activated:
Theme active
  • Log in to the WordPress admin panel using the default credentials: username user and password bitnami. Navigate to the "Plugins" section. You will see the BBplugin already installed and activated:
Plugin active

Congratulations! You have a WordPress Docker container running locally using a custom plugin and theme!

Step 7: Publish the Docker image

Now that your Docker image is built and contains your application code, you can upload it into a public registry. This tutorial uses Docker Hub, but you can select one of your own choice such as:

To upload the image to Docker Hub, follow the steps below:

  • Log in to Docker Hub:
docker login
  • Push the image to your Docker Hub account. Replace the DOCKER_USERNAME placeholder with the username of your Docker Hub account and my-custom-app:latest with the name and the version of your Docker image:
docker push DOCKER_USERNAME/my-custom-app:latest
  • Confirm that you see the image in your Docker Hub repositories dashboard.

This tutorial is part of the series

From development to production - customize and secure your WordPress deployment

Learn how to create a custom WordPress container image, deploy it on a Kubernetes production cluster, and secure your deployment with TLS and Let's Encrypt SSL certificates.