一.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/)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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 c697ab8b8b14c697ab8b8b14
PS C:\Users\Administrator> docker rmi 30c03823dd0a f6d51449c477Untagged: helloworld:devDeleted: sha256:30c03823dd0ae6484caab7f099e4442273663bf50315ac6d3558d7b5bc544e8fDeleted: sha256:0a2fe0c9198ecc65ecfc5a7dcd0823879f764247f949e8c7876257c2b00cbfcaDeleted: sha256:2551e1d0a0b9297fb0d8c6781dad394629b567e3ca3b19db6416403565e3aec2Deleted: sha256:872599e9fef81ab624703111a2c4608705c371c3bebe27b0885093d05cdc0022Deleted: sha256:9f7724eb5d7bb41dd1c060cf0212ded5b2a098088fc1e2771dbbe9cbf9a0bf3fUntagged: mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slimUntagged: mcr.microsoft.com/dotnet/core/aspnet@sha256:3af73ca8d90dd5b1d01b0499f73fb0115b468502de61881193dbcf8908c86b16Deleted: sha256:f6d51449c47712f4b96ad796f46a2933cf8f85b1fbc85863de5bd43544d3ab97Deleted: sha256:dc3d6d81a75cc325c36c784a625315b45b4cef9857436961f7d1714cdd03ab2fDeleted: sha256:c22fbc137d1d8909a4e6143aa5cd8c24b56de4e158fc980f460d131cf4067501Deleted: 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.401MBStep 1/16 : FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base ---> f6d51449c477 ...Successfully built 8a44f8d01233Successfully tagged helloworld:v1.0SECURITY 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 imagesREPOSITORY TAG IMAGE ID CREATED SIZEhelloworld v1.0 8a44f8d01233 13 minutes ago 265MBmcr.microsoft.com/dotnet/core/sdk 2.2-stretch e4747ec2aaff 13 days ago 1.74GBmcr.microsoft.com/dotnet/core/aspnet 2.2-stretch-slim f6d51449c477 13 days ago 260MBdocker4w/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.0fdfc4ca8682d3669a4d4202fbf7d551876caf0e408b4bade76f5fcc50601dea8PS C:\Users\Administrator> docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESfdfc4ca8682d helloworld:v1.0 "dotnet HelloWorld.d…" 5 seconds ago Up 3 seconds 0.0.0.0:81->80/tcp netcore_helloworldPS C:\Users\Administrator>
3.部署完成
浏览器打开localhost:81
示例代码Github地址:https://github.com/wwwu/AspNetCore_Docker
- AspNetCore容器化(Docker)部署(一) —— 入门
- AspNetCore容器化(Docker)部署(二) —— 多容器通信
- AspNetCore容器化(Docker)部署(三) —— Docker Compose容器编排
- AspNetCore容器化(Docker)部署(四) —— Jenkins自动化部署
---------------------
作者:找不到一个满意的昵称
来源:CNBLOGS
原文:https://www.cnblogs.com/wu_u/p/10840593.html
版权声明:本文为作者原创文章,转载请附上博文链接!
内容解析By:CSDN,CNBLOG博客文章一键转载插件 标签:Administrator,Users,AspNetCore,dotnet,docker,com,Docker,入门 From: https://www.cnblogs.com/admans/p/18145215