avatarSascha Peter Bajonczak

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3178

Abstract

B_HOST</span><span class="hljs-punctuation">:</span> <span class="hljs-string">db:3306</span> <span class="hljs-attribute">WORDPRESS_DB_USER</span><span class="hljs-punctuation">:</span> <span class="hljs-string">wordpress</span> <span class="hljs-attribute">WORDPRESS_DB_PASSWORD</span><span class="hljs-punctuation">:</span> <span class="hljs-string">wordpress</span> <span class="hljs-attribute">volumes</span><span class="hljs-punctuation">:</span> <span class="hljs-attribute">db_data</span><span class="hljs-punctuation">:</span></pre></div><p id="da09">this is your base setup. It defines the MySQL database and the WordPress instance. The WordPress will wait for the database, to spin up and start then the web instance. Now it’s time to generate the web application</p><h1 id="e40e">Create a Web app</h1><p id="d836">To create a web app from the YAML Template, you will create a Resource group first, will call them <code>myResourceGroup</code>:</p><div id="00e0"><pre>az <span class="hljs-keyword">group</span> <span class="hljs-title">create</span> — name myResourceGroup — <span class="hljs-keyword">location</span> <span class="hljs-title">“West</span> Europe” </pre></div><p id="bf1a">After that, you must create an App Service Plan that will later host the webpage</p><div id="9dfb"><pre>az appservice plan <span class="hljs-keyword">create</span><span class="hljs-type">name</span> myAppServicePlan — resource-<span class="hljs-keyword">group</span> myResourceGroup — sku S1 — <span class="hljs-keyword">is</span>-linux</pre></div><p id="0e27">Now it’s time to spin up the WordPress hosted by a multi docker image composed by docker-compose</p><div id="1b93"><pre>az webapp create — resource-<span class="hljs-keyword">group</span> <span class="hljs-title">myResourceGroup</span> — plan myAppServicePlan — name myWebApplication — multicontainer-config-<span class="hljs-keyword">type</span> compose — multicontainer-config-file docker-compose-wordpress.yml</pre></div><p id="fe76">After this, you can navigate then to your page and you will see the installation WordPress installation wizard appearing. So far so good.</p><h1 id="29c1">The Problem</h1><p id="65d5">If your web application now goes to sleep or you restart it or you redeploy it then the configuration that you did the last time gets lost. To prevent this, it will be required to store the data outside of the container. Here come the persistent volumes to the rescue.</p><h1 id="41e0">Persistent Volumes</h1><p id="d3fd">Initially, the web application does not have the persist volume feature enabled. You can do this by the following az command in the shell</p><div id="4f73"><pre>az webapp<span class="hljs-built_in"> config </span>appsettings <span class="hljs-built_in">set</span> --resource-group myResourceGroup --name myWebApplication--settings <span class="hljs-attribute">WEBSITES_ENABLE_APP_SERVICE_STORAGE</span>=<span class="hljs-literal">TRUE</span></pre></div><p id="6a59">This will set the Setting <code>WEBSITES_ENABLE_APP_SERVICE_STORAGE</code> to true, to tell the web app that it must use the Shared Store. Typically it will be mounted into the /home direct

Options

