环境:
- window10
- centos8.2最小化安装
- Microsoft Visual Studio Enterprise 2019 版本 16.8.3
- docker-ce.x86_64 engine: 20.10.6
- asp.net core 3.1
一、新建一个空的web工程
修改appsettings.json
,手动设定监听的地址为:htttp://*:5000
:
直接发布:
二、部署到docker
在网站的发布目录创建文件Dockerfile-demo
,内容如下:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic
EXPOSE 80
WORKDIR /app
COPY . /app
ENTRYPOINT ["dotnet", "WebApplication1.dll"]
将发布的文件和Dockerfile-demo
一起拷贝到linux的/test
目录下:
执行如下命令,构建本地镜像:docker build -t dockerwebapidemo:testtag . -f Dockerfile-demo
[root@bogon test]# docker build -t dockerwebapidemo:testtag . -f Dockerfile-demo
Sending build context to Docker daemon 318kB
Step 1/6 : FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
3.1: Pulling from dotnet/core/aspnet
f7ec5a41d630: Already exists
de8293384045: Pull complete
05b41bdaf80f: Pull complete
175cf20129bb: Pull complete
43e2de0a30ff: Pull complete
Digest: sha256:b266cef5efbc93575c5f126beb6cfb4ac006d80d1ebf34b131aa8df3d2e59627
Status: Downloaded newer image for mcr.microsoft.com/dotnet/core/aspnet:3.1
---> 13cc26e654d6
Step 2/6 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic
3.1-bionic: Pulling from dotnet/core/sdk
01bf7da0a88c: Pull complete
f3b4a5f15c7a: Pull complete
57ffbe87baa1: Pull complete
05dbc3319a7f: Pull complete
df9876b4a6be: Pull complete
1824876b979d: Pull complete
7fc0ca49032c: Pull complete
47d259c596cf: Pull complete
5b12036e6c81: Pull complete
Digest: sha256:1c614d98594d6e4cef2aed3c5c92a33b10daf413397b642b74f05041551c010e
Status: Downloaded newer image for mcr.microsoft.com/dotnet/core/sdk:3.1-bionic
---> 668993052570
Step 3/6 : EXPOSE 80
---> Running in bbc0437306fa
Removing intermediate container bbc0437306fa
---> 6ef264256376
Step 4/6 : WORKDIR /app
---> Running in 569e18eea022
Removing intermediate container 569e18eea022
---> 4136722d7bd4
Step 5/6 : COPY . /app
---> 8b98796900da
Step 6/6 : ENTRYPOINT ["dotnet", "WebApplication1.dll"]
---> Running in ff7fe791daa4
Removing intermediate container ff7fe791daa4
---> 0e89cf1accaa
Successfully built 0e89cf1accaa
Successfully tagged dockerwebapidemo:testtag
[root@bogon test]#
[root@bogon test]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dockerwebapidemo testtag ac68f57a1a4a 9 seconds ago 636MB
mcr.microsoft.com/dotnet/core/aspnet 3.1 13cc26e654d6 13 hours ago 208MB
mcr.microsoft.com/dotnet/core/sdk 3.1-bionic 668993052570 13 hours ago 635MB
redis 6.0 3038fc9615f1 8 days ago 104MB
[root@bogon test]#
运行镜像dockerwebapidemo
:docker run -e "ASPMETCORE_ENVIRONEMENT=Development" -v /etc/localtime:/etc/localtime -d -p 6565:5000 --name=webdemo dockerwebapidemo:testtag
[root@bogon test]# docker run -e "ASPMETCORE_ENVIRONEMENT=Development" -v /etc/localtime:/etc/localtime -d -p 6565:5000 --name=webdemo dockerwebapidemo:testtag
f144b2420cd475e18ed04cbef7ea54692d717e47a4aa40731ba3938531e439ed
[root@bogon test]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f144b2420cd4 dockerwebapidemo:testtag "dotnet WebApplicati…" 7 seconds ago Up 6 seconds 80/tcp, 0.0.0.0:6565->5000/tcp, :::6565->5000/tcp webdemo
2420c44f4576 redis:6.0 "docker-entrypoint.s…" About an hour ago Up 52 minutes 0.0.0.0:16379->6379/tcp, :::16379->6379/tcp redistest2
[root@bogon test]# curl http://localhost:6565
Hello World![root@bogon test]#
解释上面的配置和命令:
dockerfile文件中的:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic
:表示使用的.net sdk的发行版本,因为sdk 3.1的在连接sqlserver数据库时报错,所以改用sdk 3.1-bionic
。
docker build中的:
-t
:表示构建好的镜像名称。
-f
:表示都贱镜像使用的配置文件。
docker run中的:
-d
:表示后台运行程序,可以改成-it
,修改后,程序将会在前台运行。
-e
:表示指定程序运行的环境,这里指定运行开发环境
-v
:表示将linux主机的目录挂在到docker中区,这里是让docker中的时区和linux主机的保持一致。
-p
:表示对外的端口映射,这里是让docker中程序的5000端口映射到外面6565。
--name
:表示启动的容器名称。