Skip to content

Commit e394ec7

Browse files
author
Jill Grant
authored
Merge pull request #13112 from ghogen/customize-container
Split up large article about customizing containerized app projects
2 parents 86feca1 + 7a14844 commit e394ec7

10 files changed

+445
-275
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
title: Build a containerized Visual Studio project from the command line
3+
author: ghogen
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+
ms.author: ghogen
6+
ms.date: 09/17/2024
7+
ms.subservice: container-tools
8+
ms.topic: how-to
9+
---
10+
11+
# Build a container project from the command line
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.
14+
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.
17+
:::moniker-end
18+
19+
## Use Docker build
20+
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.
22+
23+
```cmd
24+
docker build -f Dockerfile ..
25+
```
26+
27+
You can set the build context in the project file by setting the `DockerfileContext` property. For example,
28+
29+
```xml
30+
<PropertyGroup>
31+
<DockerfileContext>contextfolder</DockerfileContext>
32+
</PropertyGroup>
33+
```
34+
35+
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+
37+
:::moniker range=">=vs-2022"
38+
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+
:::moniker-end
40+
41+
## Use MSBuild
42+
43+
::: moniker range=">=vs-2022"
44+
> [!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).
46+
::: moniker-end
47+
48+
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+
50+
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+
52+
```cmd
53+
MSBuild MyProject.csproj /t:ContainerBuild /p:Configuration=Release
54+
```
55+
56+
You see output similar to what you see in the **Output** window when you build your solution from the Visual Studio IDE. Always use `/p:Configuration=Release`, since in cases where Visual Studio uses the multistage build optimization, results when building the **Debug** configuration might not be as expected. See [Customize container images for debugging](container-debug-customization.md).
57+
58+
If you're using a Docker Compose project, use this command to build images:
59+
60+
```cmd
61+
msbuild /p:SolutionPath=<solution-name>.sln /p:Configuration=Release docker-compose.dcproj
62+
```
63+
64+
To view the MSBuild logs, see [Obtaining build logs with MSBuild](../msbuild/obtaining-build-logs-with-msbuild.md).
65+
66+
## Related content
67+
68+
- [MSBuild properties for container projects](container-msbuild-properties.md).
69+
- [MSBuild](../msbuild/msbuild.md)

0 commit comments

Comments
 (0)