Build docker in ASP.NET Core: “no such file or dir

2019-03-09 02:58发布

问题:

I'm getting an error when I build my docker.

Sending build context to Docker daemon 3.584 kB
Step 1/8 : FROM microsoft/aspnetcore:1.1
 ---> 2628aaa7b8cf
Step 2/8 : ENV ASPNETCORE_URLS "http://*:5000"
 ---> Using cache
 ---> 5dffde204fef
Step 3/8 : ENV ASPNETCORE_ENVIRONMENT "Development"
 ---> Using cache
 ---> 3064358bc0eb
Step 4/8 : ARG source
 ---> Using cache
 ---> 4159d0eb78c0
Step 5/8 : WORKDIR /app
 ---> Using cache
 ---> 61a394c84304
Step 6/8 : EXPOSE 5000
 ---> Using cache
 ---> c7c2309f7085
Step 7/8 : COPY ${source:-obj/Docker/publish} .
lstat obj/Docker/publish: no such file or directory

Here's my Dockerfile.

FROM microsoft/aspnetcore:1.1
ENV ASPNETCORE_URLS="http://*:5000"
ENV ASPNETCORE_ENVIRONMENT="Development"
ARG source
WORKDIR /app
EXPOSE 5000
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "ProjectTestApi.dll"

and I run a command:

$ docker build -t my-docker-image-test .

Do you have any ide what is wrong?

回答1:

It's happening because you didn't published your solution. The error message is self-explanatory:

no such file or directory

By default, when you add Docker support to you ASP.NET Core Visual Studio 2017 project, it creates a bunch of docker-compose.yml files, one of them is docker-compose.ci.build.yml which handles the build process. Then, when you build the project through Visual Studio, full docker-compose pipeline is executed.

The contents of docker-compose.ci.build.yml, are similiar to this (it depends on custom config and project names obviously):

version: '2'

services:
  ci-build:
    image: microsoft/aspnetcore-build:1.0-1.1
    volumes:
      - .:/src
    working_dir: /src
    command: /bin/bash -c "dotnet restore ./SolutionName.sln && dotnet publish ./SolutionName.sln -c Release -o ./obj/Docker/publish"

As you can see in the last line, there is a dotnet publish command invoked, which actually builds & publishes your project.

So the solution for your issue, will be just building the project before calling docker:

dotnet publish ./SolutionName.sln -c Release -o ./obj/Docker/publish
docker build -t my-docker-image-test .


回答2:

When you use the option "Add Docker Support" in Visual Studio 2017, VS generates a bunch of docker-compose files and Dockerfiles. As Marcin Zablocki mentioned, one of these is docker-compose.ci.build.yml.

To stay in line with the Docker philosophy, you can use a docker container to publish your project. To publish your project you would take the following steps:

  1. git clone/pull your project & cd into the root dir
  2. Publish your project with docker-compose -f docker-compose.ci.build.yml up
  3. Start your project with docker-compose up

I have encountered situations where the Dockerfile of your main project is giving problems. This happens with FROM microsoft/aspnetcore:1.1, you can change it to FROM microsoft/aspnetcore:1.1.2 or whatever version is needed for your project.