question

VENKATESHH-9422 avatar image
0 Votes"
VENKATESHH-9422 asked VENKATESHH-9422 commented

MSBuild Switch for Building Docker Image

Is it possible to build docker image (docker build command) implicitly / directly from msbuild (say, thru a flag or switch). Searched a lot, but couldn't find anything.

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

cooldadtx avatar image
0 Votes"
cooldadtx answered VENKATESHH-9422 commented

MSBuild is for building a project, not containers. In general you will have a build script that runs that would handle this. In a build server environment the script generally pulls down the code, sets up any build infrastructure, builds the solution(s), runs unit tests and then publishes the build and/or builds containers. MSBuild is only responsible for one of these steps.

If for some reason you really, really want to do this as part of the build then the workaround would be to create a post-build event in your project. However post build events run after a project builds, not the solution. Therefore you'd have to ensure it was the last project to build. Then you can run any command line tools you need to. But, again, this is not recommended. Furthermore post build events run in VS as well so you probably don't want to waste the time building a container while debugging.

Finally note that VS has built in support for containers. While I haven't used it myself I suspect VS can help generate the scripts you need.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I was trying to use a custom target with Exec task where I was trying to call docker build. But I ran into issues. Other observation is that, when I try to build a docker image from VS, it builds the project also, not sure why? Anyways, that's a different question.

0 Votes 0 ·

MSBuild is per project so as I mentioned before unless you ensure the project you assign that build task to as last in the list of projects built by the solution then you're going to run into issues. Even then I wouldn't recommend it. Use a build script that runs outside MSBuild. That is how most apps beyond the most trivial work anyway and sets you up for using a build server. A simple PS or BAT file is sufficient and both approaches can build docker containers.

0 Votes 0 ·

Ok. Thanks a lot for your prompt response.

0 Votes 0 ·