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. loadrunner 更新中......

    一.安装及参考说明 1.51 testing 链接:http://www.51testing.com/zhuanti/LoadRunner.html 2.官网链接:http://learnloadru ...

  2. win7 64位 python启动报错:无法启动此程序,因为计算机中丢失api-ms-win-crt-process-l1-1-0.dll

    安装python3.7,安装成功后,在cmd窗口输入python检查是否安装成功,报错:无法启动此程序,因为计算机中丢失api-ms-win-crt-process-l1-1-0.dll 在网上查询了 ...

  3. 用django统计代码行数+注释行数

    实现统计代码行数: 1.首先在url.py中配置 from django.conf.urls import url from django.contrib import admin from app0 ...

  4. npm 包下载的各种姿势

    最近在写Node程序的时候,突然对 npm install 的-save和-save-dev 这两个参数的使用比较混乱.其实博主在这之前对这两个参数的理解也是模糊的,各种查资料和实践后对它们之间的异同 ...

  5. c# 制作弹窗

    1.右键选择添加,添加windows窗体 2.添加第几个窗体这就是Form几 3.具现化  窗口,然后调用       具现化窗口名+ShowDialog  就可以弹出新的窗口 这个功能需要使用,自己 ...

  6. nfs共享文件搭建

    Linux NFS服务器的安装与配置详解 一.NFS服务简介  NFS是Network  File System(网络文件系统).主要功能是通过网络让不同的服务器之间可以共享文件或者目录.NFS客户端 ...

  7. QT多线程信号和槽参数传递

    写了一个这样的信号 void caculateReady( QList<QString> adds, QList<double> hotV, QList<double&g ...

  8. Modbus库开发笔记之一:实现功能的基本设计(转)

    源: Modbus库开发笔记之一:实现功能的基本设计

  9. Zookeeper注册中心概述

    Zookeeper介绍(配合Dubbox使用) 官方推荐使用zookeeoer注册中心,注册中心负责服务地址的注册和查找,相当于目录服务,提供提供者和消费者只在启动时与注册中心交互,注册中心不转发请求 ...

  10. oracle 9i/10gR2所有版本下载地址

    Oracle 9i Oracle9i Database Release 2 Enterprise/Standard/Personal Edition for Windows NT/2000/XP ht ...