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-build-from-command-line.md
+84-7Lines changed: 84 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,27 +3,66 @@ title: Build a containerized Visual Studio project from the command line
3
3
author: ghogen
4
4
description: Build a container project in Visual Studio using the command line, either with MSBuild.exe or using Docker build, and learn how to enable detailed build logs.
5
5
ms.author: ghogen
6
-
ms.date: 09/17/2024
6
+
ms.date: 9/10/2025
7
7
ms.subservice: container-tools
8
8
ms.topic: how-to
9
9
---
10
10
11
11
# Build a container project from the command line
12
12
13
-
If you want to build a container project with a Dockerfile outside of Visual Studio, you can use `docker build`, `MSBuild`, `dotnet build`, or `dotnet publish` to build from the command line.
13
+
:::moniker range="visualstudio"
14
+
If you want to build a container project with a Dockerfile outside of Visual Studio, you can use `docker build` (or `podman build`), or `dotnet publish /t:PublishContainer` to build from the command line.
14
15
15
-
:::moniker range=">=vs-2022"
16
-
If you're using the .NET SDK build type, you don't have a Dockerfile, so you can't use `docker build`; instead, use `MSBuild`, `dotnet build` or `dotnet publish` to build on the command line.
16
+
If you're using the .NET SDK build type, you don't have a Dockerfile, so you can't use `docker build` or `podman build`; instead, use `dotnet publish /t:PublishContainer` to build on the command line.
17
17
:::moniker-end
18
18
19
+
:::moniker range="vs-2022"
20
+
If you want to build a container project with a Dockerfile outside of Visual Studio, you can use `docker build` or `dotnet publish /t:PublishContainer` to build from the command line.
21
+
22
+
If you're using the .NET SDK build type, you don't have a Dockerfile, so you can't use `docker build`; instead, use `dotnet publish /t:PublishContainer` to build on the command line.
23
+
:::moniker-end
24
+
25
+
:::moniker range="vs-2019"
26
+
If you want to build a container project with a Dockerfile outside of Visual Studio, you can use `docker build` or `dotnet publish /t:PublishContainer` to build from the command line.
27
+
:::moniker-end
28
+
29
+
:::moniker range="visualstudio"
30
+
31
+
## Prerequisites
32
+
33
+
-[Docker Desktop](https://hub.docker.com/editions/community/docker-ce-desktop-windows) or [Podman Desktop](https://podman-desktop.io/downloads).
34
+
-[Visual Studio](https://visualstudio.microsoft.com/downloads/?cid=learn-onpage-download-cta), or for Podman support, [Visual Studio (Insiders)](https://visualstudio.microsoft.com/insiders/?cid=learn-onpage-download-cta), with the **ASP.NET and web development**, **Azure development** workload, and/or **.NET desktop development** workload installed.
-[Visual Studio](https://visualstudio.microsoft.com/downloads/?cid=learn-onpage-download-cta) with the **ASP.NET and web development**, **Azure development** workload, and/or **.NET desktop development** workload installed.
-[Visual Studio 2019 or later](https://visualstudio.microsoft.com/downloads/?cid=learn-onpage-download-cta) with the **ASP.NET and web development**, **Azure development** workload, **.NET desktop development**, and/or **.NET Core cross-platform development** workload installed.
52
+
53
+
:::moniker-end
54
+
55
+
:::moniker range="<=vs-2022"
19
56
## Use Docker build
20
57
21
-
To build a containerized solution from the command line, you can usually use the command `docker build <context>` for each project in the solution. You provide the *build context* argument. The *build context* for a Dockerfile is the folder on the local machine that's used as the working folder to generate the image. For example, it's the folder that you copy files from when you copy to the container. In .NET Core projects, the default is to use the folder that contains the solution file (.sln). Expressed as a relative path, this argument is typically ".." for a Dockerfile in a project folder, and the solution file in its parent folder. For .NET Framework projects, the default build context is the project folder, not the solution folder.
58
+
To build a containerized solution from the command line, you can usually use the command `docker build <context>` for each project in the solution. You provide the *build context* argument. The *build context* for a Dockerfile is the folder on the local machine that's used as the working folder to generate the image. For example, it's the folder that you copy files from when you copy to the container. In .NET Core projects, the default is to use the folder that contains the solution file (.sln or .slnx). Expressed as a relative path, this argument is typically ".." for a Dockerfile in a project folder, and the solution file in its parent folder.
22
59
23
60
```cmd
24
61
docker build -f Dockerfile ..
25
62
```
26
63
64
+
For .NET Framework projects, the default build context is the project folder, not the solution folder.
65
+
27
66
You can set the build context in the project file by setting the `DockerfileContext` property. For example,
28
67
29
68
```xml
@@ -32,21 +71,54 @@ You can set the build context in the project file by setting the `DockerfileCont
32
71
</PropertyGroup>
33
72
```
34
73
74
+
:::moniker-end
75
+
76
+
:::moniker range="visualstudio"
77
+
## Use Docker build or Podman build
78
+
79
+
To build a containerized solution from the command line, you can usually use the command `docker build <context>` (or `podman build <context>`) for each project in the solution. You provide the *build context* argument. The *build context* for a Dockerfile is the folder on the local machine that's used as the working folder to generate the image. For example, it's the folder that you copy files from when you copy to the container. In .NET Core projects, the default is to use the folder that contains the solution file (.sln or .slnx). Expressed as a relative path, this argument is typically ".." for a Dockerfile in a project folder, and the solution file in its parent folder.
80
+
81
+
```cmd
82
+
docker build -f Dockerfile ..
83
+
```
84
+
85
+
```cmd
86
+
podman build -f Dockerfile ..
87
+
```
88
+
89
+
You can set the build context in the project file by setting the `ContainerBuildContext` property. For example,
Relative paths in the Dockerfile are relative to the build context, so if you change the context, be sure to update the relative paths accordingly.
36
100
37
-
:::moniker range=">=vs-2022"
101
+
:::moniker range="vs-2022"
38
102
With Visual Studio 17.11 and later, when you add Docker support to a project, you can specify a folder for the build context. If you want to change the build context, you could delete the Dockerfile (if it doesn't have other changes you want to keep), and rerun **Add Docker support**, this time specifying the new build context. The new Dockerfile will have relative paths updated to correspond to the new build context.
39
103
:::moniker-end
40
104
105
+
:::moniker range="visualstudio"
106
+
When you add container support to a project, you can specify a folder for the build context. If you want to change the build context, you could delete the Dockerfile (if it doesn't have other changes you want to keep), and rerun **Add Container Support**, this time specifying the new build context. The new Dockerfile will have relative paths updated to correspond to the new build context.
107
+
:::moniker-end
108
+
41
109
## Use MSBuild
42
110
43
111
::: moniker range=">=vs-2022"
44
112
> [!NOTE]
45
-
> This section describes how you can customize your Docker containers when you choose the Dockerfile container build type. If you are using the .NET SDK build type, the customization options are different, and the information in this article isn't applicable. Instead, see [Containerize a .NET app with dotnet publish](/dotnet/core/docker/publish-as-container?pivots=dotnet-8-0).
113
+
> This section describes how you can customize your containers when you choose the Dockerfile container build type. If you are using the .NET SDK build type, the customization options are different, and the information in this article isn't applicable. Instead, see [Containerize a .NET app with dotnet publish](/dotnet/core/docker/publish-as-container?pivots=dotnet-8-0).
46
114
::: moniker-end
47
115
116
+
:::moniker range="vs-2022"
117
+
48
118
Dockerfiles created by Visual Studio for .NET Framework projects (and for .NET Core projects created with versions of Visual Studio prior to Visual Studio 2017 Update 4) aren't multistage Dockerfiles. The steps in these Dockerfiles don't compile your code. Instead, when Visual Studio builds a .NET Framework Dockerfile, it first compiles your project using MSBuild. When that succeeds, Visual Studio then builds the Dockerfile, which simply copies the build output from MSBuild into the resulting Docker image. Because the steps to compile your code aren't included in the Dockerfile, you can't build .NET Framework Dockerfiles using `docker build` from the command line. You should use MSBuild to build these projects.
49
119
120
+
:::moniker-end
121
+
50
122
To build an image for single Docker container project, you can use MSBuild with the `/t:ContainerBuild` command option. This command tells MSBuild to build the target `ContainerBuild` rather than the default target `Build`. For example:
51
123
52
124
```cmd
@@ -65,7 +137,12 @@ To view the MSBuild logs, see [Obtaining build logs with MSBuild](../msbuild/obt
65
137
66
138
## Build from the command line
67
139
140
+
:::moniker range="<=vs-2022"
68
141
Visual Studio uses Fast Mode (if enabled) to produce a container image configured to work best for development and debugging, so we don't recommend copying the docker build commands from the Output window after a Fast Mode build. To build a standard image without nonstandard optimizations, you can right-click on the Dockerfile and choose the **Build Docker image** option.
142
+
:::moniker-end
143
+
:::moniker range="visualstudio"
144
+
Visual Studio uses Fast Mode (if enabled) to produce a container image configured to work best for development and debugging, so we don't recommend copying the `docker build` or `podman build` commands from the Output window after a Fast Mode build. To build a standard image without nonstandard optimizations, you can right-click on the Dockerfile and choose the **Build Image** option.
145
+
:::moniker-end
69
146
70
147
Visual Studio uses the `dev` tag to designate images that it has specially prepared to optimize the startup time during debugging. However, these images shouldn't be used outside of the context of Visual Studio. This tag is an indication the images have nonstandard modifications and customizations, for example, to support Fast Mode debugging. See [Customize Docker containers in Visual Studio](container-build.md).
0 commit comments