Azure WebApp with Docker Compose

Overview of Docker Compose :

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Image for post

Compose works in all environments: production, staging, development, testing, as well as CI workflows.

Using Compose is basically a three-step process:

  1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
  2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
  3. Run docker-compose up and Compose starts and runs your entire app.

A sampledocker-compose.yml looks like this:

version: '2'

services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
redis:
image: redis

Compose has commands for managing the whole lifecycle of your application:

Create a multi-container Azure WebApp using a Docker Compose :

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.

We will complete this tutorial in Cloud Shell, but we can also run these commands locally with the Azure CLI command-line tool.

  1. In Cloud Shell, create a tutorial directory and then change to it.

mkdir tutorial

cd tutorial

Image for post

2. Next, we can run the following command to clone the sample app repository to your tutorial directory. Then change to the multicontainerwordpress directory.

git clone https://github.com/Azure-Samples/multicontainerwordpress

cd multicontainerwordpress

Image for post

3. Create a new Resource group in Azure cloud for this demo purpose , may be we can use the location such as “East US” or “Central US”.

az group create — name myResourceGroup — location “East US”

Image for post

4. Next we need to create a new App Service plan since for the web app and functions app the ASP is mainly needed . In this case we need to use the Linux plan accordingly.

az appservice plan create — name myAppServicePlan — resource-group myResourceGroup — sku S1 — is-linux

Image for post

5. Next step is to create a new Docker Compose webapp from the compose file that we have cloned from the GIT repo . we can use the below CLI commands .

az webapp create — resource-group myResourceGroup — plan myAppServicePlan — name <app-name> — multicontainer-config-type compose — multicontainer-config-file docker-compose-wordpress.yml

Image for post
Image for post
Image for post
Image for post

Once after the above steps are completed , we can see the output by the below way :

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 refresh the browser.

Image for post
Congratulations, you’ve created a multi-container app in Web App for Containers.

Connect to MYSQL database to the WebApp:

We need to configure your app to use Azure Database for MySQL. Before that we should not install WordPress as in the compose file we have the DB credentials.

  1. We need to go to the same Azure Resource Group and then start creating the MS-SQL DB.

az mysql server create — resource-group myResourceGroup — name <mysql-server-name> — location “South Central US” — admin-user adminuser — admin-password My5up3rStr0ngPaSw0rd! — sku-name B_Gen4_1 — version 5.7

Creating the server may take a few minutes to complete. When the MySQL server is created, we can see it in our Resource Group.

2. We need to add the Firewall into the SQL DB , as this is the main step since we need to connect from the any source to the destination .

We can Create a firewall rule for our 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.

az mysql server firewall-rule create — name allAzureIPs — server <mysql-server-name> — resource-group myResourceGroup — start-ip-address 0.0.0.0 — end-ip-address 0.0.0.0

3. Next is we need to create our WordPress DB from the MySQL which we created on the step 2 .

az mysql db create — resource-group myResourceGroup — server-name <mysql-server-name> — name wordpress

Configure MySQL 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.

az webapp config appsettings set — resource-group myResourceGroup — name <app-name> — settings WORDPRESS_DB_HOST=”<mysql-server-name>.mysql.database.azure.com” WORDPRESS_DB_USER=”adminuser@<mysql-server-name>” WORDPRESS_DB_PASSWORD=”My5up3rStr0ngPaSw0rd!” WORDPRESS_DB_NAME=”wordpress” MYSQL_SSL_CA=”BaltimoreCyberTrustroot.crt.pem”

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

Image for post

Add persistent storage to the Azure WebApp :

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.

  1. To use 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.

az webapp config appsettings set — resource-group myResourceGroup — name <app-name> — settings WEBSITES_ENABLE_APP_SERVICE_STORAGE=TRUE

2. In the Cloud Shell, type vi docker-compose-wordpress.yml to open the vi 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.

We can use this environment variable in the volumes option so that the WordPress files are installed into persistent storage instead of the container.

version: '3.3'services:
wordpress:
image: microsoft/multicontainerwordpress
volumes:
- ${WEBAPP_STORAGE_HOME}/site/wwwroot:/var/www/html
ports:
- "8000:80"
restart: always

az webapp config container set — resource-group myResourceGroup — name <app-name> — multicontainer-config-type compose — multicontainer-config-file docker-compose-wordpress.yml

Browse to the deployed app at (http://<app-name>.azurewebsites.net).The WordPress container is now using Azure Database for MySQL and persistent storage.

Find WebApp Docker Container logs :

If you run into issues using multiple containers, you can access the container logs by browsing to:

You’ll see output similar to the following example:

[
{
"machineName":"RD00XYZYZE567A",
"lastUpdated":"2018-05-10T04:11:45Z",
"size":25125,
"href":"https://<app-name>.scm.azurewebsites.net/api/vfs/LogFiles/2018_05_10_RD00XYZYZE567A_docker.log",
"path":"/home/LogFiles/2018_05_10_RD00XYZYZE567A_docker.log"
}
]

You see a log for each container and an additional log for the parent process.