WordPress with MySQL

In this tutorial, you'll learn how to deploy WordPress and MySQL on Release.

Docker Compose and Release for WordPress development

Using Docker Compose during WordPress theme or plugin development ensures that your development and build environments can be recreated easily on your team's workstations and staging servers.

By adding your WordPress repository to Release, you can create environments per branch, create staging environments, test contributors' pull requests, and share demo links with clients.

WordPress and Docker Compose

For this tutorial, we'll start with the WordPress with MySQL example from Awesome Compose. To simplify getting started, we've created a repository with only the wordpress-mysql folder from the Awesome Compose repository: wordpress-mysql.

Requirements

Create a Release account and integrate your source control (GitHub, Bitbucket, or GitLab) account.

Besides a web browser, you won't need anything specific installed on your computer for this guide.

Fork the wordpress-mysql example repository

Fork the wordpress-mysql repository to a private or public repository in your version control hosting service.

An overview of the example application

The docker-compose.yaml file lists two services for this application: db and wordpress.

The service called db runs a specific mysql Docker image, while the wordpress service runs the latest wordpress Docker image.

Create a new application on Release

Release can import applications based on Docker Compose, so we won't need to change anything in the repository to add this application to Release.

Firstly, log into Release, and create an application by clicking on Create new app.

Name your application and select your repository

Enter a unique name for your application, and pick the forked repository you created earlier, then click Next step.

Pick your services

  1. Pick Analyze the repository, so Release can create services from docker-compose.yaml.

  2. Select the branch from your repository you'd like to track in this application.

  3. Select the docker-compose.yaml file.

  4. Click Start analysis.

Release will read your Docker Compose file, and list the services found.

Click Next step.

Generate a template

Release will generate a template based on the services from your docker-compose.yaml file.

Release will use this template to create new environments later on, and the defaults should work as expected for most Docker Compose applications.

Expose container ports for the database service

Release does not currently convert expose directives when importing services from Docker Compose. Add a container_port for each port in your Docker Compose file. Read more about ports in our template schema.

For this example application, we'll make one important change to the template to make sure the db service exposes two ports for MySQL.

Find the db service in the application template YAML, and add two container ports by making the following changes:

  services:
  - name: db
+   ports:
+   - type: container_port
+     port: '3306'
+   - type: container_port
+     port: '33060'
# ...

Release supports two different port types: node_port and container_port. We're adding a container port to the db service because this port should only be accessible on your application's private network. This enables the wordpress service to connect to the db service without exposing your MySQL database to the internet.

After editing the template, click Next step.

Environment variables

The docker-compose.yaml file from our repo lists the environment variables for each service.

To see how each service's container will use these environment variables, we can consult the Docker images documentation.

mysql Docker image variables

MYSQL_ROOT_PASSWORD

Specifies the root MySQL user's password. On a production server, this should be treated as secret.

MYSQL_DATABASE

If this variable is set, MySQL will create a database with this variable's value as a name. For our example, we'll set this to wordpress.

We can expect the MySQL server to create a database called wordpress on startup.

MYSQL_USER and MYSQL_PASSWORD

MySQL uses these two variables together to set a username and password for a new MySQL user on startup. This user will be granted superuser privileges (GRANT ALL) on the database specified by MYSQL_DATABASE.

For the example application, we'll set both to wordpress.

On startup, we can expect MySQL to create a new user called wordpress with the password wordpress, and grant the user all privileges on the wordpress database.

wordpress Docker image variables

WordPress uses a wp-config.php file to set database access strings and other settings. Usually, you would edit wp-config.php manually and set the database hostname, username, and password as PHP variables.

In Docker, we do this differently. The WordPress Docker image uses a custom wp-config.php file that enables you to configure WordPress through environment variables.

For more information about the benefits of using environment variables for configuration (not just in Docker), we recommend reading The Twelve-Factor App III: Config.

Let's take a look at the four environment variables in our example application.

WORDPRESS_DB_HOST

The WordPress Docker image uses this variable to set the database hostname. For our example, we'll set this to db.

Release creates a private network for your application, and by default, any service in your application can discover any other service in the application by querying a service's name.

