3
votes

I have a simple out of the box VS2017 web api that I am trying to build a Docker Image for on VSTS and publish the image to Azure container registry. But its not working, error below:

2018-05-21T16:49:45.8481201Z Step 7/17 : COPY WebApi/WebApi.csproj WebApi/
2018-05-21T16:49:45.8503445Z COPY failed: stat /var/lib/docker/tmp/docker-builder936381234/WebApi/WebApi.csproj: no such file or directory
2018-05-21T16:49:45.8644972Z ##[error]COPY failed: stat /var/lib/docker/tmp/docker-builder936381234/WebApi/WebApi.csproj: no such file or directory
2018-05-21T16:49:45.8732546Z ##[error]/usr/local/bin/docker failed with return code: 1

Its a standard VS2017 solution.

Dockerfile

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 63537
EXPOSE 44369

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY WebApi/WebApi.csproj WebApi/
RUN dotnet restore WebApi/WebApi.csproj
COPY . .
WORKDIR /src/WebApi
RUN dotnet build WebApi.csproj -c Release -o /app

FROM build AS publish
RUN dotnet publish WebApi.csproj -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebApi.dll"]

.dockerignore

.dockerignore
.env
.git
.gitignore
.vs
.vscode
docker-compose.yml
docker-compose.*.yml
*/bin
*/obj

docker-compose.yml

version: '3.4'

services:
  webapi:
    image: webapi
    build:
      context: .
      dockerfile: WebApi/Dockerfile

I have tried all the suggested solutions by changing the .dockerignore file, deleting it etc etc.

5
the line COPY WebApi/WebApi.csproj WebApi/fails as WebApi/WebApi.csprojdoes not exist, so check whyuser2915097
Same configuration works locally.Chirdeep Tomar
Be aware that it matters in which folder you place the dockerfile, or respectively, in which folder you call the docker build command. Docker will take a snapshot of this folder (and all subfolders) and forward it to the docker deamon to process. I.e. your paths in the dockerfile must be relative to where you call the docker build command.Efrain
@ChirdeepTomar Do you solve this issue?starian chen-MSFT
@starianchen-MSFT Yes by using the Docker Compose task from the build tasks agents.Chirdeep Tomar

5 Answers

10
votes

It is based on the build context to copy files, by default, the Use Default Build Context option is checked for Docker task, you need to uncheck this option and specify the corresponding path (same level of solution), for example . for root folder.

2
votes

I had the same problem on Visual Studio 2019. This is what solved it for me:

  1. Delete Dockerfile; copy your customizations somewhere if you made any!
  2. Right click the project -> Add -> Docker support, this should generate a new docker file. Copy your custom stuff back in.
  3. Right click the project -> Edit Project File.
  4. Find this line <DockerfileContext>.</DockerfileContext> and remove it.
0
votes

Your context in the compose file should be - context: ./WebApi

As mentioned in other answer Docker build takes directory as context which is the root directory of contents it needs to build the image.

0
votes

I was using the Docker instead of Docker Compose task from the build tasks agents. Change to Docker Compose and point to Docker compose file did the trick.

0
votes

My problem was that I had an unnecessary Dockerfile that was located in a subdirectory.

Since VSTS by default searches for the Dockerfile with **/Dockerfile, this unnecessary Dockerfile was used, resulting in a wrong context. The correct Dockerfile was ignored.