You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/containers/container-tools-react.md
+6-185Lines changed: 6 additions & 185 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,42 +4,34 @@ titleSuffix: ""
4
4
author: ghogen
5
5
description: Learn how to create a containerized React SPA app with Visual Studio Container Tools and Docker
6
6
ms.author: ghogen
7
-
ms.date: 05/30/2024
7
+
ms.date: 7/24/2025
8
8
ms.subservice: container-tools
9
9
ms.topic: quickstart
10
+
monikerRange: vs-2019
10
11
---
11
12
12
13
# Quickstart: Use Docker with a React Single-page application in Visual Studio
13
14
14
15
With Visual Studio, you can easily build, debug, and run containerized ASP.NET Core apps, including those with client-side JavaScript such as React.js single-page application (SPA), and publish them to Azure Container Registry, Docker Hub, Azure App Service, or your own Container Registry. In this article, we publish to Azure Container Registry.
15
16
17
+
> [!NOTE]
18
+
> This article applies to Visual Studio 2019. If you're using Visual Studio 2022, new JavaScript project templates exist which make the scenario much easier. See [Tutorial: Create an ASP.NET Core app with React in Visual Studio - Docker](../javascript/tutorial-asp-net-core-with-react.md?view=vs-2022&preserve-view=true#docker).
-[Visual Studio 2022](https://visualstudio.microsoft.com/downloads/?cid=learn-onpage-download-cta) with the **Web Development**, **Azure Tools** workload, and/or **.NET Core cross-platform development** workload installed
29
-
- To publish to Azure Container Registry, an Azure subscription. [Sign up for a free trial](https://azure.microsoft.com/offers/ms-azr-0044p/).
30
-
-[Node.js](https://nodejs.org/en/download/)
31
-
- For Windows containers, Windows 10 version 1809 or later, to use the Docker images referenced in this article.
32
-
33
-
::: moniker-end
34
28
35
29
## Installation and setup
36
30
37
31
For Docker installation, first review the information at [Docker Desktop for Windows: What to know before you install](https://docs.docker.com/docker-for-windows/install/#what-to-know-before-you-install). Next, install [Docker Desktop](https://hub.docker.com/editions/community/docker-ce-desktop-windows).
38
32
39
33
## Create a project and add Docker support
40
34
41
-
::: moniker range="vs-2019"
42
-
43
35
1. Create a new project using the **ASP.NET Core with React.js** template.
44
36
45
37

@@ -75,7 +67,7 @@ If you're targeting .NET 8 or later, the default Dockerfile that Visual Studio c
75
67
76
68
For all .NET versions, use the following steps to update the Dockerfile to install Node.js:
77
69
78
-
1. Add the following lines to install curl, Node.js 14.x, and certain required Node libraries in the container. Be sure to add these lines both in the first section, to add the installation of the Node package manager `npm.exe` to the base image, as well as in the `build` section.
70
+
- Add the following lines to install curl, Node.js 14.x, and certain required Node libraries in the container. Be sure to add these lines both in the first section, to add the installation of the Node package manager `npm.exe` to the base image, as well as in the `build` section.
79
71
80
72
```dockerfile
81
73
RUN apt-get update
@@ -194,147 +186,15 @@ Update the Dockerfile by adding the following lines. These lines will copy Node
194
186
195
187
1. Update the `.dockerignore` file by removing the `**/bin`.
196
188
197
-
::: moniker-end
198
-
::: moniker range=">=vs-2022"
199
-
200
-
Follow these steps if you're using Visual Studio 2022 version 17.8 or later:
201
-
202
-
1. Create a new project using the **React and ASP.NET Core** template.
203
-
204
-
:::image type="content" alt-text="Screenshot of creating a new React and ASP.NET Core project." source="media/container-tools-react/vs-2022/react-and-asp-net-core.png" lightbox="media/container-tools-react/vs-2022/react-and-asp-net-core.png" :::
205
-
206
-
1. On the **Additional information** screen, select **Enable container support**. Be sure to select **Dockerfile** option, since you'll need to manually make changes to that file.
207
-
208
-
:::image type="content" alt-text="Screenshot of creating a React and ASP.NET Core project - Additional information screen." source="media/container-tools-react/vs-2022/additional-information-net-8.png" lightbox="media/container-tools-react/vs-2022/additional-information-net-8.png" :::
209
-
210
-
> [!NOTE]
211
-
> In some versions of Visual Studio 2022, this option is not enabled, but you can add that support later.
212
-
213
-
Visual Studio creates two projects - one for the React JavaScript client code, and another for the ASP.NET Core C# server code.
214
-
215
-
1. If you didn't add Docker container support during project creation, right-click on the server project node, and choose **Add** > **Docker Support** and be sure to choose the Dockerfile option to create a Dockerfile.
216
-
217
-
:::image alt-text="Screenshot of Add Docker support menu item." type="content" source="media/container-tools-react/vs-2022/add-docker-support.png" lightbox="media/container-tools-react/vs-2022/add-docker-support.png" :::
218
-
219
-
1. Select the container type.
220
-
221
-
Use the following steps for Visual Studio 2022 version 17.0 to 17.7:
222
-
223
-
1. Create a new project using the **ASP.NET Core with React.js** template.
224
-
225
-

226
-
227
-
1. On the **Additional information** screen, you can't select **Enable Docker Support**, but don't worry, you can add that support later.
228
-
229
-

230
-
231
-
1. Right-click on the project node, and choose **Add** > **Docker Support** to add a Dockerfile to your project.
232
-
233
-

234
-
235
-
1. Select the container type.
236
-
237
-
:::moniker-end
238
-
239
189
## Debug
240
190
241
-
:::moniker range=">=vs-2022"
242
-
With Visual Studio 2022 version 17.9 or later and the **React and ASP.NET Core** template that uses `vite.js`, the projects are already configured to start both the client and server projects with debugging support, but you need to set up the right port for the single-page application (SPA) proxy to use to access the ASP.NET Core server running in the container. You can get the host port from the **Containers** window in Visual Studio and set it in the React project as described in [Create a React app - Docker](../javascript/tutorial-asp-net-core-with-react.md#docker).
243
-
244
-
You can also disable the launch in the browser for the server, which is set up to open with Swagger, which is not required for this scenario. To disable the browser launch, open the **Properties** (**Alt**+**Enter**), go to the **Debug** tab, and click on the link **Open debug launch profiles UI**, and clear the **Launch browser** checkbox.
245
-
246
-
If you're using an earlier version of Visual Studio 2022, continue reading to set up debugging with the single-page application (SPA) proxy server.
247
-
248
-
The project uses the SPA Proxy during debugging. See [Improved single-page app (SPA) templates](https://devblogs.microsoft.com/dotnet/asp-net-core-updates-in-net-6-preview-4/#improved-single-page-app-spa-templates). When debugging, the JavaScript client runs on the host machine, but the ASP.NET Core server code runs in the container. When published, the proxy isn't run, and the client code runs on the same server as the ASP.NET Core code. You already have a Debug profile **Docker* that you can use to debug the server code. To debug the JavaScript client code, you can create an additional debug profile. You also need to start the proxy manually from a command prompt when debugging JavaScript. You can leave it running through multiple debug sessions.
249
-
250
-
1. Build the project, if not already built.
251
-
252
-
1. Open a Visual Studio dev command prompt, go to the ClientApp folder in your project, and then give the command, `npm run start`. You should see something like this:
253
-
254
-
```output
255
-
Compiled successfully!
256
-
257
-
You can now view project3_spa in the browser.
258
-
259
-
Local: https://localhost:44407
260
-
On Your Network: https://192.168.1.5:44407
261
-
262
-
Note that the development build isn't optimized.
263
-
To create a production build, use npm run build.
264
-
265
-
webpack compiled successfully
266
-
```
267
-
268
-
> [!NOTE]
269
-
> Note the local URL. You need to provide this in a debug launch profile, which is stored in your `launchSettings.json` file.
270
-
271
-
1. Open the dropdown list that contains debug profiles (next to the green triangle icon or **Start** button), and choose **{ProjectName} Debug Properties**, and choose the **Docker** profile.
272
-
1. Check the **Environment variables** section and add the following environment variables if not already present:
1. Set the **URL** to `https://localhost:{proxy-port}` where `{proxy-port}` is the port from the proxy server (from step 1).
277
-
278
-

279
-
280
-
This action changes the Docker entry in the `launchSettings.json` file and launches the correct URL for the local proxy running on the host. Find the `launchSettings.json` file in **Solution Explorer** under **Properties**.
281
-
282
-
1. You should see something like the following code:
> Do not set the launch settings option `publishAllPorts` to `true` if you're using a proxy. That option publishes all exposed ports to a random port, which won't work when you set a specific port in the SPA proxy.
301
-
302
-
1. Open the file *ClientApp/src/setupProxy.js* and change the line that sets the target to use the `localhost` address and port on the container. You can find the port on the **Ports** tab of the **Containers** window.
303
-
304
-
```javascript
305
-
consttarget='https://localhost:{container-port}';
306
-
```
307
-
308
-
If you're using HTTPS, be sure to choose the right port for HTTPS, which is 443 in this tutorial.
309
-
310
-
1. Launch the app with debugging (**F5**).
311
-
312
-

313
-
314
-
If you get a build error trying to write the output assemblies, you might have to stop a previously running container to unlock the files.
315
-
316
-
1. Verify that you can hit a breakpoint in client-side JavaScript code by setting a breakpoint in **ClientApp/src/components/Counter.js** in the **incrementCounter** function, and then try hitting the breakpoint by clicking the **Increment** button on the Counters page.
1. Next, try hitting a breakpoint in the server-side ASP.NET Core code. Set a breakpoint in `WeatherController.cs` in the `Get` method and try appending `/weatherforecast` to the base `localhost` and port URL to activate that code.
321
-
322
-

323
-
324
-
1. If the container port changes, which can happen if you make a significant change, such as updating `launchSettings.json` or updating the debug launch profile in the IDE, you need to update the port in `setupProxy.js` and also restart the proxy. Terminate the current proxy (**Ctrl**+**C** in the command window where it's running), and then restart it using the same command `npm run start`.
325
-
326
-
:::moniker-end
327
-
328
-
:::moniker range="vs-2019"
329
-
330
191
Select **Docker** from the debug dropdown list in the toolbar, and start debugging the app. You might see a message with a prompt about trusting a certificate; choose to trust the certificate to continue. The first time you build, Docker downloads the base images, so it might take a bit longer.
331
192
332
193
The **Container Tools** option in the **Output** window shows what actions are taking place. You should see the installation steps associated with *npm.exe*.
333
194
334
195
The browser shows the app's home page.
335
196
336
197

337
-
:::moniker-end
338
198
339
199
## Containers window
340
200
@@ -357,8 +217,6 @@ You can also view the images and inspect information about them. Choose the **Im
357
217
358
218
Once the develop and debug cycle of the app is completed, you can create a production image of the app.
359
219
360
-
:::moniker range="vs-2019"
361
-
362
220
1. Change the configuration dropdown list to **Release** and build the app.
363
221
1. Right-click your project in **Solution Explorer** and choose **Publish**.
364
222
1. On the publish target dialog, select **Docker Container Registry**.
@@ -391,43 +249,6 @@ Once the develop and debug cycle of the app is completed, you can create a produ
|**DNS Prefix**| Globally unique name | Name that uniquely identifies your container registry. |
414
-
|**Subscription**| Choose your subscription | The Azure subscription to use. |
415
-
|**[Resource Group](/azure/azure-resource-manager/resource-group-overview)**| myResourceGroup | Name of the resource group in which to create your container registry. Choose **New** to create a new resource group.|
416
-
|**[SKU](/azure/container-registry/container-registry-skus)**| Standard | Service tier of the container registry |
417
-
|**Registry Location**| A location close to you | Choose a Location in a [region](https://azure.microsoft.com/regions/) near you or near other services that use your container registry. |
418
-
419
-

420
-
421
-
1. Select **Create**, and then select **Finish**.
422
-
423
-

424
-
425
-
When the publish process ends, you can review the publish settings, and edit them, when needed, or publish the image again using the **Publish** button.
0 commit comments