Develop Locally A Custom WordPress Using A Bitnami Docker Image
Tags: docker, kubernetes, opensource, devops
Published: 2020-01-29 15:22:12

*Originally published at [docs.bitnami.com](https://docs.bitnami.com/tutorials/deploy-locally-custom-wordpress/).*

# Why WordPress and Why Containers for Local Development?

When you are searching for an open-source CMS to create your blog or website, chances are that you finally chose [WordPress](https://wordpress.com/). 

WordPress is easy to use and includes a lot of plugins and themes enabled by default so you can run your website in a matter of minutes. 

The best way to start developing locally is to use containers. They offer many of the same advantages as developing in VMs, but with lower overhead in terms of developer effort and development machine resources.

[Bitnami containers](https://github.com/bitnami/) are a great resource for local development: they allow you to save a lot of time in coding and ensure that you launch always the latest and secure application image. 

As a developer, you may probably want to add some customizations to the default configuration that Bitnami delivers in its containers. This tutorial shows how to add and activate both a custom plugin and a theme for the first run.

# Set Up Prerequisites

Before starting, make sure that you have already set up the following:

* A Docker environment [installed and configured](https://docs.docker.com/install/). 
* [A Docker Hub account](Docker Hub account). 

# 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.

1) Clone the [Bitnami Docker WordPress repository](https://github.com/bitnami/bitnami-docker-wordpress) and change to the directory that contains all files:

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

The next 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/)*.

2) In the *rootfs/* directory, create a file named *download-extra.sh*  with the following content: 

~~~bash
#!/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
~~~

# 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.

1) Open the Dockerfile (*5/debian-9/Dockerfile*) and add the *unzip* packages to the *RUN install_packages* command list:

~~~bash
# 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
~~~

2) Add the *RUN bash download-extra.sh* command under the *COPY rootfs/* line:

~~~bash
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.

1) Open the *app-entrypoint.sh* (*5/debian-9/rootfs/app-entrypoint.sh*) file and add the following:

~~~bash
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 latest version of the WordPress Docker image packaged by Bitnami. To point to your custom image, it is necessary to change that value in the *docker-compose.yml*. 

> In the next steps, remember to replace "DOCKER_USERNAME/my-custom-wordpress" with the username of your Docker account and the name of your image, respectively. 

~~~yaml
wordpress:
   image: 'DOCKER_USERNAME/my-custom-wordpress:latest'
~~~

# Step 5: Build the Docker Image

Let's build the Docker image.

1) Execute the *docker build* command within the directory that contains the Dockerfile (*5/debian-9/*):

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

2) Check that the image has been added to the local repository by executing:

~~~bash
docker images | grep my-custom-wordpress
~~~

# Step 6: Test the Docker Image and Access Locally To Your Custom Application

To test locally your custom image, it is necessary to run the container.

1) Execute the *docker-compose up* command:

~~~bash
docker-compose up
~~~

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

![Plugin Activated](https://dev-to-uploads.s3.amazonaws.com/i/ktqa45ua133n5kjv69d3.png)

2) Enter http://localhost in your web browser and check that the theme is already activated:


![Theme Activated](https://dev-to-uploads.s3.amazonaws.com/i/ow5dagi9ada6d75g515g.png)

3) 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 Enabled](https://dev-to-uploads.s3.amazonaws.com/i/7opbmfplqgh73uts3d2u.png)

# 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 guide uses [Docker Hub](https://hub.docker.com/), but you can select one of your own choice such as:

* [Google Container Registry](https://cloud.google.com/container-registry/)
* [Amazon EC2 Container Registry](https://aws.amazon.com/ecr/)
* [Azure container Registry](https://azure.microsoft.com/en-us/services/container-registry/)
* [Quay](https://quay.io/)

1) Log in to Docker Hub

~~~bash
docker login
~~~

2) Push the image to your Docker Hub account: 

~~~bash
docker push DOCKER_USERNAME/my-custom-app:latest
~~~

Congratulations! You have customized a WordPress Docker image, tested and built it, and now it is available in Docker Hub and ready to be deployed in a production scenario! 

## Next Steps

[Deploy a Custom WordPress Image on a Kubernetes Cluster Using Bitnami Helm Charts](https://dev.to/raquelcampuzano/deploy-a-custom-wordpress-image-on-a-kubernetes-cluster-using-bitnami-helm-charts-2hmc)