Dockerize a .NET Core application
Dockerize a .NET Core application
Introduction
This example demonstrates how to dockerize an ASP.NET Core application.
Why build ASP.NET Core?
- Open-source
- Develop and run your ASP.NET Core apps cross-platform on Windows, MacOS and Linux
- Great for modern cloud-based apps, such as web apps, IoT apps and mobile backends
- ASP.NET Core apps can run on .NET Core or on the full .NET Framework
- Designed to provide an optimized development framework for apps that are deployed to the cloud or run on-premise
- Modular components with minimal overhead retain flexibility while constructing your solutions
Prerequisites
This example assumes you already have an ASP.NET Core app on your machine. If you are new to ASP.NET you can follow a simple tutorial to initialize a project or clone our ASP.NET Docker Sample.
Create a Dockerfile for an ASP.NET Core application
- Create a
Dockerfilein your project folder. - Add the text below to your
Dockerfilefor either Linux or Windows Containers. The tags below are multi-arch meaning they pull either Windows or Linux containers depending on what mode is set in Docker for Windows. Read more on switching containers. - The
Dockerfileassumes that your application is calledaspnetapp. Change theDockerfileto use the DLL file of your project.
FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app # Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore # Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out # Build runtime image
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "aspnetapp.dll"]
To make your build context as small as possible add a .dockerignore file to your project folder and copy the following into it.
bin\
obj\
Build and run the Docker image
- Open a command prompt and navigate to your project folder.
- Use the following commands to build and run your Docker image:
$ docker build -t aspnetapp .
$ docker run -d -p 8080:80 --name myapp aspnetapp
View the web page running from a container
- Go to localhost:8080 to access your app in a web browser.
- If you are using the Nano Windows Container and have not updated to the Windows Creator Update there is a bug affecting how Windows 10 talks to Containers via “NAT” (Network Address Translation). You must hit the IP of the container directly. You can get the IP address of your container with the following steps:
- Run
docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" myapp - Copy the container ip address and paste into your browser. (For example,
172.16.240.197)
- Run
Further reading
Dockerize a .NET Core application的更多相关文章
- Dockerize an ASP.NET Core application
原文:Dockerize an ASP.NET Core application 介绍 本示例演示了如何对ASP.NET Core应用程序进行容器化. 为什么要构建ASP.NET Core? 开源 在 ...
- First ASP.NET Core Application on a Mac Using Visual Studio Code
一直希望可以在mac上直接编写webapp (用C#)现在终于伴随着 core 世界美好了,不需要用pd windows了 nice. Visual studio code 更新1.1版本了 怀着激动 ...
- jenkins publish .net core application to linux server in docker
上一个Demo进行了单独的Jenkins远程部署, 本Demo将使用流行的Jenkins+Git+Docker进行持续部署. 准备Linux服务器 和上一篇Demo一样, 在Azure创建一台Cent ...
- ASP.NET ZERO Core Application 学习笔记
地址:https://www.aspnetzero.com/Documents/Development-Guide-Core 1.恢复数据库 MIGRATOR CONSOLE APPLICATION ...
- Analyzing .net core application with SonarQube Scanner for MSBuild
SonarQube是管理代码质量一个开放平台,省略安装过程,下面介绍下如何使用sonarqube去扫描c# 代码. 前提:下载SonarQube Scanner for MSBuild.https:/ ...
- jenkins publish .net core application to linux server
最近学习Docker与Jenkins, 网上大部分都是关于Jenkins+Git+Docker进行持续远程部署, 我一直在考虑为什么Jenkins和Docker要绑定一块使用, 因为我想单独使用Jen ...
- Ubuntu & Docker & Consul & Fabio & ASP.NET Core 2.0 微服务跨平台实践
相关博文: Ubuntu 简单安装 Docker Mac OS.Ubuntu 安装及使用 Consul Consul 服务注册与服务发现 Fabio 安装和简单使用 阅读目录: Docker 运行 C ...
- Docker & Consul & Fabio & ASP.NET Core 2.0 微服务跨平台实践
相关博文: Ubuntu 简单安装 Docker Mac OS.Ubuntu 安装及使用 Consul Consul 服务注册与服务发现 Fabio 安装和简单使用 阅读目录: Docker 运行 C ...
- NET Core 2.0 微服务跨平台实践
NET Core 2.0 微服务跨平台实践 相关博文: Ubuntu 简单安装 Docker Mac OS.Ubuntu 安装及使用 Consul Consul 服务注册与服务发现 Fabio 安装和 ...
随机推荐
- redis的数据类型命令
存储sortedset: 存储:zadd key score menber1 score menber2 ... 升序排列:zrange key start end [withscores] 降序排列 ...
- antd-design model 数据特点
- Linux基础命令---显示路由表route
route route指令用于显示或者修改IP路由表.它的主要用途是在使用ifconfig(8)程序配置接口后,通过接口设置到特定主机或网络的静态路由.当使用add或del选项时,路由将修改路由表.如 ...
- jQuery图片懒加载插件jquery.lazyload.js使用实例注意事项说明
jQuery图片懒加载插件jquery.lazyload.js使用实例注意事项说明 jquery.lazyload.js是一个用JavaScript编写的jQuery插件.它可以延迟加载长页面中的图片 ...
- 安装ES6及HEAD插件
1.下载相应npm包 es6地址:https://www.elastic.co/downloads/elasticsearch head插件地址:https://github.com/mobz/ela ...
- 自学java第六周的总结
在这个星期里,我主要将以前看过的复习了一遍,并且阅读并做了相关的题. 下个星期继续努力.
- css 初级进阶
摘自:https://www.jianshu.com/p/dcc40ccc9841 CSS中级 Class和ID选择器 CSS初级教程中我们可以使用HTML标签选择器定义样式. 同样你也可以使用Cla ...
- Cookie,Session,正则表达式
一.Cookie和Session基础知识 Cookie:客户端本地存储的键值对 Http访问是不记录状态的,所以要借助session和cookie来保存访问状态 当你在浏览网站的时候,WEB 服务器 ...
- Subversion版本控制系统的安装和操作.
SVN的简单介绍 SVN是Subversoin的简称,是一个开源的版本控制系统 Subversion将文件存放在中心版本库里,这个版本库很像一个普通的文件服务器,不同的是,他可以记录每一次文件和目录的 ...
- Kafka学习笔记之Kafka三款监控工具
0x00 概述 在之前的博客中,介绍了Kafka Web Console这 个监控工具,在生产环境中使用,运行一段时间后,发现该工具会和Kafka生产者.消费者.ZooKeeper建立大量连接,从而导 ...