分享好友 最新动态首页 最新动态分类 切换频道
Add Docker support using the Dockerfile container build type
2024-12-26 21:50

The tools included in Visual Studio for developing with Docker containers are easy to use, and greatly simplify building, debugging, and deployment for containerized applications. You can work with a container for a single project, or use container orchestration with Docker Compose or Service Fabric to work with multiple services in containers.

Add Docker support using the Dockerfile co<i></i>ntainer build type

niker="vs-2019">
  • Docker Desktop
  • Visual Studio 2019 with the Web Development, Azure Tools workload, and/or .NET Core cross-platform development workload installed
  • .NET Core Development Tools for development with .NET Core.
  • To publish to Azure Container Registry, an Azure subscription. Sign up for a free trial.

Docker support is available for ASP.NET projects, ASP.NET Core projects, and .NET Core and .NET Framework console projects.

The support for Docker in Visual Studio has changed over a number of releases in response to customer needs. There are two levels of Docker support you can add to a project, and the supported options vary by the type of project and the version of Visual Studio. With some supported project types, if you just want a container for a single project, without using orchestration, you can do that by adding Docker support. The next level is container orchestration support, which adds appropriate support files for the particular orchestrator you choose.

With Visual Studio 2019, you can use Docker Compose, Kubernetes, and Service Fabric as container orchestration services.

In Visual Studio 2019 version 16.4 and later, the Containers window is available, which lets you view running containers, browse available images, view environment variables, logs, and port mappings, inspect the filesystem, attach a debugger, or open a terminal window inside the container environment. See Use the Containers window.

niker="vs-2022">
  • Docker Desktop
  • Visual Studio 2022 with the Web Development, Azure Tools workload, and/or .NET desktop development workload installed
  • To publish to Azure Container Registry, an Azure subscription. Sign up for a free trial.

Docker support is available for ASP.NET projects, ASP.NET Core projects, and .NET Core and .NET Framework console projects.

The support for Docker in Visual Studio has changed over a number of releases in response to customer needs. There are several options to add Docker support to a project, and the supported options vary by the type of project and the version of Visual Studio. With some supported project types, if you just want a container for a single project, without using orchestration, you can do that by adding Docker support. The next level is container orchestration support, which adds appropriate support files for the particular orchestrator you choose.

With Visual Studio 2022 version 17.9 and later, when you add Docker support to a .NET 7 or later project, you have two container build types to choose from for adding Docker support. You can choose to add a Dockerfile to specify how to build the container images, or you can choose to use the built-in container support provided by the .NET SDK.

Also, with Visual Studio 2022 and later, when you choose container orchestration, you can use Docker Compose or Service Fabric as container orchestration services.

In Visual Studio 2022, the Containers window is available, which lets you view running containers, browse available images, view environment variables, logs, and port mappings, inspect the filesystem, attach a debugger, or open a terminal window inside the container environment. See Use the Containers window.

You can enable Docker support during project creation by selecting Enable Docker Support when creating a new project, as shown in the following screenshot:

niker="vs-2019">
niker="vs-2022">

You can add Docker support to an existing project by selecting Add > Docker Support in Solution Explorer. The Add > Docker Support and Add > Container Orchestrator Support commands are located on the right-click menu (or context menu) of the project node for an ASP.NET Core project in Solution Explorer, as shown in the following screenshot:

niker="vs-2019">
niker="vs-2022">
niker="vs-2019">

When you add or enable Docker support, Visual Studio adds the following to the project:

  • a Dockerfile file
  • a file
  • a NuGet package reference to the Microsoft.VisualStudio.Azure.Containers.Tools.Targets

The Dockerfile you add will resemble the following code. In this example, the project was named , and you chose Linux containers:


niker="vs-2022">

When you add or enable Docker support to a .NET 7 or later project, Visual Studio shows the Container Scaffolding Options dialog box, which gives you the choice of operating system (Linux or Windows), but also the ability to choose the container build type, either Dockerfile or .NET SDK. This dialog box does not appear in .NET Framework projects or Azure Functions projects.