ory. You can see this, by using the <code>df -h</code> command</p><figure id="8f01"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*IueJHKPY1h4_gX88QYk8Eg.png"><figcaption></figcaption></figure><p id="3f52">After that, you must now use the <code>{WEBAPP_STORAGE_HOME}</code> variable to reference the <code>/home</code> directory. So you can now modify the compose definition like this, to store the data (MySQL and WordPress) onto the persistent storage</p><div id="1a13"><pre><span class="hljs-attribute">version</span>: '<span class="hljs-number">3</span>.<span class="hljs-number">3</span>'</pre></div><div id="6114"><pre><span class="hljs-symbol">services:</span> <span class="hljs-symbol"> db:</span> <span class="hljs-symbol"> image:</span> mysql:<span class="hljs-number">5.7</span> <span class="hljs-symbol"> volumes:</span> - <span class="hljs-punctuation">{</span>WEBAPP_STORAGE_HOME<span class="hljs-punctuation">}</span><span class="hljs-keyword">/site/</span>db:<span class="hljs-keyword">/var/</span>lib/mysql <span class="hljs-symbol"> restart:</span> always <span class="hljs-symbol"> environment:</span> <span class="hljs-symbol"> MYSQL_ROOT_PASSWORD:</span> somewordpress <span class="hljs-symbol"> MYSQL_DATABASE:</span> wordpress <span class="hljs-symbol"> MYSQL_USER:</span> wordpress <span class="hljs-symbol"> MYSQL_PASSWORD:</span> wordpress <span class="hljs-symbol"> wordpress:</span> <span class="hljs-symbol"> image:</span> wordpress:latest <span class="hljs-symbol"> volumes:</span> - $<span class="hljs-punctuation">{</span>WEBAPP_STORAGE_HOME<span class="hljs-punctuation">}</span><span class="hljs-keyword">/site/</span>wwwroot:<span class="hljs-keyword">/var/</span>www/html <span class="hljs-symbol"> ports:</span> - <span class="hljs-string">"8000:80"</span> <span class="hljs-symbol"> restart:</span> always <span class="hljs-symbol"> environment:</span> <span class="hljs-symbol"> WORDPRESS_DB_HOST:</span> db:<span class="hljs-number">3306</span> <span class="hljs-symbol"> WORDPRESS_DB_USER:</span> wordpress <span class="hljs-symbol"> WORDPRESS_DB_PASSWORD:</span> wordpress</pre></div><p id="06d7">From now on the database data will be stored in <code>/home/db</code> and any application-based data on <code>/home/site/wwwroot</code>. If you scale up the service to use 2 or more nodes, this does not matter, because the <code>/home</code> directory will be shared through each instance.</p><h1 id="7b9b">Final words</h1><p id="6f31">I like the docker support in azure, with the persistent volumes you will have the ability to store the data outside the container. So the manually entered data will be stored in a safe way so that a recreation of the container will not destroy the complete work. What do you think about this support?</p><div id="9e0b"><pre>Want <span class="hljs-keyword">to</span> <span class="hljs-keyword">Connect</span>?</pre></div><div id="e881"><pre><span class="hljs-attribute">Say</span> Hello <span class="hljs-literal">on</span>: LinkedIn, GitHub, and Blog.</pre></div></article></body>

Work With Persistent Volumes in Azure Webapps and Docker

Persistent volumes are now available in azure web apps

I work with Azure Web apps for a while. Now I discovered the docker support in Azure Web apps. this is a nice feature, but there are some settings that are not very well documented (in my opinion). One of them is the mounting of volumes for a persistent storage

I will show you, how to create a WordPress application in Azure with a persistent volume.

What are volumes?

A volume in docker allows you to persist data that live outside of your container. So Volumes are like external shared for docker

So let’s spin up the azure command shell in the browser

The docker-compose.yml

I will use docker-compose this will be supported in azure web apps, and it is very easy to handle.

to create a docker-compose.yml in the azure shell you simply type

nano docker-compose-wordpress.yml

After that, you can copy the following yml content

version: '3.3'
services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress
wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
volumes:
    db_data:

this is your base setup. It defines the MySQL database and the WordPress instance. The WordPress will wait for the database, to spin up and start then the web instance. Now it’s time to generate the web application

Create a Web app

To create a web app from the YAML Template, you will create a Resource group first, will call them myResourceGroup:

az group create — name myResourceGroup — location “West Europe”

After that, you must create an App Service Plan that will later host the webpage

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

Now it’s time to spin up the WordPress hosted by a multi docker image composed by docker-compose

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

After this, you can navigate then to your page and you will see the installation WordPress installation wizard appearing. So far so good.

The Problem

If your web application now goes to sleep or you restart it or you redeploy it then the configuration that you did the last time gets lost. To prevent this, it will be required to store the data outside of the container. Here come the persistent volumes to the rescue.

Persistent Volumes

Initially, the web application does not have the persist volume feature enabled. You can do this by the following az command in the shell

az webapp config appsettings set --resource-group myResourceGroup --name myWebApplication--settings WEBSITES_ENABLE_APP_SERVICE_STORAGE=TRUE

This will set the Setting WEBSITES_ENABLE_APP_SERVICE_STORAGE to true, to tell the web app that it must use the Shared Store. Typically it will be mounted into the /home directory. You can see this, by using the df -h command

After that, you must now use the ${WEBAPP_STORAGE_HOME} variable to reference the /home directory. So you can now modify the compose definition like this, to store the data (MySQL and WordPress) onto the persistent storage

version: '3.3'
services:
   db:
     image: mysql:5.7
     volumes:
       - ${WEBAPP_STORAGE_HOME}/site/db:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress
   wordpress:
     image: wordpress:latest
     volumes:
       - ${WEBAPP_STORAGE_HOME}/site/wwwroot:/var/www/html
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress

From now on the database data will be stored in /home/db and any application-based data on /home/site/wwwroot. If you scale up the service to use 2 or more nodes, this does not matter, because the /home directory will be shared through each instance.

Final words

I like the docker support in azure, with the persistent volumes you will have the ability to store the data outside the container. So the manually entered data will be stored in a safe way so that a recreation of the container will not destroy the complete work. What do you think about this support?

Want to Connect?
Say Hello on: LinkedIn, GitHub, and Blog.
Azure
Docker
DevOps
Web Development
Programming
Recommended from ReadMedium