分享好友 最新动态首页 最新动态分类 切换频道
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

最新文章
电脑打不开pdf文件需要安装什么软件(电脑打不开pdf文件怎么办)
摘要:电脑打不开pdf文件需要安装什么软件(电脑打不开pdf文件怎么办),电脑突然打不开PDF文件的原因可能是缺少专用的阅读器,一般来说电脑都是有自带的,没有估计是被自己删除,具体解决方法步骤如下:1、首先找到一个PDF文件并单击。2、借
电饭煲排行榜及选购技巧
在现今社会,各种电器的使用已经成为主流。厨房里对食品进行蒸、煮、炖、煨等多种操作功能都离不开电饭煲作用的发挥。电饭煲,又称作电锅、电饭锅。是利用电能转变为内能的炊具,使用方便,清洁卫生。电饭煲作为一种厨房常用的电器,在生活
靖江市深入推进“高效办成一件事” 已办理近7.5万件 办理时限缩短80%
12月12日上午,市数据局举办政务服务开放日活动,并聚焦“高效办成一件事”,与卫健委、住建局、交通运输局、市场监管局等部门深入开展“我陪群众走流程”活动。行风监督员代表受邀参加活动。数据显示,2024年,市、镇、村三级政务服务大厅
硬核、高能,龙蜥社区走进中科方德 MeetUp 圆满结束(附技术PPT下载)
11 月 29日,以“OS 升级 迭代·兼容·安全”为主题的​​龙蜥社区「走进系列」​​之走进中科方德 MeetUp 在北京成功召开,线上观看人次超 2 万。现场来自海光信息、阿里云、浪潮信息、三未信安等企业的专家为大家带来了精彩的主题演讲,
人工智能机器视觉相机:定义、分类以及应用
引言:人工智能(Artificial Intelligence,简称AI)的快速发展使得人工智能机器视觉相机成为了制造业、安防监控、智能交通等领域的关键技术。本文将从定义、分类和应用三个方面来阐述人工智能机器视觉相机的相关知识。正文:定义:人工智
華為巴黎旗艦店舉辦GoPaint全球創作活動
/美通社/ -- 華為在其巴黎旗艦店(巴黎歌劇院旁)成功舉辦了GoPaint全球創作活動。本次活動由兩位才華橫溢的插畫師Mayada和Kenza主導,旨在通過可在華為MatePad系列設備上使用的GoPaint應用展現數字藝術的潛力以及實現創作的便捷性。該活動
科技通报“小拉抢单器新款怎么用教程”其实确实有挂
小拉抢单器新款怎么用教程是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可以加我微下载使用。抢单可以一键让你轻松成为“必赢”。其操作方式十分简单,打开这个应用便可以自定义抢单系统规律,只需要输入自己
盘点即将上市的新款手机 你更喜欢哪一款?
点击上方电脑爱好者关注我们转眼2019年已经过半,手机圈的竞争也即将进入下半场,5G、折叠屏、UFS3.0、屏幕摄像头、全身无开孔设计等元素都会一一在新品中呈现。作为消费者,咱们暂时无需将目光投的太远,如果聚焦当下,将于近期上市的新款
高效SEO排名神器,助您网站流量翻倍,率飙升
推荐一款SEO快速排名软件,助您高效优化网站,快速提升流量与,让您的网站在搜索引擎中脱颖而出。随着互联网的快速发展,越来越多的企业开始重视网络营销,而SEO(搜索引擎优化)作为网络营销的重要组成部分,已经成为了企业提高网站流量、
深圳一医生被曝拒诊,耽误患者!官方回应 | 医脉3分钟
今天的医疗圈发生了哪些与你有关的大事?更新、更全的医学动态3分钟一网打尽今日关键词:韩语学生,轻断食,职业病类别,创新医疗器械来源 | 医脉通作者 |晚报君新闻60秒➤76岁老人被接到医院“免费”做手术,家属投诉并质疑医院@第一帮帮
相关文章
推荐文章
发表评论
0评