欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

AspNetCore容器化(Docker)部署(一) —— 入门

程序员文章站 2022-09-27 22:38:05
一.docker注册安装 Windows Docker Desktop https://www.docker.com/products/docker-desktop Linux Docker CE https://docs.docker.com/install/linux/docker-ce/ubu ......

一.docker注册安装

windows docker desktop 

linux docker ce 

本文使用windows环境linux container做演示,安装完后切换到linux container,“switch to linux containers...”。

AspNetCore容器化(Docker)部署(一) —— 入门

打开powershell查看docker版本信息

ps c:\users\administrator> docker --version
docker version 18.09.2, build 6247962
ps c:\users\administrator> docker version
client: docker engine - community
 version:           18.09.2
 api version:       1.39
 go version:        go1.10.8
 git commit:        6247962
 built:             sun feb 10 04:12:31 2019
 os/arch:           windows/amd64
 experimental:      false

server: docker engine - community
 engine:
  version:          18.09.2
  api version:      1.39 (minimum version 1.12)
  go version:       go1.10.6
  git commit:       6247962
  built:            sun feb 10 04:13:06 2019
  os/arch:          linux/amd64
  experimental:     false
ps c:\users\administrator>

 

二.创建一个.netcore示例项目“helloworld”

AspNetCore容器化(Docker)部署(一) —— 入门

 

1.添加dockerfile

visual studio下右键点击项目 - 添加 - docker支持 - 选择linux os,vs会自动生成dockerfile文件、构建image、预热container等一系列动作,像极了docker-compose。

生成的dockerfile文件(指令详解:)

from mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim as base
workdir /app
expose 80

from mcr.microsoft.com/dotnet/core/sdk:2.2-stretch as build
workdir /src
copy ["helloworld/helloworld.csproj", "helloworld/"]
run dotnet restore "helloworld/helloworld.csproj"
copy . .
workdir "/src/helloworld"
run dotnet build "helloworld.csproj" -c release -o /app

from build as publish
run dotnet publish "helloworld.csproj" -c release -o /app

from base as final
workdir /app
copy --from=publish /app .
entrypoint ["dotnet", "helloworld.dll"]

 

镜像

ps c:\users\administrator> docker images
repository                             tag                 image id            created             size
helloworld                             dev                 30c03823dd0a        23 minutes ago      260mb
mcr.microsoft.com/dotnet/core/aspnet   2.2-stretch-slim    f6d51449c477        13 days ago         260mb
docker4w/nsenter-dockerd               latest              2f1c802f322f        7 months ago        187kb

 

容器

ps c:\users\administrator> docker ps -a
container id        image               command                  created             status              ports                               names
c697ab8b8b14        helloworld:dev      "tail -f /dev/null"      2 hours ago         up 2 hours          0.0.0.0:60169->80/tcp               elegant_bardeen

 

2.下面抛开ide,手动来生成镜像、容器、启动

首先清空环境,docker rm等下文所用到的命令行文档 

ps c:\users\administrator> docker rm -f c697ab8b8b14
c697ab8b8b14
ps c:\users\administrator> docker rmi 30c03823dd0a f6d51449c477
untagged: helloworld:dev
deleted: sha256:30c03823dd0ae6484caab7f099e4442273663bf50315ac6d3558d7b5bc544e8f
deleted: sha256:0a2fe0c9198ecc65ecfc5a7dcd0823879f764247f949e8c7876257c2b00cbfca
deleted: sha256:2551e1d0a0b9297fb0d8c6781dad394629b567e3ca3b19db6416403565e3aec2
deleted: sha256:872599e9fef81ab624703111a2c4608705c371c3bebe27b0885093d05cdc0022
deleted: sha256:9f7724eb5d7bb41dd1c060cf0212ded5b2a098088fc1e2771dbbe9cbf9a0bf3f
untagged: mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim
untagged: mcr.microsoft.com/dotnet/core/aspnet@sha256:3af73ca8d90dd5b1d01b0499f73fb0115b468502de61881193dbcf8908c86b16
deleted: sha256:f6d51449c47712f4b96ad796f46a2933cf8f85b1fbc85863de5bd43544d3ab97
deleted: sha256:dc3d6d81a75cc325c36c784a625315b45b4cef9857436961f7d1714cdd03ab2f
deleted: sha256:c22fbc137d1d8909a4e6143aa5cd8c24b56de4e158fc980f460d131cf4067501
deleted: sha256:33362d15fdad76b1889cd6a1697e216c311d031dc11d2c96fbcd3f5c51ce39e5
ps c:\users\administrator>

 

构建image

或者直接cd到dockerfile所在目录,然后docker build -t helloworld:v1.0 .

ps c:\users\administrator> docker build -f "c:\users\administrator\source\repos\aspnetcore_docker\helloworld\dockerfile" -t helloworld:v1.0 "c:\users\administrator\source\repos\aspnetcore_docker"
sending build context to docker daemon  4.401mb
step 1/16 : from mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim as base
 ---> f6d51449c477
 ...
successfully built 8a44f8d01233
successfully tagged helloworld:v1.0
security warning: you are building a docker image from windows against a non-windows docker host. all files and directories added to build context will have '-rwxr-xr-x' permissions. it is recommended to double check and reset permissions for sensitive files and directories.
ps c:\users\administrator> docker images
repository                             tag                 image id            created             size
helloworld                             v1.0                8a44f8d01233        13 minutes ago      265mb
mcr.microsoft.com/dotnet/core/sdk      2.2-stretch         e4747ec2aaff        13 days ago         1.74gb
mcr.microsoft.com/dotnet/core/aspnet   2.2-stretch-slim    f6d51449c477        13 days ago         260mb
docker4w/nsenter-dockerd               latest              2f1c802f322f        7 months ago        187kb

 

创建容器 

-d:分离模式启动容器

--restart=always:自动重启

-p:端口映射

ps c:\users\administrator> docker run --name netcore_helloworld -d --restart=always -p 81:80 helloworld:v1.0
fdfc4ca8682d3669a4d4202fbf7d551876caf0e408b4bade76f5fcc50601dea8
ps c:\users\administrator> docker ps -a
container id        image               command                  created             status              ports                               names
fdfc4ca8682d        helloworld:v1.0     "dotnet helloworld.d…"   5 seconds ago       up 3 seconds        0.0.0.0:81->80/tcp                  netcore_helloworld
ps c:\users\administrator>

 

3.部署完成

浏览器打开localhost:81

 

示例代码github地址https://github.com/wwwu/aspnetcore_docker