In this article

In this article

Web App for Containers provides a flexible way to use Docker images. In this tutorial, you'll learn how to create a multi-container app using WordPress and MySQL. You'll complete this tutorial in Cloud Shell, but you can also run these commands locally with the Azure CLI command-line tool (2.0.32 or later).

In this tutorial, you'll learn how to:

Convert a Docker Compose configuration to work with Web App for Containers

Convert a Kubernetes configuration to work with Web App for Containers

Create a resource group

A resource group is a logical container into which Azure resources like web apps, databases, and storage accounts are deployed and managed. For example, you can choose to delete the entire resource group in one simple step later.

Docker Compose configuration options

For this tutorial, you use the compose file from Docker, but you'll modify it include Azure Database for MySQL, persistent storage, and Redis. Alternatively, you can use a Kubernetes configuration. The configuration files can be found at Azure Samples.

The following lists show supported and unsupported Docker Compose configuration options in Web App for Containers:

Supported options

command

entrypoint

environment

image

ports

restart

services

volumes

Unsupported options

build (not allowed)

depends_on (ignored)

networks (ignored)

secrets (ignored)

Note

Any other options not explicitly called out are also ignored in Public Preview.

Docker Compose with WordPress and MySQL containers

Create a Docker Compose app

In your Cloud Shell, create a multi-container web app in the myAppServicePlan App Service plan with the az webapp create command. Don't forget to replace <app_name> with a unique app name.

Browse to the app

Browse to the deployed app at (http://<app_name>.azurewebsites.net). The app may take a few minutes to load. If you receive an error, allow a few more minutes then refresh the browser. If you're having trouble and would like to troubleshoot, review container logs.

Congratulations, you've created a multi-container app in Web App for Containers. Next you'll configure your app to use Azure Database for MySQL. Don't install WordPress at this time.

Connect to production database

It's not recommended to use database containers in a production environment. The local containers aren't scalable. Instead, you'll use Azure Database for MySQL which can be scaled.

Create an Azure Database for MySQL server

In the following command, substitute your MySQL server name where you see the <mysql_server_name> placeholder (valid characters are a-z, 0-9, and -). This name is part of the MySQL server's hostname (<mysql_server_name>.database.windows.net), it needs to be globally unique.

Configure server firewall

Create a firewall rule for your MySQL server to allow client connections by using the az mysql server firewall-rule create command. When both starting IP and end IP are set to 0.0.0.0, the firewall is only opened for other Azure resources.

Configure database variables in WordPress

To connect the WordPress app to this new MySQL server, you'll configure a few WordPress-specific environment variables, including the SSL CA path defined by MYSQL_SSL_CA. The Baltimore CyberTrust Root from DigiCert is provided in the custom image below.

Use a custom image for MySQL SSL and other configurations

By default, SSL is used by Azure Database for MySQL. WordPress requires additional configuration to use SSL with MySQL. The WordPress 'official image' doesn't provide the additional configuration, but a custom image has been prepared fo your convenience. In practice, you would add desired changes to your own image.

The custom image is based on the 'official image' of WordPress from Docker Hub. The following changes have been made in this custom image for Azure Database for MySQL:

To use the custom image, you'll update your docker-compose-wordpress.yml file. In Cloud Shell, type nano docker-compose-wordpress.yml to open the nano text editor. Change the image: wordpress to use image: microsoft/multicontainerwordpress. You no longer need the database container. Remove the db, environment, depends_on, and volumes section from the configuration file. Your file should look like the following code:

Browse to the app

Browse to the deployed app at (http://<app_name>.azurewebsites.net). The app is now using Azure Database for MySQL.

Add persistent storage

Your multi-container is now running in Web App for Containers. However, if you install WordPress now and restart your app later, you'll find that your WordPress installation is gone. This happens because your Docker Compose configuration currently points to a storage location inside your container. The files installed into your container don't persist beyond app restart. In this section, you'll add persistent storage to your WordPress container.

Configure environment variables

To use of persistent storage, you'll enable this setting within App Service. To make this change, use the az webapp config appsettings set command in Cloud Shell. App settings are case-sensitive and space-separated.

Modify configuration file

In the Cloud Shell, type nano docker-compose-wordpress.yml to open the nano text editor.

The volumes option maps the file system to a directory within the container. ${WEBAPP_STORAGE_HOME} is an environment variable in App Service that is mapped to persistent storage for your app. You'll use this environment variable in the volumes option so that the WordPress files are installed into persistent storage instead of the container. Make the following modifications to the file:

In the wordpress section, add a volumes option so it looks like the following code:

Browse to the app

Browse to the deployed app at (http://<app_name>.azurewebsites.net).

The WordPress container is now using Azure Database for MySQL and persistent storage.

Add Redis container

The WordPress 'official image' does not include the dependencies for Redis. These dependencies and additional configuration needed to use Redis with WordPress have been prepared for you in this custom image. In practice, you would add desired changes to your own image.

The custom image is based on the 'official image' of WordPress from Docker Hub. The following changes have been made in this custom image for Redis:

Configure environment variables

To use Redis, you'll enable this setting, WP_REDIS_HOST, within App Service. This is a required setting for WordPress to communicate with the Redis host. To make this change, use the az webapp config appsettings set command in Cloud Shell. App settings are case-sensitive and space-separated.

Browse to the app

Connect WordPress to Redis

Log-in to WordPress admin. In the left navigation, select Plugins, and then select Installed Plugins.

Show all plugins here

In the plugins page, find Redis Object Cache and click Activate.

Click on Settings.

Click the Enable Object Cache button.

WordPress connects to the Redis server. The connection status appears on the same page.

Congratulations, you've connected WordPress to Redis. The production-ready app is now using Azure Database for MySQL, persistent storage, and Redis. You can now scale out your App Service Plan to multiple instances.

Use a Kubernetes configuration (optional)

In this section, you'll learn how to use a Kubernetes configuration to deploy multiple containers. Make sure you follow earlier steps in to create a resource group and an App Service plan. Since the majority of the steps are similar to that of the compose section, the configuration file has been combined for you.

Supported Kubernetes options for multi-container

args

command

containers

image

name

ports

spec

Note

Any other Kubernetes options not explicitly called out aren't supported in Public Preview.

Kubernetes configuration file

You'll use kubernetes-wordpress.yml for this portion of the tutorial. It is displayed here for your reference:

Create an Azure Database for MySQL server

In the following command, substitute your MySQL server name where you see the <mysql_server_name> placeholder (valid characters are a-z, 0-9, and -). This name is part of the MySQL server's hostname (<mysql_server_name>.database.windows.net), it needs to be globally unique.

Configure server firewall

Create a firewall rule for your MySQL server to allow client connections by using the az mysql server firewall-rule create command. When both starting IP and end IP are set to 0.0.0.0, the firewall is only opened for other Azure resources.

Create a multi-container app (Kubernetes)

In Cloud Shell, create a multi-container web app in the myResourceGroup resource group and the myAppServicePlan App Service plan with the az webapp create command. Don't forget to replace <app_name> with a unique app name.

Configure database variables in WordPress

To connect the WordPress app to this new MySQL server, you'll configure a few WordPress-specific environment variables. To make this change, use the az webapp config appsettings set command in Cloud Shell. App settings are case-sensitive and space-separated.

Add persistent storage

Your multi-container is now running in Web App for Containers. The data will be erased on restart because the files aren't persisted. In this section, you'll add persistent storage to your WordPress container.

Configure environment variables

To use of persistent storage, you'll enable this setting within App Service. To make this change, use the az webapp config appsettings set command in Cloud Shell. App settings are case-sensitive and space-separated.