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

  1. Create a Dockerfile in your project folder.
  2. Add the text below to your Dockerfile for 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.
  3. The Dockerfile assumes that your application is called aspnetapp. Change the Dockerfile to 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

  1. Open a command prompt and navigate to your project folder.
  2. 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:
    1. Run docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" myapp
    2. Copy the container ip address and paste into your browser. (For example, 172.16.240.197)

  

Further reading

Dockerize a .NET Core application的更多相关文章

  1. Dockerize an ASP.NET Core application

    原文:Dockerize an ASP.NET Core application 介绍 本示例演示了如何对ASP.NET Core应用程序进行容器化. 为什么要构建ASP.NET Core? 开源 在 ...

  2. First ASP.NET Core Application on a Mac Using Visual Studio Code

    一直希望可以在mac上直接编写webapp (用C#)现在终于伴随着 core 世界美好了,不需要用pd windows了 nice. Visual studio code 更新1.1版本了 怀着激动 ...

  3. jenkins publish .net core application to linux server in docker

    上一个Demo进行了单独的Jenkins远程部署, 本Demo将使用流行的Jenkins+Git+Docker进行持续部署. 准备Linux服务器 和上一篇Demo一样, 在Azure创建一台Cent ...

  4. ASP.NET ZERO Core Application 学习笔记

    地址:https://www.aspnetzero.com/Documents/Development-Guide-Core 1.恢复数据库 MIGRATOR CONSOLE APPLICATION ...

  5. Analyzing .net core application with SonarQube Scanner for MSBuild

    SonarQube是管理代码质量一个开放平台,省略安装过程,下面介绍下如何使用sonarqube去扫描c# 代码. 前提:下载SonarQube Scanner for MSBuild.https:/ ...

  6. jenkins publish .net core application to linux server

    最近学习Docker与Jenkins, 网上大部分都是关于Jenkins+Git+Docker进行持续远程部署, 我一直在考虑为什么Jenkins和Docker要绑定一块使用, 因为我想单独使用Jen ...

  7. Ubuntu & Docker & Consul & Fabio & ASP.NET Core 2.0 微服务跨平台实践

    相关博文: Ubuntu 简单安装 Docker Mac OS.Ubuntu 安装及使用 Consul Consul 服务注册与服务发现 Fabio 安装和简单使用 阅读目录: Docker 运行 C ...

  8. Docker & Consul & Fabio & ASP.NET Core 2.0 微服务跨平台实践

    相关博文: Ubuntu 简单安装 Docker Mac OS.Ubuntu 安装及使用 Consul Consul 服务注册与服务发现 Fabio 安装和简单使用 阅读目录: Docker 运行 C ...

  9. NET Core 2.0 微服务跨平台实践

    NET Core 2.0 微服务跨平台实践 相关博文: Ubuntu 简单安装 Docker Mac OS.Ubuntu 安装及使用 Consul Consul 服务注册与服务发现 Fabio 安装和 ...

随机推荐

  1. cvc-complex-type.3.2.2: 元素 'constructor-arg' 中不允许出现属性 'name'

    将版本号改成 3.0 以上的即可.

  2. Spring源码阅读(八)

    摘要: 本文首先将举例说明如何使用BeanWrapper,然后根据例子中的结果分析BeanWrapper的源码.由于在spring中BeanWrapperImpl是BeanWrapper接口的唯一实现 ...

  3. 《nodejs开发指南》微博实例express4.x版

    之前一直执着于前端开发,最近几天,开始学起了nodejs.作为一名前端开发者,见到这样一门用javascript写的后台自然是很激动的.但是,后台毕竟不同于前端,在学习的过程中,还是会遇到不少问题. ...

  4. 【转】ETL介绍与ETL工具比较

    本文转载自:http://blog.csdn.net/u013412535/article/details/43462537 ETL,是英文 Extract-Transform-Load 的缩写,用来 ...

  5. 【转】阿里出品的ETL工具dataX初体验

    原文链接:https://www.imooc.com/article/15640 来源:慕课网 我的毕设选择了大数据方向的题目.大数据的第一步就是要拿到足够的数据源.现实情况中我们需要的数据源分布在不 ...

  6. HashMap 和 ConcurrentHashMap比较

    基础知识: 1. ConcurrentHashMap: (JDK1.7) segment数组,分段锁:segment 内部是 HashEnty数组,类似HashMap: 统计长度的方法,先不加锁统计两 ...

  7. 怎样从外网访问内网IIS?

    本地安装了一个IIS,只能在局域网内访问,怎样从外网也能访问到本地的IIS呢?本文将介绍具体的实现步骤. 准备工作 安装并启动IIS 默认安装的IIS端口是80. 实现步骤 下载并解压holer软件包 ...

  8. 使用Wisdom RESTClient自动化测试REST API,如何取消对返回的body内容的校验?

    使用Wisdom RESTClient V1.1 自动化测试API,默认是对返回HTTP状态码和body内容进行校验的. 如果您的API返回body内容是变化的,可以通过设置来取消对body内容的校验 ...

  9. 第三节 深入JavaScript

    函数的返回值:(把函数体内的数据传出到函数体外) 什么是函数的返回值:函数执行结果.可以没有返回值 一种函数应该只返回一种类型的值 函数传参:(与函数返回值相反,把外面数据传入函数体内) 可变参(不定 ...

  10. 基于ARM Cortex-M0+ 的Bootloader 参考

    源: 基于ARM Cortex-M0+内核的bootloader程序升级原理及代码解析