title
stringlengths
3
46
content
stringlengths
0
1.6k
11:409
If you installed WSL, please select Linux, since it is the default used by the Docker server when WSL is available.
11:410
11:411
The advantage of enabling Docker Compose instead of just Docker is that you can manually configure how the image is run on the development machine, as well as how Docker image ports are mapped to external ports by editing the Docker Compose files that are added to the solution.
11:412
If your Docker runtime has been installed properly and is running, you should be able to run the Docker image from Visual Studio. Please try it!
11:413
Now that we have explored how to configure and run Docker images, let’s delve deeper into the structure and composition of these images. Understanding the Docker file created by Visual Studio is key to grasping how it orchestrates the creation and management of these images.
11:414
Analyzing the Docker file
11:415
Let’s analyze the Docker file that was created by Visual Studio. It is a sequence of image creation steps. Each step enriches an existing image with something else with the help of the From instruction, which is a reference to an already existing image. The following is the first step:
11:416
FROM mcr.microsoft.com/dotnet/aspnet:x.x AS base
11:417
WORKDIR /app
11:418
EXPOSE 80
11:419
EXPOSE 443
11:420
11:421
The first step uses the mcr.microsoft.com/dotnet/aspnet:x.x ASP.NET (Core) runtime that was published by Microsoft in the Docker public repository (where x.x is the ASP.NET (Core) version that was selected in your project).
11:422
The WORKDIR command creates the directory that follows the command within the image that is going to be created. The two EXPOSE commands declare which ports will be exposed outside the image and mapped to ports of the actual hosting machine. Mapped ports are decided in the deployment stage either as command-line arguments of a Docker command or within a Docker Compose file. In our case, there are two ports: one for HTTP (80) and another for HTTPS (443).
11:423
This intermediate image is cached by Docker, which doesn’t need to recompute since it doesn’t depend on the code we write but only on the selected version of the ASP.NET (Core) runtime.
11:424
The second step produces a different image that will not be used to deploy. Instead, it will be used to create application-specific files that will be deployed:
11:425
FROM mcr.microsoft.com/dotnet/core/sdk:x.x AS build
11:426
WORKDIR /src
11:427
COPY [`MvcDockerTest/MvcDockerTest.csproj`, `MvcDockerTest/`]
11:428
RUN dotnet restore MvcDockerTest/MvcDockerTest.csproj
11:429
COPY . .
11:430
WORKDIR /src/MvcDockerTest
11:431
RUN dotnet build MvcDockerTest.csproj -c Release -o /app/build
11:432
FROM build AS publish
11:433
RUN dotnet publish MvcDockerTest.csproj -c Release -o /app/publish
11:434
11:435
This step starts from the ASP.NET SDK image, which contains parts we don’t need to add for deployment; these are needed to process the project code. The new src directory is created in the build image and made the current image directory. Then, the project file is copied into /src/MvcDockerTest.
11:436
The RUN command executes an operating system command on the image. In this case, it calls the dotnet runtime, asking it to restore the NuGet packages that were referenced by the previously copied project file.
11:437
Then, the COPY.. command copies the whole project file tree into the src image directory. Finally, the project directory is made the current directory, and the dotnet runtime is asked to build the project in release mode and copy all the output files into the new /app/build directory. Finally, the dotnet publish task is executed in a new image called publish, outputting the published binaries into /app/publish.
11:438
The final step starts from the image that we created in the first step, which contains the ASP.NET (Core) runtime and adds all the files that were published in the previous step:
11:439
FROM base AS final
11:440
WORKDIR /app
11:441
COPY --from=publish /app/publish .
11:442
ENTRYPOINT [`dotnet`, `MvcDockerTest.dll`]
11:443
11:444
The ENTRYPOINT command specifies the operating system command that’s needed to execute the image. It accepts an array of strings. In our case, it accepts the dotnet command and its first command-line argument – that is, the DLL we need to execute. With that out of the way, let’s now publish our little project!
11:445
Publishing the project
11:446
If we right-click on our project and click Publish, we are presented with several options:
11:447
11:448
Publish the image to an existing or new web app (automatically created by Visual Studio)
11:449
Publish to one of several Docker registries, including a private registry in Azure Container Registry that, if it doesn’t already exist, can be created from within Visual Studio
11:450
11:451
Docker Compose support allows you to run and publish a multi-container application and add further images, such as a containerized database that is available everywhere.
11:452
The following Docker Compose file instructs the Docker server to run two containerized ASP.NET applications:
11:453
version: '3.4'
11:454
services:
11:455
mvcdockertest:
11:456
image: ${DOCKER_REGISTRY-}mvcdockertest
11:457
build:
11:458
context: .
11:459
dockerfile: MvcDockerTest/Dockerfile
11:460
mvcdockertest1:
11:461
image: ${DOCKER_REGISTRY-}mvcdockertest1
11:462
build:
11:463
context: .
11:464
dockerfile: MvcDockerTest1/Dockerfile
11:465
11:466
You can add another ASP:NET Core MVC application to our previous docker-compose file by just adding another ASP:NET Core MVC application named MvcDockerTest 1 to the solution and by enabling docker-compose on it. However, you must pay attention to the fact that the newly created project folder is placed inside the same solution folder as MvcDockerTest.
11:467
The preceding code references existing Docker files. Any environment-dependent information is placed in the docker-compose.override.yml file, which is merged with the docker-compose.yml file when the application is launched from Visual Studio:
11:468
version: '3.4'
11:469
services:
11:470
mvcdockertest:
11:471
environment:
11:472
- ASPNETCORE_ENVIRONMENT=Development
11:473
- ASPNETCORE_URLS=https://+:443;http://+:80
11:474
ports:
11:475
- `80`
11:476
- `443`
11:477
volumes:
11:478
-${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
11:479
- ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
11:480
mvcdockertest1:
11:481
environment:
11:482
- ASPNETCORE_ENVIRONMENT=Development
11:483
- ASPNETCORE_URLS=https://+:443;http://+:80
11:484
ports:
11:485
- `80`
11:486
- `443`
11:487
volumes:
11:488
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
11:489
- ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
11:490
11:491
For each image, the file specifies some environment variables (which will be defined in the image when the application is launched), the port mappings, and some host files.
11:492
The files in the host are directly mapped into the images. Each declaration contains the path in the host, how the path is mapped in the image, and the desired access rights. In our case, volumes are used to map the machine key used for all encryption needs of the application and the self-signed HTTPS certificate that’s used by Visual Studio.
11:493
When you launch the application in Visual Studio, just the browser window opens and shows the MvcDockerTest application. However, both applications are launched, so you just need to discover which port MvcDockerTest1 is running on and open another browser window. You can discover the port by clicking on MvcDockerTest1 in the Containers tab in Visual Studio and looking at its HTTPS Host Port (60072), as shown in the figure below:
11:494
11:495
Figure 11.8: Discovering the MvcDockerTest1 host port
11:496
Now, suppose we want to add a containerized SQL Server instance. We would need something like the following instructions split between docker-compose.yml and docker-compose.override.yml:
11:497
sql.data:
11:498
image: mcr.microsoft.com/mssql/server:2022-latest
11:499
environment:
11:500
- SA_PASSWORD=Pass@word
11:501
- ACCEPT_EULA=Y
11:502
- MSSQL_PID=Express
11:503
ports:
11:504
- `5433:1433`
11:505
11:506
Here, the preceding code specifies the properties of the SQL Server container, as well as the SQL Server configuration and installation parameters. More specifically, the preceding code contains the following information:
11:507
11:508
sql.data is the name that’s given to the container.