In 17.11 and later, you can also specify the Container Image Distro and the Docker Build Context.

Container Image Distro specifies which OS image your containers use as the base image. This list changes if you switch between Linux and Windows as the container type.

The following images are available:

Windows:

  • Windows Nano Server (recommended, only available 8.0 and later, not preset for Native Ahead-of-time (AOT) deployment projects)
  • Windows Server Core (only available 8.0 and later)

Linux:

  • Default (Debian, but the tag is "8.0")
  • Debian
  • Ubuntu
  • Chiseled Ubuntu
  • Alpine

Docker Build Context specifies the folder that is used for the Docker build. See Docker build context. The default is the solution folder, which is strongly recommended. All the files needed for a build need to be under this folder, which is usually not the case if you choose the project folder or some other folder.

If you choose Dockerfile, Visual Studio adds the following to the project:

  • a Dockerfile file
  • a .dockerignore file
  • a NuGet package reference to the Microsoft.VisualStudio.Azure.Containers.Tools.Targets

The Dockerfile you add will resemble the following code. In this example, the project was named , and you chose Linux containers:



With Visual Studio 2022 17.9 and later with the .NET 7 SDK installed, in ASP.NET Core projects that target .NET 6 or later, you have the option of using .NET SDK's built-in support for container builds, which means you don't need a Dockerfile; see Containerize a .NET app with dotnet publish. Instead, you configure your containers using MSBuild properties in the project file, and the settings for launching the containers with Visual Studio are encoded in a configuration file, launchSettings.json.

Here, choose .NET SDK as the container build type to use .NET SDK's container management instead of a Dockerfile.

Container Image Distro specifies which OS image your containers use as the base image. This list changes if you switch between Linux and Windows as the container. See the previous section for a list of available images.

The .NET SDK container build entry in launchSettings.json looks like the following code:


The .NET SDK manages some of the settings that would have been encoded in a Dockerfile, such as the container base image, and the environment variables to set. The settings available in the project file for container configuration are listed at Customizing your container. For example, the Container Image Distro is saved in the project file as the property. You can change it later by editing the project file.


The Containers window lets you view containers and images on your machine and see what's going on with them. You can view the filesystem, volumes mounted, environment variables, ports used, and examine log files.

Open the Containers window by using the quick launch (Ctrl+Q) and typing . You can use the docking controls to put the window somewhere. Because of the width of the window, it works best when docked at the bottom of the screen.

Select a container, and use the tabs to view the information that's available. To check it out, run your Docker-enabled app, open the Files tab, and expand the app folder to see your deployed app on the container.

niker="vs-2019">
niker="vs-2022">

For more information, see Use the Containers window.

When you want to compose a multi-container solution using Docker Compose, add container orchestrator support to your projects. This lets you run and debug a group of containers (a whole solution or group of projects) at the same time if they're defined in the same docker-compose.yml file.

To add container orchestrator support using Docker Compose, right-click on the project node in Solution Explorer, and choose Add > Container Orchestrator Support. Then choose Docker Compose to manage the containers.

