Skip to content

Commit 7889f53

Browse files
Merge pull request #13215 from ghogen/tutorial-multicontainer
Update the Docker Compose multicontainer tutorial for .NET 8
2 parents af207f9 + ba981c9 commit 7889f53

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed
-41 KB
Loading
26 KB
Loading

docs/containers/tutorial-multicontainer.md

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,9 @@ Don't select **Enable Docker Support**. You add Docker support later in the proc
4545
::: moniker-end
4646
::: moniker range=">=vs-2022"
4747

48-
> [!NOTE]
49-
> In Visual Studio 2022 17.2 and later, you can use Azure Functions for this project instead.
50-
51-
![Screenshot showing Create ASP.NET Core Web App project.](./media/tutorial-multicontainer/vs-2022/create-web-project.png)
52-
5348
Don't select **Enable Docker Support**. You add Docker support later in the process.
5449

55-
![Screenshot of the Additional information screen when creating a web project. The option to Enable Docker Support is not selected.](./media/tutorial-multicontainer/vs-2022/create-web-project-additional-information.png)
56-
50+
![Screenshot of the Additional information screen when creating a web project. The option to Enable Docker Support is not selected.](./media/tutorial-multicontainer/vs-2022/create-web-project.png)
5751
::: moniker-end
5852

5953
## Create a Web API project
@@ -66,7 +60,10 @@ Add a project to the same solution and call it *MyWebAPI*. Select **API** as the
6660
::: moniker-end
6761

6862
:::moniker range=">=vs-2022"
69-
1. Add a project to the same solution and call it *WebAPI*. Select **API** as the project type, and clear the checkbox for **Configure for HTTPS**. In this design, we're only using SSL for communication with the client, not for communication from between containers in the same web application. Only `WebFrontEnd` needs HTTPS and the code in the examples assumes that you have cleared that checkbox. In general, the .NET developer certificates used by Visual Studio are only supported for external-to-container requests, not for container-to-container requests.
63+
1. Add a project to the same solution and call it *MyWebAPI*. Select **API** as the project type, and clear the checkbox for **Configure for HTTPS**.
64+
65+
> [!NOTE]
66+
> In this design, we're only using HTTPS for communication with the client, not for communication from between containers in the same web application. Only `WebFrontEnd` needs HTTPS and the code in the examples assumes that you have cleared that checkbox. In general, the .NET developer certificates used by Visual Studio are only supported for external-to-container requests, not for container-to-container requests.
7067
7168
![Screenshot of creating the Web API project.](media/tutorial-multicontainer/vs-2022/create-web-api-project.png)
7269

@@ -289,12 +286,25 @@ Congratulations, you're running a Docker Compose application with a custom Docke
289286
```csharp
290287
public async Task OnGet()
291288
{
289+
// Call *mywebapi*, and display its response in the page
292290
using (var client = new System.Net.Http.HttpClient())
293291
{
294-
// Call *mywebapi*, and display its response in the page
295292
var request = new System.Net.Http.HttpRequestMessage();
296-
// webapi is the container name
297-
request.RequestUri = new Uri("http://webapi/Counter");
293+
294+
// A delay is a quick and dirty way to work around the fact that
295+
// the mywebapi service might not be immediately ready on startup.
296+
// See the text for some ideas on how you can improve this.
297+
// Uncomment for .NET 8 only
298+
// await System.Threading.Tasks.Task.Delay(10000);
299+
300+
// mywebapi is the service name, as listed in docker-compose.yml.
301+
// Docker Compose creates a default network with the services
302+
// listed in docker-compose.yml exposed as host names.
303+
// The port 8080 is exposed in the WebAPI Dockerfile.
304+
// If your WebAPI is exposed on port 80 (the default for HTTP, used
305+
// with earlier versions of the generated Dockerfile), change
306+
// or delete the port number here.
307+
request.RequestUri = new Uri("http://mywebapi:8080/Counter");
298308
var response = await client.SendAsync(request);
299309
string counter = await response.Content.ReadAsStringAsync();
300310
ViewData["Message"] = $"Counter value from cache :{counter}";
@@ -305,6 +315,12 @@ Congratulations, you're running a Docker Compose application with a custom Docke
305315
> [!NOTE]
306316
> In real-world code, you shouldn't dispose `HttpClient` after every request. For best practices, see [Use HttpClientFactory to implement resilient HTTP requests](/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests).
307317

318+
The Uri given references a service name defined in the *docker-compose.yml* file. Docker Compose sets up a default network for communication between containers using the listed service names as hosts.
319+
320+
The code shown here works with .NET 8 and later, which sets up a user account in the Dockerfile without administrator privileges, and exposes port 8080 because the HTTP default port 80 is not accessible without elevated privilege.
321+
322+
The delay is used here as a workaround for .NET 8 only, because this example code could run immediately on application launch, before the MyWebAPI service is ready to receive web requests.
323+
308324
1. In the `Index.cshtml` file, add a line to display `ViewData["Message"]` so that the file looks like the following code:
309325

310326
```cshtml
@@ -329,7 +345,11 @@ Congratulations, you're running a Docker Compose application with a custom Docke
329345

330346
1. Choose **Docker Compose**.
331347

332-
1. Choose your Target OS, for example, Linux.
348+
1. **Visual Studio 17.12 and later** Choose the scaffolding options for the WebFrontEnd project.
349+
350+
![Screenshot showing Container Scaffolding Options dialog for the WebFrontEnd project.](media/tutorial-multicontainer/vs-2022/webfrontend-container-options.png)
351+
352+
**Visual Studio 17.11 and earlier** Choose your Target OS, for example, Linux.
333353

334354
![Screenshot of choosing the Target OS.](media/tutorial-multicontainer/docker-tutorial-docker-support-options.PNG)
335355

@@ -348,8 +368,6 @@ Congratulations, you're running a Docker Compose application with a custom Docke
348368
dockerfile: WebFrontEnd/Dockerfile
349369
```
350370

351-
The `version` specified in the first line is the [Docker Compose file version](https://docs.docker.com/compose/compose-file/#version-top-level-element). You normally shouldn't change it, since it's used by the tools to understand how to interpret the file.
352-
353371
The `.dockerignore` file contains file types and extensions that you don't want Docker to include in the container. These files are generally associated with the development environment and source control, not part of the app or service you're developing.
354372

355373
Look at the **Container Tools** section of the output pane for details of the commands being run. You can see the command-line tool `docker-compose` is used to configure and create the runtime containers.

0 commit comments

Comments
 (0)