Docker Compose 部署 .Net Core
1.安装指令
其中 uname -s
-uname -m
是获取计算机名和操作系统类型:Linux x86
curl -L https://github.com/docker/compose/releases/download/1.25.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose --version
2.Docker Compose 常用命令
docker-compose -h # 查看帮助
docker-compose up # 创建并运行所有容器
docker-compose up -d # 创建并后台运行所有容器
docker-compose -f docker-compose.yml up -d # 指定模板
docker-compose down # 停止并删除容器、网络、卷、镜像。
docker-compose logs # 查看容器输出日志
docker-compose pull # 拉取依赖镜像
dokcer-compose config # 检查配置
dokcer-compose config -q # 检查配置,有问题才有输出
docker-compose restart # 重启服务
docker-compose start # 启动服务
docker-compose stop # 停止服务
3.准备挂在nginxConfig
upstream mysvr {
server 192.168.10.135:8083 weight=1;
server 192.168.10.135:8084 weight=2; #热备
}
4.创建 docker-compose.yml
version: '2'
services:
copmposenetnore1:
container_name: copmposenetnore1
build:
context: .
dockerfile: Dockerfile
ports:
- "8083:80"
copmposenetnore2:
container_name: copmposenetnore2
build:
context: .
dockerfile: Dockerfile
ports:
- "8084:80"
reverse-proxy:
container_name: CopmposeNginx
image: nginx
ports:
- "8087:80"
volumes:
- /NetCoreDocekrCode/ComposeTest/nginxconfig/proxy.conf:/etc/nginx/nginx.conf
执行docker-compose up 报错:
Cannot locate specified Dockerfile: Dockerfile
原因:Docker的目录没有设置对;
重新设置Dockfile 结果:
[root@localhost WebApplication1]# docker-compose up
Building copmposenetnore1
Step 1/16 : FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
---> ac199e8d6dff
Step 2/16 : WORKDIR /app
---> Using cache
---> 564adf31fd1f
Step 3/16 : EXPOSE 80
---> Using cache
---> ab33119d751a
Step 4/16 : FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
---> 82e2a34647f0
Step 5/16 : WORKDIR /src
---> Using cache
---> cd6cecdf6e6a
Step 6/16 : COPY ["WebApplication1/WebApplication1.csproj", "WebApplication1/"]
---> 2251d98694d0
Step 7/16 : RUN dotnet restore "WebApplication1/WebApplication1.csproj"
---> Running in b32b4fd43058
Determining projects to restore...
Restored /src/WebApplication1/WebApplication1.csproj (in 2.77 sec).
Removing intermediate container b32b4fd43058
---> ae18a9b65ca2
Step 8/16 : COPY . .
---> 3d8872c4d0dc
Step 9/16 : WORKDIR "/src/WebApplication1"
---> Running in 287ebfafe22b
Removing intermediate container 287ebfafe22b
---> 162c08b2ddfa
Step 10/16 : RUN dotnet build "WebApplication1.csproj" -c Release -o /app/build
---> Running in 4a01eaaccb57
Microsoft (R) Build Engine version 16.7.2+b60ddb6f4 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
All projects are up-to-date for restore.
WebApplication1 -> /app/build/WebApplication1.dll
WebApplication1 -> /app/build/WebApplication1.Views.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:08.17
Removing intermediate container 4a01eaaccb57
---> a20134bf6091
Step 11/16 : FROM build AS publish
---> a20134bf6091
Step 12/16 : RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish
---> Running in 9f3d71ecc98f
Microsoft (R) Build Engine version 16.7.2+b60ddb6f4 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
All projects are up-to-date for restore.
WebApplication1 -> /src/WebApplication1/bin/Release/netcoreapp3.1/WebApplication1.dll
WebApplication1 -> /src/WebApplication1/bin/Release/netcoreapp3.1/WebApplication1.Views.dll
WebApplication1 -> /app/publish/
Removing intermediate container 9f3d71ecc98f
---> ac9666fc1de8
Step 13/16 : FROM base AS final
---> ab33119d751a
Step 14/16 : WORKDIR /app
---> Using cache
---> ac07bc649e7e
Step 15/16 : COPY --from=publish /app/publish .
---> 9e13fe18f3e5
Step 16/16 : ENTRYPOINT ["dotnet", "WebApplication1.dll"]
---> Running in e6a614e3ee74
Removing intermediate container e6a614e3ee74
---> 1e4dc1291f4d
Successfully built 1e4dc1291f4d
Successfully tagged webapplication1_copmposenetnore1:latest
WARNING: Image for service copmposenetnore1 was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Building copmposenetnore2
Step 1/16 : FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
---> ac199e8d6dff
Step 2/16 : WORKDIR /app
---> Using cache
---> 564adf31fd1f
Step 3/16 : EXPOSE 80
---> Using cache
---> ab33119d751a
Step 4/16 : FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
---> 82e2a34647f0
Step 5/16 : WORKDIR /src
---> Using cache
---> cd6cecdf6e6a
Step 6/16 : COPY ["WebApplication1/WebApplication1.csproj", "WebApplication1/"]
---> Using cache
---> 2251d98694d0
Step 7/16 : RUN dotnet restore "WebApplication1/WebApplication1.csproj"
---> Using cache
---> ae18a9b65ca2
Step 8/16 : COPY . .
---> Using cache
---> 3d8872c4d0dc
Step 9/16 : WORKDIR "/src/WebApplication1"
---> Using cache
---> 162c08b2ddfa
Step 10/16 : RUN dotnet build "WebApplication1.csproj" -c Release -o /app/build
---> Using cache
---> a20134bf6091
Step 11/16 : FROM build AS publish
---> a20134bf6091
Step 12/16 : RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish
---> Using cache
---> ac9666fc1de8
Step 13/16 : FROM base AS final
---> ab33119d751a
Step 14/16 : WORKDIR /app
---> Using cache
---> ac07bc649e7e
Step 15/16 : COPY --from=publish /app/publish .
---> Using cache
---> 9e13fe18f3e5
Step 16/16 : ENTRYPOINT ["dotnet", "WebApplication1.dll"]
---> Using cache
---> 1e4dc1291f4d
Successfully built 1e4dc1291f4d
Successfully tagged webapplication1_copmposenetnore2:latest
WARNING: Image for service copmposenetnore2 was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating copmposenetnore2 ...
Creating copmposenetnore1 ... done
Creating copmposenetnore2 ... done
ERROR: for CopmposeNginx a bytes-like object is required, not 'str'
ERROR: for reverse-proxy a bytes-like object is required, not 'str'
Traceback (most recent call last):
File "site-packages/docker/api/client.py", line 261, in _raise_for_status
File "site-packages/requests/models.py", line 940, in raise_for_status
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: http+docker://localhost/v1.22/containers/c60788c36af2366e252bfc51bd7724ca117d499d14c9ef9531721a50fd00fa35/start
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "compose/service.py", line 625, in start_container
File "compose/container.py", line 241, in start
File "site-packages/docker/utils/decorators.py", line 19, in wrapped
File "site-packages/docker/api/container.py", line 1095, in start
File "site-packages/docker/api/client.py", line 263, in _raise_for_status
File "site-packages/docker/errors.py", line 31, in create_api_error_from_http_exception
docker.errors.APIError: 400 Client Error: Bad Request ("b'OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:545: container init caused: rootfs_linux.go:76: mounting "/NetCoreDocekrCode/ComposeTest/nginxconfig/proxy.conf" to rootfs at "/etc/nginx/nginx.conf" caused: mount through procfd: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type'")
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "bin/docker-compose", line 6, in <module>
File "compose/cli/main.py", line 72, in main
File "compose/cli/main.py", line 128, in perform_command
File "compose/cli/main.py", line 1107, in up
File "compose/cli/main.py", line 1103, in up
File "compose/project.py", line 570, in up
File "compose/parallel.py", line 112, in parallel_execute
File "compose/parallel.py", line 210, in producer
File "compose/project.py", line 556, in do
File "compose/service.py", line 546, in execute_convergence_plan
File "compose/service.py", line 467, in _execute_convergence_create
File "compose/parallel.py", line 112, in parallel_execute
File "compose/parallel.py", line 210, in producer
File "compose/service.py", line 465, in <lambda>
File "compose/service.py", line 457, in create_and_start
File "compose/service.py", line 627, in start_container
TypeError: a bytes-like object is required, not 'str'
[47736] Failed to execute script docker-compose
[root@localhost WebApplication1]#
查看实例容器:
问题:nginx 起不来
ERROR: for CopmposeNginx a bytes-like object is required, not 'str'
ERROR: for reverse-proxy a bytes-like object is required, not 'str'
原因:nginx 的文件名称输错了proxy.conf—>nginx.conf
- /NetCoreDocekrCode/ComposeTest/nginxconfig/proxy.conf:/etc/nginx/nginx.conf
5.最后再优化
上面的yum指令在创建过程中copmposenetnore1、copmposenetnore1 分产生了两个镜像,对一个相同的project 服务。可以公用一个image,也就是说。第一个copmposenetnore1镜像创建后,后面如果有相同的copmposenetnore,可以用第一个copmposenetnore1中创建好的镜像,由于上面的yum中没有指定image,所以系统会自动命名并创建image。
为了公用image,下面针对项目MicroService.ServiceInstance创建的三个容器,这三个容器公用一个image创建(这个image在创建第一个容器时系统自当创建)
在MicroService_Instance1中build镜像,镜像名称为microservicedemo。在MicroService_Instance2、MicroService_Instance3中只要使用microservicedemo镜像,创建实例。并在实例中执行command 起服务
version: '3'
services:
MicroService_Instance1:
container_name: MicroServiceDemo_Instance1
image: microservicedemo
build: ## 创建镜像配置
context: .
dockerfile: Dockerfile
ports:
- "5101:80"
volumes:
- /NetCoreDockerMicroService/AppConfigFile/appsettings5101.json:/app/appsettings.json ##挂载配置文件
MicroService_Instance2:
container_name: MicroServiceDemo_Instance2
image: microservicedemo ## 镜像名称,上面有了就不需要build镜像了
ports:
- "5102:80"
command: ["dotnet","/app/MicroService.ServiceInstance.dll"]
volumes:
- /NetCoreDockerMicroService/AppConfigFile/appsettings5102.json:/app/appsettings.json ##挂载配置文件
MicroService_Instance3:
container_name: MicroServiceDemo_Instance3
image: microservicedemo ## 镜像名称,上面有了就需不要build镜像了
ports:
- "5103:80"
command: ["dotnet","/app/MicroService.ServiceInstance.dll"]
volumes:
- /NetCoreDockerMicroService/AppConfigFile/appsettings5103.json:/app/appsettings.json ##挂载配置文件