Docker in .NetCore

刁茂才
2023-12-01

dockerfile:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app/test/app2

#容器端口
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["WebApplication2.csproj", "WebApplication2/"]
RUN dotnet restore "WebApplication2/WebApplication2.csproj"
#COPY . . #使用VS-docker调试时,copy在WORKDIR之前执行
WORKDIR "/src/WebApplication2"
COPY . .
RUN dotnet build "WebApplication2.csproj" -c Release -o /app/test/app2/build

FROM build AS publish
RUN dotnet publish "WebApplication2.csproj" -c Release -o /app/test/app2/publish

FROM base AS final

#定义时区环境变量
ENV TIME_ZONE Asiz/Shanghai
#设置时区
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "${TIME_ZONE}" >/etc/timezone

WORKDIR /app/test/app2
COPY --from=publish /app/test/app2/publish .
#匿名卷
VOLUME ["/app/test/app2/Log","/app/test/app2/certificate"]

#绑定端口
ENV ASPNETCORE_URLS="http://+;https://+;"
# https端口(和外部端口一致)
ENV ASPNETCORE_HTTPS_PORT="8001"
ENV ASPNETCORE_ENVIRONMENT="Release"

#kestrel-ssl证书配置
ENV ASPNETCORE_Kestrel__Certificates__Default__Path="/app/test/app2/certificate/docker.pfx"
ENV ASPNETCORE_Kestrel__Certificates__Default__Password="Michael"
ENV hello=docker_hello
ENV test=docker_test
ENTRYPOINT ["dotnet", "WebApplication2.dll"]

#-v=volumn匿名卷 hostPath:containerPath
#docker run --rm -d -p 5000:80 -p 5001:443 --name app2 
#-e ASPNETCORE\_Kestrel\_\_Certificates\_\_Default\_\_Path=/app/https/certificate/docker.pfx 
#-e ASPNETCORE\_Kestrel\_\_Certificates\_\_Default\_\_Password=Michael 
#-v "D:\Certificate":"/app/https/certificate" img_app2

docker-compose:

version: '3.3'
services:
  web_app2:
    container_name: app2-v1
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - '8001:443'
      - '8000:80'
    volumes:#kestrel 证书执行本地目录(Windows)

      - type: bind
        source: D:/certificate
        target: /app/test/app2/certificate
        read_only: false
        bind:
          propagation: rslave
      - type: bind
        source: D:\App2\log
        target: /app/test/app2/Log
        read_only: true
        bind:
          propagation: rslave
    # State a dependancy on Redis working
    depends_on:
      - redis
    links:
      - redis
    networks:
     - my_network
    # Pass it in as an Environmental Variable
    environment:
      - RedisConnection=redis:6379,password=Michael,allowAdmin=true,connectTimeout=1000,connectRetry=3
      #localhost:6379,password=Michael,allowAdmin=true,connectTimeout=1000,connectRetry=3
  redis:
    container_name: 'myredis-docker'
    image: 'redis'
    restart: always
    ports:
      - 6381:6379
    command: redis-server --requirepass Michael
    networks:
      my_network:
        aliases:
          - redis

# volumes:
  # app2log: D:\App2\log

networks:
  my_network:
 

参考:docker-compose 使用介绍 - Cocowool - 博客园

docker-compose网络设置之networks_Kiloveyousmile的博客-CSDN博客

 类似资料:

相关阅读

相关文章

相关问答