This means the wordpress service can reach the db service's container on the private network by querying the db hostname.

WORDPRESS_DB_USER, WORDPRESS_DB_PASSWORD, and WORDPRESS_DB_NAME

The WordPress Docker image uses these three variables to know which database to use and to authenticate with the MySQL database server.

For this example, we'll set all three to wordpress so that they match the equivalent variables set for the db service.

Set your environment variables

Edit the YAML environment variables in Release to look like this:

---
defaults: []
services:
  db:
  - key: MYSQL_ROOT_PASSWORD
    value: somewordpress
    secret: true
  - key: MYSQL_DATABASE
    value: wordpress
  - key: MYSQL_USER
    value: wordpress
  - key: MYSQL_PASSWORD
    value: wordpress
    secret: true
  wordpress:
  - key: WORDPRESS_DB_HOST
    value: db
  - key: WORDPRESS_DB_USER
    value: wordpress
  - key: WORDPRESS_DB_PASSWORD
    value: wordpress
    secret: true
  - key: WORDPRESS_DB_NAME
    value: wordpress

Secret environment variables

You'll notice that we marked the MYSQL_ROOT_PASSWORD, MYSQL_PASSWORD, and WORDPRESS_DB_PASSWORD variables as secret by setting the secret flag to true for each of them.

Release saves secret variables in an encrypted vault, and exposes these secrets to your running containers at startup.

Click Next step to save your environment variables.

Build arguments

This application does not use any build arguments, so we can click Next step.

Save and deploy

Click Deploy your app to create your application.

Release will now pull two Docker images, mysql and wordpress, then start two services, db and wordpress.

Your browser should redirect to a deployment status page, showing the progress of the setup workflow.

After Release's set-up and deployment workflow completes, navigate to your new environment and click on the URL for the wordpress service.

Install WordPress

You should now see the WordPress setup screen.

Select your language and click Continue.

Create a user and install WordPress by following these steps:

  1. Enter a site title.

  2. Enter a username, which you'll use later to log into WordPress.

  3. Copy and save the recommended password. You'll use this later to log into WordPress.

  4. Enter your email address.

  5. Tick the search engine visibility checkbox. We recommend this to discourage search engines from crawling websites in development.

  6. Click Install WordPress.

Your WordPress service will now run WordPress's installation script, which connects to your MySQL database and adds the required tables and configuration. WordPress also creates an admin user based on the username and password you entered.

Click log in.

Log into WordPress

Enter your username and password, then click Log in.

View your WordPress site

On the WordPress dashboard, hover over your site's name and click visit site.

You should see a sample page on your WordPress site now.

Troubleshooting

If you don't see the WordPress language selector after the deployment workflow above, you can view the logs for each service in your environment to see whether either service logged any errors.

To view a service's logs, navigate to your environment's details page, scroll down to the list of services, and click on logs.

Debugging database connection errors

The most common problem when first installing WordPress is a database connection error.

On Release, this error can be solved by working through the following steps:

  1. Make sure the corresponding environment variables from both services match.

    Values for the variables in each row below should match:

  2. Make sure the WORDPRESS_DB_HOST environment variable matches the name of your database service.

  3. Make sure your database service exposes the correct ports. MySQL on Docker uses ports 3306 and 33060.

Further reading

Docker is most useful when developing WordPress plugins and themes, rather than just configuring a WordPress site.

We recommend pre-installing your themes and plugins if you're using Docker for WordPress development.

Pre-installed themes and plugins

To learn more about how you can pre-install WordPress plugins using Docker Compose, we recommend reading Include pre-installed themes / plugins from the WordPress Docker image documentation.

Persistent volumes for uploads

In the example above, only the database service mounts a persistent volume. This means that the database data for each environment will remain after the application restarts or re-deploys.

The WordPress service does not use a persistent volume.

Images and other files uploaded to your WordPress site will be deleted when you re-deploy your application.

To learn about how to mount a persistent volume for WordPress using Docker Compose, we recommend reading Quickstart: Compose and WordPress.

To learn how to mount a persistent volume on a Release service, see the Volumes section in our template schema documentation.

Last updated