【问题标题】:unable to run a net core docker container无法运行 net core docker 容器
【发布时间】:2019-10-15 11:44:16
【问题描述】:

从 docker-compose 重构为每个项目的单独 Docker 文件后,我无法运行容器,它只是退出。我同时使用 docker 和 docker-compose,因为我以后会有更多项目。

我的docker文件如下。

docker-compose.yml

version: '3'

services:

  customer:
    image: customer
    container_name: customer
    build:
      context: ./Customer
      dockerfile: Dockerfile

客户/Dockerfile

FROM mcr.microsoft.com/dotnet/core/runtime:2.2

WORKDIR /Customer

EXPOSE 80

COPY ./bin/Release/netcoreapp2.2/ service/

ENTRYPOINT ["dotnet", "service/Customer.dll"]

我之前在 docker-compose 文件中也有这个。如何在Dockerfile 内将6001 映射到80?

ports:
  - 6001:80

尝试 2

FROM mcr.microsoft.com/dotnet/core/sdk:2.2

WORKDIR /Customer

ENV DATABASE_HOST=database
ENV DATABASE_NAME=db
ENV DATABASE_USER=sa
ENV DATABASE_PASSWORD=Password

EXPOSE 80

COPY . .

CMD dotnet build

ENTRYPOINT ["dotnet", "Customer/bin/Release/netcoreapp2.2/Department.dll"]

尝试 3

从主站点复制。

从这里复制https://docs.docker.com/engine/examples/dotnetcore/

FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "aspnetapp.dll"]

您的意思是运行 dotnet SDK 命令吗?请安装 dotnet SDK 来自:https://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409

【问题讨论】:

  • 当您使用 Dockerfile 运行容器时,它是否正在运行?你先验证了吗
  • 日志是空的,没有活动,我检查 docker container ls,什么都没有
  • eb3444bdc367 customer "bash" 9 minutes ago Exited (0) 9 minutes ago
  • 你同意我写 docker 和 docker-compose 文件的方式吗?我也在运行 consul,另一个服务,我看到列出的容器,但是这个 .net 核心失败
  • 我会给你样品,一会儿。

标签: docker .net-core docker-compose


【解决方案1】:

这是一个懒惰的基本 docker 文件。我添加了一些 cmets 和一些有用的构建/调试选项,例如:“RUN ls -al”来列出当前目录。就像一个 linux 虚拟机。

# step 1 - building you app with SDK as base image
FROM microsoft/dotnet:2.2-sdk AS build-env # call the environment build
WORKDIR /build # create a work dir

COPY . . # you don't need copy everything to build your app, but this is for simplisity

RUN ls -al # linux command to list dir content

RUN cd /Customer && dotnet publish -o out # actually building the app and publishing to /out dir

RUN cd /Customer && ls -al # navigate to the folder you copied and list dir

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS runtime # step 2, runtime env (slimmed down container)
WORKDIR /app # create workdir
COPY --from=build-env /<YOUR_BULD_PATH>/out ./ # copy from prev container build output

RUN ls -al # list again

ENTRYPOINT ["dotnet", "Department.dll", "--urls", "http://*:6001"] # example from .NET Core 2.2 webapi with port 6005, this might not be your case

现在要运行 docker-compose,只需像您已经完成的那样指出 docker 文件。但是 docker/docker-compose 现在应该可以正常工作了。当然你需要稍微调整一下 docker 文件,我不知道你的应用程序或文件夹结构。

只是一个提示,如果你想单独运行你的 docker 文件,当你启动它来映射端口时不要忘记 args --> -p 6001:80

【讨论】:

  • 今天早上开始新鲜,现在我一遍又一遍地看了几件事,你在这里创建了两个容器,一个是构建环境,另一个是运行环境,然后你正在复制从构建到运行环境的 dll 文件。哦,所以我们不能做到这一点,我们需要两个容器?
  • 是的,您最终会得到 2 个容器,一个用于创建 .dll:s 的构建容器 - 然后将它们复制到精简的运行时容器中。这段视频介绍了它:youtube.com/watch?v=wGz_cbtCiEA - 要查看您机器上的图像,请执行 docker images 并清除所有中间(构建)容器,执行 docker system prune --force 这将删除所有未标记的图像
  • 最后一件事,我应该在哪里指定环境变量,有什么做法?在Dockerfile 或docker-compose 文件中,从未在生产中工作过,只是在本地播放
  • 我在这个 docker compose 中指定了其他几个容器,或者换句话说,我如何在容器之间共享环境变量,例如它们都接收相同的数据库凭据。
  • @AppDeveloper 在docker-compose 文件中指定环境变量。然后,如果您想在 Kubernetes 中托管您的容器,您可以在那里覆盖这些环境变量。
猜你喜欢
  • 1970-01-01
  • 2021-10-26
  • 2021-03-31
  • 2020-11-27
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-16
相关资源
最近更新 更多