Deploy a web application with Docker on Azure
This tutorial guides you through the process of containerizing your web application using Docker and deploying it to Azure App Service. Azure App Service provides a fully managed platform for building, deploying, and scaling web apps and APIs, and it has excellent support for Docker containers.
Prerequisites
- An Azure account with an active subscription. If you don't have one, you can create a free account.
- Docker installed on your local machine. You can download it from docker.com.
- An Azure CLI installed on your local machine. Install Azure CLI.
- A web application project ready to be containerized. For this tutorial, we'll assume a simple Node.js or Python web app.
Step 1: Containerize Your Application
First, you need to create a Dockerfile
in the root directory of your web application. This file defines how your application will be built into a Docker image.
Example Dockerfile (Node.js):
# Use an official Node runtime as a parent image
FROM node:18-alpine
# Set the working directory in the container
WORKDIR /usr/src/app
# Copy package.json and package-lock.json
COPY package*.json ./
# Install app dependencies
RUN npm install
# Bundle app source
COPY . .
# Make port 8080 available to the world outside this container
EXPOSE 8080
# Define environment variable
ENV NODE_ENV production
# Run the app when the container launches
CMD [ "node", "server.js" ]
Example Dockerfile (Python Flask):
# Use an official Python runtime as a parent image
FROM python:3.9-slim
# Set the working directory in the container
WORKDIR /app
# Copy the requirements file into the container at /app
COPY requirements.txt ./
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Copy the current directory contents into the container at /app
COPY . .
# Make port 5000 available to the world outside this container
EXPOSE 5000
# Run app.py when the container launches
CMD ["python", "app.py"]
Make sure to adjust the EXPOSE
port and the CMD
instruction to match your application's configuration.
Step 2: Build and Test the Docker Image Locally
Open your terminal or command prompt, navigate to your application's root directory (where the Dockerfile
is located), and run the following command to build the Docker image:
docker build -t my-web-app .
Replace my-web-app
with a name for your image.
Once the build is complete, test your container locally:
# For Node.js (assuming EXPOSE 8080)
docker run -p 8080:8080 my-web-app
# For Python Flask (assuming EXPOSE 5000)
docker run -p 5000:5000 my-web-app
Open your web browser and navigate to http://localhost:8080
(or the port you exposed) to verify that your application is running correctly inside the container.
Step 3: Create an Azure Container Registry
You need a place to store your Docker image. Azure Container Registry (ACR) is a managed, private Docker registry service that you can use to store and manage your private Docker container images and related artifacts.
Create ACR using Azure CLI:
# Log in to your Azure account
az login
# Set your subscription (replace with your actual subscription ID)
az account set --subscription "YOUR_SUBSCRIPTION_ID"
# Create a resource group (if you don't have one)
az group create --name myResourceGroup --location eastus
# Create an Azure Container Registry
az acr create --resource-group myResourceGroup --name myContainerRegistryName --sku Basic --location eastus
Replace myContainerRegistryName
with a unique name for your registry.
Step 4: Log in to ACR and Push the Docker Image
Log in to your Azure Container Registry:
az acr login --name myContainerRegistryName
Tag your local Docker image with your ACR login server:
# Get your ACR login server name
az acr show --name myContainerRegistryName --query loginServer --output tsv
# Tag your image (replace with your ACR login server)
docker tag my-web-app /my-web-app:v1
Push the tagged image to your ACR:
docker push /my-web-app:v1
Step 5: Deploy to Azure App Service
Now you can create an Azure App Service instance and configure it to pull your Docker image from ACR.
Using Azure CLI
Create an App Service plan:
az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --sku B1 --is-linux
Create a Web App for Containers:
az webapp create --resource-group myResourceGroup --plan myAppServicePlan --name myDockerWebApp --deployment-container-image-name /my-web-app:v1
Replace myDockerWebApp
with a unique name for your web app.
Configure ACR credentials for the web app:
az webapp identity assign --resource-group myResourceGroup --name myDockerWebApp
# Get the system assigned managed identity's principal ID
PRINCIPAL_ID=$(az webapp identity show --resource-group myResourceGroup --name myDockerWebApp --query principalId --output tsv)
# Grant the web app's identity access to pull images from ACR
az acr permission update --name myContainerRegistryName --resource-group myResourceGroup --id $PRINCIPAL_ID --actions pull
Finally, configure the web app to use your ACR credentials:
az webapp config container set --resource-group myResourceGroup --name myDockerWebApp --docker-registry-server-url https://myContainerRegistryName.azurecr.io --docker-registry-server-user --docker-registry-server-password YOUR_ACR_PASSWORD
You can get your ACR password using az acr credential show --name myContainerRegistryName --query passwords[0].name --output tsv
and then az acr credential show --name myContainerRegistryName --query passwords[0].value --output tsv
, or preferably, use a service principal for better security in production scenarios.
Using the Azure Portal
- Navigate to the Azure Portal.
- Click on Create a resource.
- Search for "Web App" and select it.
- Click Create.
- On the Basics tab:
- Select your Subscription and Resource group.
- Enter a unique Name for your web app.
- Select Docker Container as the Publish option.
- Choose your preferred Operating System (Linux).
- Select a Region.
- Choose or create an App Service plan.
- On the Docker tab:
- Select Azure Container Registry.
- Choose your Container Registry.
- Select your Image and tag (e.g.,
my-web-app:v1
). - Ensure the Start Up Command is correctly set if needed.
- Click Review + create, and then Create.
- After deployment, navigate to your web app and ensure it's running correctly. You might need to configure ACR access permissions for the App Service's managed identity.
Step 6: Access Your Deployed Application
Once the deployment is complete, you can access your web application by navigating to its URL, which will be in the format http://your-docker-web-app-name.azurewebsites.net
.
Tip:
For production environments, consider using Azure Kubernetes Service (AKS) for more advanced container orchestration capabilities or Azure Container Instances (ACI) for running single containers without managing infrastructure.
Next Steps
Explore more advanced deployment strategies, such as CI/CD pipelines with Azure DevOps or GitHub Actions, to automate your Docker deployments.