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

一.docker注册安装

Windows Docker Desktop https://www.docker.com/products/docker-desktop

Linux Docker CE https://docs.docker.com/install/linux/docker-ce/ubuntu/

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

打开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”

 

1.添加Dockerfile

Visual Studio下右键点击项目 - 添加 - Docker支持 - 选择Linux OS,VS会自动生成Dockerfile文件、构建image、预热container等一系列动作,像极了docker-compose。

生成的Dockerfile文件(指令详解:https://docs.docker.com/engine/reference/builder/)

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等下文所用到的命令行文档 https://docs.docker.com/engine/reference/commandline/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

 

 

posted @ 2019-06-25 09:33  找不到一个满意的昵称  阅读(2946)  评论(0编辑  收藏  举报