After you add container orchestrator support to your project, you see a Dockerfile added to the project (if there wasn't one there already) and a docker-compose folder added to the solution in Solution Explorer, as shown here:

niker="vs-2019">
niker="vs-2022">

If docker-compose.yml already exists, Visual Studio just adds the required lines of configuration code to it.

Repeat the process with the other projects that you want to control using Docker Compose.

If you work with a large number of services, you can save time and computing resources by selecting which subset of services you want to start in your debugging session. See Start a subset of Compose services.

With Service Fabric tools in Visual Studio, you can develop and debug for Azure Service Fabric, run and debug locally, and deploy to Azure.

Visual Studio 2019 and later support developing containerized microservices using Windows containers and Service Fabric orchestration.

For a detailed tutorial, see Tutorial: Deploy a .NET application in a Windows container to Azure Service Fabric.

For more information on Azure Service Fabric, see Service Fabric.

Visual Studio integrates readily with Azure Pipelines for automated and continuous integration and delivery of changes to your service code and configuration. To get started, see Create your first pipeline.

For Service Fabric, see Tutorial: Deploy your ASP.NET Core app to Azure Service Fabric by using Azure DevOps Projects.

For further details on the services implementation and use of Visual Studio tools for working with containers, read the following articles:

Debugging apps in a local Docker container

Deploy an ASP.NET container to a container registry using Visual Studio

Deploy to Azure App Service

最新文章
百度优化推广打压揭秘,破解与逆袭之道
百度优化推广打压现象普遍存在,影响企业推广效果。应对策略包括优化内容质量、利用多样化推广渠道、加强与百度沟通以及关注行业动态。突破方法包括提升用户体验、创新推广手段和提升品牌影响力。随着互联网的快速发展,搜索引擎优化()和
阳江市西门子中国(授权)一级代理商-西门子选型-西门子技术支持-西门子维修服务
1. 设置PG/PC接口打开计算机的控制面板,将右上角的查看方式改为大图标,找到 “设置PG/PC 接口”或“Set PG/PC Interface(32-bit)”并双击该图标。在弹出的窗口中上方的应用程序访问点的下拉菜单中选择 “S7ONLINE(STEP7)”,并在下方
视频提取人声消除背景音乐,五个软件帮你办到
在视频的后期编辑制作中,视频提取人声消除背景音乐这一操作有利于获取清晰、准确的语音内容,也方便我们后续把人声和背景音乐单独编辑、从而应用到其他的音视频中。如何准确地消除视频中的背景音乐,获得清晰的人声呢?本文将介绍几种软件
漫漫漫画免费版 v5.2.4.2官方版
海量连载漫画资源,全部免费,极速阅读:经典热门的国漫,韩国漫画,日本漫画,香港漫画,古风漫画、腐漫耽美,二次元bil小伙伴集结圣地,人人都是漫画家!1、超高清漫画画质,丰富的漫画资源库2、每日更新漫画章节3、热门漫画阅读榜单,漫
计算机伦理期末题库2023 免费
计算机伦理作业 (第1次) 1. 强力法和训练法各有什么优缺点?分别适用于什么应用场景?它们的结合有什么必要性和优势? 强力法: 强力法(或称为规则引擎)是一种基于规则的人工智能技术,它依赖
璟云里/2024售楼处发布-首页网站-青浦璟雲里百度百科-上海房天下
1站华为,0.41低密墅居【璟云里】入市在即!约138-215㎡3-5房联排,总价约700万起,实景示范区持续开放中!青浦『璟云里』售楼处电话☎:400-1189-515【已认证】在售户型图丨项目介绍丨最新房源丨周边配套丨详细解答〢◆开发商营销中心诚挚
推他她,精准定位,优化关键词,助力企业高效营销
随着互联网的快速发展,企业间的竞争日益激烈。如何在众多竞争者中脱颖而出,成为每个企业关注的焦点。推他她作为一款专业的工具,以其精准的定位和高效的优化功能,受到了广大企业的青睐。本文将从多个角度阐述推他她优化关键词的优势,为
震撼发布 | AI门户2.0:智能新纪元,未来已来!
[未来已来]AI门户2.0一站式AI探索平台
【MAX(移动版)360清理大师下载】酷比MAX 移动版360清理大师8.4.2免费下载
清理大师:更简单,更畅快!6亿用户信赖的手机清理!清理大师是一款完全免费的手机加速与空间清理软件,强力加速使手机运行更流畅,一键清理快速解决空间不足问题。【一键清理】一键搞定无用垃圾,手机清理更省心【手机加速】瞬间释放手机
项目计划书PPT是对项目计划书内容的展示和解读,它可帮助项目团队更好地向利益相关者传达项目信息。以下是制作项目计划
在当今这个数字化时代人工智能()已经成为推动各行各业创新和发展的关键力量。随着技术的不断进步越来越多的企业开始探索将融入其业务流程以期提升效率、减少成本并创造新的商业价值。成功实项目并非易事它需要周密的规划、严谨的实策略以
相关文章
推荐文章
发表评论
0评