How to Fix "Read-Only File System" Error in Azure App Service Due to WEBSITE_RUN_FROM_PACKAGE
If you’re running an application on Azure App Service and encounter an error like:
Invalid operation on Read-Only File System: Create New File
or a message saying:
Your app is currently in read-only mode because you are running from a package file. To make any changes, please update the content in your zip file and WEBSITE_RUN_FROM_PACKAGE app setting.
This means your application is likely running in read-only mode because it’s configured to run from a ZIP package. This setting is controlled by the WEBSITE_RUN_FROM_PACKAGE environment variable in Azure App Service.
In this post, we’ll cover the causes of this issue, how to fix it, and how to prevent it from happening in the future.
What Is WEBSITE_RUN_FROM_PACKAGE
?
WEBSITE_RUN_FROM_PACKAGE
is a feature that lets your app run directly from a ZIP package in Azure App Service. While this can speed up deployments and ensure consistency, it makes the file system read-only.
This works well if your application doesn’t need to modify files at runtime, but if it does, you will encounter issues when trying to write or modify files.
Why Does This Happen?
When you deploy an app using a ZIP file or certain CI/CD pipelines (like Azure DevOps, GitHub Actions, or other tools), Azure may automatically enable WEBSITE_RUN_FROM_PACKAGE
. This causes your app to run from a ZIP file, resulting in the file system being read-only.
Any attempt by your application to create or modify files at runtime will fail because the app is running from this read-only ZIP package.
How to Fix the Read-Only File System Issue
The easiest way to fix this issue is to disable the WEBSITE_RUN_FROM_PACKAGE
setting or modify your deployment pipeline to avoid running in read-only mode.
Solution 1: Set WEBSITE_RUN_FROM_PACKAGE
to 0
You can disable WEBSITE_RUN_FROM_PACKAGE
by setting it to 0
, which will allow your app to use a writable file system.
Here’s how you can do that:
- Go to the Azure Portal.
- Navigate to your App Service.
- Go to "Configuration" under the "Settings" section.
- Find the
WEBSITE_RUN_FROM_PACKAGE
setting and set its value to0
(or delete it entirely). - Save the configuration and restart your app.
This will disable the ZIP package deployment and allow the file system to be writable, resolving the error.
Solution 2: Set WEBSITE_RUN_FROM_PACKAGE
to 0
in the Pipeline
If you're using a CI/CD pipeline (like Azure DevOps), you can configure the pipeline to automatically set WEBSITE_RUN_FROM_PACKAGE
to 0
after deployment.
For example, in Azure Pipelines, you can add the following task:
- task: AzureAppServiceSettings@1
inputs:
azureSubscription: 'your-azure-subscription'
ResourceGroupName: 'your-resource-group'
appName: 'your-app-service'
appSettings: '-WEBSITE_RUN_FROM_PACKAGE 0'
This ensures that after each deployment, WEBSITE_RUN_FROM_PACKAGE
is set to 0
to avoid the read-only file system issue.
Alternatively, you can use the Azure CLI in your pipeline to accomplish the same thing:
- task: AzureCLI@2
inputs:
azureSubscription: 'your-azure-subscription'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
az webapp config appsettings set --resource-group your-resource-group --name your-app-service --settings WEBSITE_RUN_FROM_PACKAGE=0
Solution 3: Use a Writable Directory for File Operations
If you need to keep WEBSITE_RUN_FROM_PACKAGE
enabled but still want to write files at runtime, you can direct your application to use one of the writable directories in Azure App Service.
Writable directories in Azure App Service include:
D:\home
: This directory is writable and persists across restarts.D:\local
: This is a temporary directory that is writable but does not persist across restarts.
You can modify your code to write files to these directories:
var writablePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "myfile.txt");
// Use this path for file operations
Preventing the Issue in Future Deployments
If your pipeline or deployment method automatically enables WEBSITE_RUN_FROM_PACKAGE
, you can prevent this by:
- Disabling ZIP deployment in your pipeline and using another deployment method (like web deploy).
- Ensuring the correct settings are applied after deployment (as shown in Solution 2).
Conclusion
Running from a ZIP package with WEBSITE_RUN_FROM_PACKAGE
is useful for fast and efficient deployments, but it can sometimes lead to a "read-only file system" issue if your app needs to modify files at runtime.
By disabling this setting or adjusting your deployment pipeline, you can ensure your app runs smoothly without encountering file system issues.
Let me know if you need further assistance!
Aucun commentaire:
Enregistrer un commentaire