Bald Bearded Builder
cloud/azure-file-shares-as-container-volume-mounts

Azure File Shares as Container Volume Mounts

Azure File Storage can provide persistent volume mounts for containerized apps running in Azure App Service through path mappings and Docker Compose.

Loads from YouTube when you press play

Transcript 4 topics

Why containers need storage

0:00

Need to scale your app? Put it in the container. Need more resiliency? Put it in the container. Is your code starting to smell?

Put it in the container. Are you lacking unit tests? It's okay. Just put it in the container. Welcome back, my name is Michael Jolley and I'm the bald bearded builder.

And today I wanna talk about using Docker containers and Azure file storage as volume mounts. Containers are great and they solve a lot of needs and they also cause some problems if you're not aware of them. So one of the things that'll get you really quick, and I'm sure all of you have had the situation, when you first start saving data or saving files in a container, and then you restart it one day and realize, rah rah rah raggy, my data is gone. That's the nature of containers. They're transient.

They don't persist, right? You can't expect a container that's running your app right now to be running your container in five minutes. So where do you put that data? Well, if you're watching this video, you probably already know if you're saving files, it's using a volume mount. But when you move your app to the cloud, it gets a little more complex because you can't just say load this host directory as a mount on this container that's running here.

You've gotta use something a little more special. And in Azure, luckily you can use Azure storage. So we're gonna talk about that today. How do you add a Azure file storage as a volume out in your app service that's running? So why would you do that?

Well, let's take this as an example. Let's say you're building a web app and you're allowing users to upload their profile pictures, right? Common scenario. You need to put that file somewhere, you can't save it within the container because, you know, bye bye. But you could save it to something like Azure storage, specifically a file storage inside of Azure storage.

You could save it to a database if you wanted, but I don't recommend that. But let's just say you're saving that file in there or perhaps somebody's uploading CSV or Excel files into your application and you need to process those. Well, you don't wanna save them in that container for the same reasons. So to get started, we're gonna need an Azure App Service and Azure File Storage. And with those two combined, we'll have our solution.

Set up the file share

2:21

So I'm gonna assume you already have a storage account and an app service ready to go and we just need to configure. So first, let's go configure our storage account. We wanna click on the file shares blade and then click on the new file share. Get that created. We're gonna just put in a name there.

We're gonna use the default quota and tiers for now, and we'll then we'll click create file share. That'll get us a file share ready to use, and then inside of that, we need to create a directory. Now we can press, you know, add directory and then give it a name, press okay. You can create as many directories or subdirectories as you need. It's your file structure, but you need to remember what paths you're using because we're gonna use that later when we mount it to the app service.

Mount Azure file storage

3:08

Okay, so now we wanna configure the app service. We can go into the configuration blade, click on path mappings and click on the new Azure storage mount button. On the screen, name is the most important field. You're gonna use this later in your Docker Compose to let your container know what the name of the share is. Under storage account, choose the account that has your file share.

For storage type, choose Azure files. Then you can select your previously created file share. Under mount path, you wanna put the path that you created, that directory structure that you created in that file share. So you map it to wherever you want, start it with a slash and go to town.

Wire the Docker volume

3:52

Okay, we're nearly there. Now we wanna open a Docker Compose file. It's gonna tell our container how to connect to that mount that we've created. Add a volumes property under the service that we need to configure the mount for. And then for the pattern, let's use the mount name that we configured when we set up this path mapping, colon, whatever path we want it to be inside the container.

This will make the contents of that Azure file share look like it's a local path inside the container. So if we need to save files to the file share, we just save them to that path locally or read from that path locally, and it'll get saved automatically out to that Azure file share. Azure's made this fairly simple and now you can save your files without worry about them getting lost in between container restarts. Not just restarts, but when the container gets replaced. You don't wanna lose that stuff and now you won't, you've got the file share to handle it.

Do you use containers inside of Azure? How are you handling file storage and the like? Let me know in the comments below, I'm really interested. If you'd like some more content like this, be sure to hit the subscribe button below. And hopefully you'll wanna hit the like button below, I'd appreciate that.

I love talking about Docker and Azure and all things software development. So if you wanna hang out and talk more, be sure to join our Twitch stream. We stream there a couple of days a week and you can find the link in the description. Thanks for hanging out with me until next time.

Sign in to join in. Reading needs nothing.