首页 > 解决方案 > 无法在 docker 容器中以非 root 身份运行 EF

问题描述

我有以下泊坞窗文件:

    FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
    WORKDIR /src
    COPY ["myapp.database/src/myapp.database.csproj", "myapp.database/"]
    COPY ["myapp.database/src/NuGet.config", "myapp.database/"]
    RUN dotnet restore "myapp.database/myapp.database.csproj"
    COPY myapp.database/src myapp.database
    WORKDIR /src/myapp.database
    RUN dotnet build "myapp.database.csproj" -c Release -o /app/build

    FROM build AS publish
    RUN dotnet publish "myapp.database.csproj" -c Release -o /app/publish

    FROM build AS final

    RUN groupadd -g 500 dotnetuser && \
        useradd -r -u 500 -g dotnetuser dotnetuser
        
    RUN dotnet tool install --global dotnet-ef --version 3.1.0
    ENV PATH="${PATH}:/root/.dotnet/tools"

    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "myapp.database.dll"]

我创建了一个 uid 为 500 的用户“dotnetuser”。我在 docker-compose 中指定了以下内容:

version: '3.4'
services:
  postgres:
    image: postgres:12.1-alpine
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: myapp
      POSTGRES_PASSWORD: myapp
      POSTGRES_DB: myapp
    volumes:
      - postgresvolume:/var/lib/postgresql/data
    networks:
      - dockercomposedatabase_default
  myapp.database:
    depends_on:
      - postgres
    user: "500"
    build:
      context: ..
      dockerfile: myapp.database/build/Dockerfile
    environment:
      DOTNET_CLI_HOME: "/tmp/DOTNET_CLI_HOME"
    networks:
      - dockercomposedatabase_default
volumes:
  postgresvolume:
   external: false
networks:
  dockercomposedatabase_default:
    external: true

但是,如果我以 root 身份运行容器,我只能从我的容器运行 EF 命令。

如果我以 dotnetuser 身份运行,则会收到以下错误:

Could not execute because the specified command or file was not found.
Possible reasons for this include:
* You misspelled a built-in dotnet command.
* You intended to execute a .NET Core program, but dotnet-ef does not exist.
* You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.

我尝试了各种方法让 dotnetuser 以非 root 身份运行 ef 命令,但没有任何成功:(

即使我将工具安装到 dotnetuser 主路径,我仍然会遇到权限问题。

dotnet ef database update如果我以非 root 身份运行 docker,我该如何运行?

标签: entity-frameworkdocker.net-coredocker-compose

解决方案


事实证明,我想做的事情是不可能的。这是因为即使您发布已编译的二进制文件,EF 也会尝试构建。在不可变图像上,您将无法执行 EF 工具。https://github.com/dotnet/efcore/issues/13339

但是,我在 github 问题上提到的这个博客上找到了一种解决方法:https ://mattmillican.com/blog/ef-core-migrations-on-linux

当然,就我而言,情况有些不同。我更改了 docker 并创建了一个 migration.sh 脚本。

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["myapp.database/src/myapp.database.csproj", "myapp.database/"]
COPY ["myapp.database/src/NuGet.config", "myapp.database/"]
COPY ["myapp.database/src/migration.sh", "myapp.database/"]
RUN dotnet restore "myapp.database/myapp.database.csproj"
COPY myapp.database/src myapp.database
WORKDIR /src/myapp.database
RUN dotnet build "myapp.database.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "myapp.database.csproj" -c Release -o /app/publish

RUN chmod +x /app/publish/migration.sh

FROM build AS final

RUN groupadd -g 500 dotnetuser && \
    useradd -r -u 500 -g dotnetuser dotnetuser

RUN mkdir /tools

RUN dotnet tool install  dotnet-ef --tool-path /tools --version 3.1.0
ENV PATH="${PATH}:/tools:/app"

WORKDIR /app
COPY --chown=dotnetuser:dotnetuser --from=publish /app/publish .

RUN rm -r /src

RUN chown dotnetuser:dotnetuser -R /tools
RUN chown dotnetuser:dotnetuser -R /app

USER dotnetuser
ENTRYPOINT ["migration.sh"]

我使用我的migration.sh脚本来执行迁移,然后执行 mymyapp.database.dll来运行我的装置。

#!/bin/sh
# MIGRATION_NAME is an environmental variable for specifying the which migration to migrate to. It will allow you to migrate forward and back. Don't set the environmental value if you want to migrate to the latest migration.
# This path is important but may change between upgrades!!!
EF_DLL_PATH=/tools/.store/dotnet-ef/3.1.0/dotnet-ef/3.1.0/tools/netcoreapp3.1/any/tools/netcoreapp2.0/any/ef.dll

# standard compiled files for dotnet
DEPS_FILE=myapp.database.deps.json
RUNTIME_CONFIG=myapp.database.runtimeconfig.json

# assembly name
PROJECT_NAME=myapp.database
cd /app

echo "Executing the migration script..."
dotnet exec --depsfile ${DEPS_FILE} --runtimeconfig ${RUNTIME_CONFIG} "${EF_DLL_PATH}" database update ${MIGRATION_NAME} --context MyAppDbContext --assembly ${PROJECT_NAME}.dll --startup-assembly ${PROJECT_NAME}.dll --root-namespace ${PROJECT_NAME} || {
    echo "Could not execute the migration!" 
    exit 1
}

echo "migration to ${MIGRATION} complete!"

echo "Run the fixtures..."
dotnet myapp.database.dll
echo "Fixtures applied!"

希望这可以帮助某人。


推荐阅读