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. 2017-2018-1 20155228 《信息安全系统设计基础》第六周学习总结&课下作业

    20155228 2017-2018-1 <信息安全系统设计基础>第六周学习总结&课下作业 教材学习内容总结 异常及其种类 异常可以分为四类:中断(interrupt) ,陷阱(t ...

  2. .net中的集合

    集合命令空间: 命令空间:类型逻辑上的分类 System.Collections  非泛型集合 System.Collections.Generic 泛型集合 集合内部存数据,实际上都是存到了数组里. ...

  3. MVC 翻頁的那些坑

    思绪良久,最后还是决定记录一下遇到的坑,毕竟被 ‘折磨’ 了三天,关于分页,这个话题,我一开始时拒绝的,因为真正接触项目的时候,才发现每个框架都会封装一套自己的分页,毕竟相同风格的项目是不常见的,而在 ...

  4. new sh file

    创建新文件 sbash='#!/bin/bash' sauth='# auth: xiluhua' sdate="# date: $(date +%Y-%m-%d)" shead= ...

  5. NGINX的几个应用场景

    NGINX的几个应用场景 两个参考地址: NGINX的百度百科:https://baike.baidu.com/item/nginx/3817705?fr=aladdin NGINX的中文网站:htt ...

  6. Python基础二_操作字符串常用方法、字典、文件读取

    一.字符串常用方法: name.captitalize()                       #字符串首字母大写 name.center(50,'*')                   ...

  7. Spring IOC 和 AOP

    一. IOC 1. 概念及原理 IOC: Inversion of Control(控制反转)是一种设计思想,就是容器控制应用程序所需要的外部资源的创建和管理,然后将其反转给应用程序.对象及其依赖对象 ...

  8. Mysql初级第三天(wangyun)

    1.JDBC简介 1).数据库驱动 2).SUN公司为统一数据库的操作,定义了一套Java操作数据库的规范,称之为JDBC. 3).JDBC全称:Java Database Connectivity( ...

  9. MyEclipse如何修改XML文件默认打开的编辑器

    1.MyEclipse如何修改XML文件默认打开的编辑器 Windows--->Preferences--->General--->Editors--->File Associ ...

  10. ansible中常用模块详解

    ansible中常用的模块详解: file模块 ansible内置的可以查看模块用法的命令如下: [root@docker5 ~]# ansible-doc -s file - name: Sets ...