Ubuntu 上对 ASP.NET Core Mvc进行 Docker 部署

C#

浏览数:71

2019-8-29

1创建项目

创建Asp.net Core项目项目:选择MVC模板,勾选启用Docker


创建Asp.net Core项目


image.png

2修改 Dockerfile文件

FROM microsoft/dotnet:2.1-aspnetcore-runtime
#设置工作目录为 `/app` 文件夹,即容器启动默认的文件夹
WORKDIR /app
#拷贝项目publish文件夹中的所有文件到 docker容器中的publish文件夹中  
COPY . /app
#设置环境变量(必要)
ENV ASPNETCORE_URLS http://0.0.0.0:60000
#设置Docker容器对外暴露60000端口
EXPOSE 60000
ENTRYPOINT ["dotnet", "WebApplication2.dll"]

image.png

修改Dockerfile文件的属性为始终输出

4发布:

image.png
发布生成的文件

5将发布的文件拷贝到Ubunto系统

image.png

CD到发布文件
root@ubuntu:/home/fcj/Desktop/code/DockerPub# cd /home/fcj/Desktop/code/DockerPubMvc
安装microsoft/dotnet :2.1-aspnetcore-runtim镜像之所以安装此镜像,是因为Dockerfile文件指明项目依赖于此镜像
sudo docker pull microsoft/dotnet :2.1-aspnetcore-runtim
生成应用程序的镜像:

root@ubuntu:/home/fcj/Desktop/code/DockerPubMvc# docker build -t mymvc:1.0 .
Sending build context to Docker daemon  4.516MB
Step 1/5 : FROM microsoft/dotnet:2.1-aspnetcore-runtime
 ---> e56d8091e0bf
Step 2/5 : WORKDIR /app
 ---> Using cache
 ---> dfbe020d70cd
Step 3/5 : COPY bin/release/netcoreapp2.1/publish .
COPY failed: stat /var/lib/docker/tmp/docker-builder894661515/bin/release/netcoreapp2.1/publish: no such file or directory
root@ubuntu:/home/fcj/Desktop/code/DockerPubMvc# docker build -t mymvc:1.0 .
Sending build context to Docker daemon  4.516MB
Step 1/5 : FROM microsoft/dotnet:2.1-aspnetcore-runtime
 ---> e56d8091e0bf
Step 2/5 : WORKDIR /app
 ---> Using cache
 ---> dfbe020d70cd
Step 3/5 : COPY . .
 ---> ed0bf98f17fc
Step 4/5 : ENV ASPNETCORE_URLS http://0.0.0.0:8011
 ---> Running in ea7c38255f5d
Removing intermediate container ea7c38255f5d
 ---> 910dac865b8d
Step 5/5 : ENTRYPOINT ["dotnet", "WebApplication2.dll"]
 ---> Running in 6b4f315ea5f3
Removing intermediate container 6b4f315ea5f3
 ---> d105803509b7
Successfully built d105803509b7
Successfully tagged mymvc:1.0

运行:

root@ubuntu:/home/fcj/Desktop/code/DockerPubMvc# docker run --name mymvccontiner -d -p 8072:8011 mymvc:1.0
474ecf2c8d02ce90070b56e0a6a62a124ccd2357ea664d8e15b0c4b48cc8ecc0

查看已运行的容器

root@ubuntu:/home/fcj/Desktop/code/DockerPubMvc# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
474ecf2c8d02        mymvc:1.0           "dotnet WebApplicati…"   38 minutes ago      Up 38 minutes       0.0.0.0:8072->8011/tcp   mymvccontiner
82f572351418        mycore:2.0          "dotnet WebApplicati…"   About an hour ago   Up About an hour    0.0.0.0:8062->80/tcp     mycontiner1
root@ubuntu:/home/fcj/Desktop/code/DockerPubMvc# 

在浏览器查看:


image.png

在局域网其他电脑浏览器查看:
需要知道ubuntu系统的IP
通过ifconfig -a获取

image.png

作者:凌雲木