In this tutorial I will show you how to deploy a multi-container solution to Azure. The example I will use is an ASP.NET 6.0 Razor web app that works with a SQL Server database. The web app and database server run in separate containers. We will setup the solution on the Azure portal.
Assumptions
- .NET 6.0 is installed on your computer
- Docker Desktop is installed on your computer
- You have an Azure subscription
- Git is installed on your computer
- You have a docker hub account
Getting started
Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:6.0COPY dist /appWORKDIR /appEXPOSE 80/tcpENTRYPOINT ["dotnet", "AspMsSQL.dll"]
docker-compose.yml
version: '3.8'services:db:image: mcr.microsoft.com/azure-sql-edgevolumes:- sqlsystem:/var/opt/mssql/- sqldata:/var/opt/sqlserver/data- sqllog:/var/opt/sqlserver/log- sqlbackup:/var/opt/sqlserver/backupports:- "1433:1433"restart: alwaysenvironment:ACCEPT_EULA: YMSSQL_SA_PASSWORD: SqlPassword!webapp:build:context: .dockerfile: Dockerfiledepends_on:- dbports:- "8888:80"restart: alwaysenvironment:- DBHOST=db- DBPORT=1433- DBUSER=sa- DBPASSWORD=SqlPassword!- DBNAME=YellowDB- ASPNETCORE_ENVIRONMENT=Developmentvolumes:sqlsystem:sqldata:sqllog:sqlbackup:
Running the solution on your computer
Cleanup
docker volume rm aspmssql-docker-compose_sqlbackupdocker volume rm aspmssql-docker-compose_sqldatadocker volume rm aspmssql-docker-compose_sqllogdocker volume rm aspmssql-docker-compose_sqlsystem
Prepare solution for Azure deployment
You ensure that you created an image named asp-mssql, type the following command:
docker images
You will see the image that you created among the list of docker images on your computer.
We can now push our image to docker hub. First we need to login into docker-hub with the following command:
docker login --username=snoopy
You will be prompted for your password. If all goes well. you will see the following output:
Login Succeeded
Logging in with your password grants your terminal complete access to your account.
For better security, log in with a limited-privilege personal access token. Learn more at https://docs.docker.com/go/access-tokens/
We now need to push our image to docker-hub with:
docker push snoopy/asp-mssql:1.0.0
The output will be similar to this:
The push refers to repository [docker.io/snoopy/asp-mssql]
5f70bf18a086: Mounted from snoopy/toon3b9aa4fcf4e8: Pusheda41af57309b5: Mounted from snoopy/toon63fa163dde0c: Mounted from snoopy/toon0f53df05d8e3: Mounted from snoopy/toonbc1e58de0815: Mounted from snoopy/toon2edcec3590a4: Mounted from snoopy/toon
If you login to https://hub.docker.com, you will find that the image is sitting in your repository.
Let's modify our docker-compose.yml file so that it used this image on docker-hub instead of building it locally. Open docker-compose.yml in an editor and replace lines 22-24 with:
image: snoopy/asp-mssql:1.0.0
Needless to say that instead of snoopy, you should use your docker-hub username.
The final docker-compose.yml will look like this:
services:
db:
image: mcr.microsoft.com/azure-sql-edge
volumes:
- sqlsystem:/var/opt/mssql/
- sqldata:/var/opt/sqlserver/data
- sqllog:/var/opt/sqlserver/log
- sqlbackup:/var/opt/sqlserver/backup
ports:
- "1433:1433"
restart: always
environment:
ACCEPT_EULA: Y
MSSQL_SA_PASSWORD: SqlPassword!
webapp:
image: snoopy/asp-mssql:1.0.0
depends_on:
- db
ports:
- "8888:80"
restart: always
environment:
- DBHOST=db
- DBPORT=1433
- DBUSER=sa
- DBPASSWORD=SqlPassword!
- DBNAME=YellowDB
- ASPNETCORE_ENVIRONMENT=Development
volumes:
sqlsystem:
sqldata:
sqllog:
sqlbackup:
Deploying solution to Azure
Options | Docker Compose (Preview) |
Image Source | Docker Hub |
Access Type | Public |
Configuration File | Navigate to the docker-compose.yml file and load it. It will load in the text-area below. |
You will see a blue "Go to resource" button once the deployment is completed.
Click on "Go to resource". This takes you to the control page for your web app.Click on the URL on the top right-side to see your solution running in the browser. Be patient because the solution takes some time to load. In my case, the app displayed like this: