利用docker容器运行.net core webapi
docker常用命令
- docker info|查询docker基本信息
- docker images | 查看所有镜像
- docker ps | 查询所有容器
- docker rmi | Remove one or more images
- docker rm | Remove one or more containers
- docker build | Build an image from a Dockerfile
- docker run | Run a command in a new container
- docker stop | Stop one or more running containers
- docker start | Start one or more stopped containers
- docker pull | Pull an image or a repository from a registry
cmd创建项目
安装.net core sdk
查询是否安装成功
C:\Users\jiangyi\myproj>dotnet -version
Unknown option: -version
.NET Core SDK (3.0.100)
新建项目
C:\Users\jiangyi>dotnet new webapi -n myproj
The template "ASP.NET Core Web API" was created successfully.
Processing post-creation actions...
Running ‘dotnet restore‘ on myproj\myproj.csproj...
C:\Users\jiangyi\myproj\myproj.csproj 的还原在 88.66 ms 内完成。
Restore succeeded.
生成项目
cd到项目目录:
C:\Users\jiangyi>cd myproj
编译生成项目:
C:\Users\jiangyi\myproj>dotnet restore
C:\Users\jiangyi\myproj\myproj.csproj 的还原在 30.56 ms 内完成。
在本地运行
C:\Users\jiangyi\myproj>dotnet run
info: Microsoft.Hosting.Lifetime[0]
Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
可以通过浏览器访问https://localhost:5001
创建镜像
ps:myprojimage为你要输出的镜像名称,后面有‘.‘注意
C:\Users\jiangyi\myproj>docker build -t myprojimage .
...
Build succeeded. 0 Warning(s) 0 Error(s)
Time Elapsed 00:00:03.15
发布项目
ps:运行镜像,将docker的80端口发布出去,在外部访问使用8080端口
C:\Users\jiangyi\myproj>docker run -p 8080:80 myprojimage
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://[::]:80
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.