Implement Containerized Solutions
This guide covers the AZ-204 exam topics for implementing containerized solutions using Docker and Azure services:
- Create and manage container images for solutions
- Publish an image to Azure Container Registry
- Run containers by using Azure Container Instance
- Create solutions by using Azure Container Apps
Prerequisites
- Docker
- Python (Optional)
- Azure Subscription (Free tier or $200 credit recommended for minimal costs)
- Existing Resource Group (
az204
)
Build the Dockerfile
Follow these steps to create the Dockerfile:
-
Create a directory for your files, then create a
Dockerfile
with the content below. -
After setting up the directory, create the
Dockerfile
.
Step 1: Create the directory
mkdir azurecompute25
cd azurecompute25
Step 2: Create the Dockerfile
FROM python:3.9-slim
WORKDIR /app
COPY . .
RUN pip install flask
EXPOSE 5000
CMD ["python", "app.py"]
Create the Flask App
In the same directory, create a file named app.py
with the following content:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello from Docker!'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
Build and Test Docker Locally
Follow these steps to build and test the Docker image locally:
-
Build the Docker image using the command below.
-
Run the container to test it.
-
Open
http://localhost:5000
in a browser (should display "Hello from Docker!"). -
Stop the container with
Ctrl+C
.
Step 1: Build the Docker image
docker build -t whateveryouwant:1.0 .
Step 2: Run the container
docker run -p 5000:5000 whateveryouwant:1.0
Create a Container Registry
Follow these steps to create an Azure Container Registry (ACR):
-
Create the ACR using the command below.
-
Enable admin access for the ACR.
-
Retrieve the credentials for the ACR.
Step 1: Create the ACR
az acr create --resource-group az204 --name acryourname --sku Basic
Step 2: Enable admin access
az acr update -n acryourname --admin-enabled true
Step 3: Retrieve credentials
az acr credential show --name acryourname
Push the Image to ACR
Follow these steps to push the Docker image to ACR:
-
Log in to ACR using the command below.
-
Tag the image for ACR.
-
Push the image to ACR.
-
In the Azure Portal, go to your ACR, click Repositories, and verify
myapp
is listed.
Step 1: Log in to ACR
docker login acryourname.azurecr.io --username <username> --password <password>
Step 2: Tag the image
docker tag whateveryouwant:1.0 acryourname.azurecr.io/myapp:1.0
Step 3: Push the image
docker push acryourname.azurecr.io/myapp:1.0
Deploy Container to Azure Container Instance (ACI)
Follow these steps to deploy the container to Azure Container Instance (ACI):
-
Deploy the container using the command below.
-
Test the container by retrieving its FQDN.
-
Open
http://<fqdn>:5000
in a browser (should display "Hello from Docker!"). -
In the Azure Portal, navigate to your Container Instance to verify it’s running.
Step 1: Deploy the container
az container create --resource-group az204 --name az204viacli --os-type Linux --cpu 1 --memory 1 --image acryourname.azurecr.io/myapp:1.0 --registry-login-server acryourname.azurecr.io --registry-username <username> --registry-password <password> --ports 5000 --dns-name-label az204viacli --location westus3
Step 2: Test the container
az container show --resource-group az204 --name az204viacli --query ipAddress.fqdn --output tsv
Deploy to Container Apps Environment
Follow these steps to deploy the container to Azure Container Apps:
-
Create a Container Apps Environment using the command below.
-
Deploy the app to the Container Apps Environment.
-
In the Azure Portal, go to your Resource Group, click on your Container App, and click the URL to verify it displays "Hello from Docker!".
Step 1: Create a Container Apps Environment
az containerapp env create --name az204AppEnv --resource-group az204 --location westus3
Step 2: Deploy the app
az containerapp create --name az204containerapp --resource-group az204 --environment az204AppEnv --image acryourname.azurecr.io/myapp:1.0 --registry-server acryourname.azurecr.io --registry-username <username> --registry-password <password> --target-port 5000 --ingress external
Clean Up (Optional)
To avoid exceeding the $200 credit limit, delete all resources:
az group delete -n az204 --no-wait --yes
Next Steps
- Repeat the process using only the Azure Portal.