Docker compose VSTS task failder

2019-08-16 23:11发布

问题:

I'working on Asp.Bet Core 2 app with MSSQL on Linux. I would like to configure CI and CD to Azure. I defined docker-compose task in Visual Studio Team Services, but I'm getting an error.

2017-11-09T20:24:09.4725450Z ##[section]Starting: Create images
2017-11-09T20:24:09.4801610Z ==============================================================================
2017-11-09T20:24:09.4817660Z Task         : Docker Compose
2017-11-09T20:24:09.4835680Z Description  : Build, push or run multi-container Docker applications. Task can be used with Docker or Azure Container registry.
2017-11-09T20:24:09.4852370Z Version      : 0.4.7
2017-11-09T20:24:09.4867100Z Author       : Microsoft Corporation
2017-11-09T20:24:09.4881820Z Help         : [More Information](https://go.microsoft.com/fwlink/?linkid=848006)
2017-11-09T20:24:09.4897520Z ==============================================================================
2017-11-09T20:24:10.5040990Z [command]/usr/local/bin/docker-compose -f /opt/vsts/work/1/s/docker-compose.yml -f /opt/vsts/work/1/s/docker-compose.ci.build.yml -f /home/vsts/agent/.docker-compose.1510259050468.yml -p Travelingowe build
2017-11-09T20:24:10.8628630Z db uses an image, skipping
2017-11-09T20:24:10.8646610Z Building api
2017-11-09T20:24:10.9373040Z Step 1/7 : FROM microsoft/aspnetcore:2.0
2017-11-09T20:24:12.0041790Z 2.0: Pulling from microsoft/aspnetcore
2017-11-09T20:25:03.6328690Z Digest: sha256:e36cb8d1edcd1bfd7aea0412349482a9c1a601089d76d1a294067f5f7f1098a9
2017-11-09T20:25:03.6497680Z Status: Downloaded newer image for microsoft/aspnetcore:2.0
2017-11-09T20:25:03.6524910Z  ---> e0e49def2506
2017-11-09T20:25:03.6551560Z Step 2/7 : MAINTAINER Maciej Skuratowski <maciejskuratowski@gmail.com>
2017-11-09T20:25:03.7496070Z  ---> Running in e7667861d293
2017-11-09T20:25:03.9222120Z  ---> 24ba03484562
2017-11-09T20:25:03.9436840Z Removing intermediate container e7667861d293
2017-11-09T20:25:03.9458600Z Step 3/7 : ARG source
2017-11-09T20:25:04.0562980Z  ---> Running in 7b57f9fc515f
2017-11-09T20:25:04.2292420Z  ---> 4d07188edf18
2017-11-09T20:25:04.2503550Z Removing intermediate container 7b57f9fc515f
2017-11-09T20:25:04.2581270Z Step 4/7 : WORKDIR /app
2017-11-09T20:25:04.5347440Z  ---> 4546b8dc771f
2017-11-09T20:25:04.5527690Z Removing intermediate container 0a595d10e668
2017-11-09T20:25:04.5558170Z Step 5/7 : EXPOSE 80
2017-11-09T20:25:04.6765650Z  ---> Running in aa273cc9ca2c
2017-11-09T20:25:04.8330020Z  ---> 6dd874db617e
2017-11-09T20:25:04.8619290Z Removing intermediate container aa273cc9ca2c
2017-11-09T20:25:04.8650100Z Step 6/7 : COPY ${source:-obj/Docker/publish} .
2017-11-09T20:25:04.8686070Z Service 'api' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder211771018/obj/Docker/publish: no such file or directory
2017-11-09T20:25:04.9064960Z ##[error]db uses an image, skipping
2017-11-09T20:25:04.9127660Z ##[error]Building api
2017-11-09T20:25:04.9175860Z ##[error]Service 'api' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder211771018/obj/Docker/publish: no such file or directory
2017-11-09T20:25:04.9258880Z ##[error]/usr/local/bin/docker-compose failed with return code: 1
2017-11-09T20:25:04.9325830Z ##[section]Finishing: Create images

Here's my docker-compose:

version: '3'

services:
  api:
    image: api
    container_name: api
    build:
      context: ./Api
      dockerfile: Dockerfile
    ports:
      - "8000:80"
    depends_on:
      - db
  db:
    image: "microsoft/mssql-server-linux"
    container_name: mssql
    environment:
        SA_PASSWORD: "testtest3030!"
        ACCEPT_EULA: "Y"
        MSSQL_PID: "Developer"
    ports:
      - "127.0.0.1:8001:1433"

and docker-compose.ci.build.yml file:

version: '3'

services:

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

Also I attached my VSTS docker-compose taks:

Do you have any idea for I'm doing wrong?

回答1:

Aha, I faced the same issue a few days ago. Your issue is inside your DockerFile. For example, let's look at my file:

FROM microsoft/aspnetcore:2.0
ARG source
WORKDIR /app
EXPOSE 80
COPY  ${source:-obj/Docker/publish} .
ENTRYPOINT [ "dotnet","MyWeb.Api.dll" ]

If docker cannot find the path that you specify in your COPY command, it will produce an error:

Service 'api' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder211771018/obj/Docker/publish: no such file or directory

So, basically, what you need to do is to fix your build release path (you need to be sure that files after the build in the right folder).

Also, there is a problem, if you execute:

docker-compose build

It doesn't build the solution. That is why DockerFile cannot find the needed folder. If you execute the following command before your docker-compose, it will work.

dotnet publish ./YourSolution.sln -c Release -o ./obj/Docker/publish


回答2:

Regarding the error, the reason is that there isn’t the obj/Docker/publish folder or file. (check the answer of Leonid)

The docker-compose.ci.build.yml file is for building the project and the docker-compose.yml file is used for running the project, so you need to build the project through docker-compose.ci.build.yml file with Dokcer-compose task, then build/run image with docker-compose.yml file with Docker-compose task.

There is a thread about docker-compose.ci.build.yml, docker-compose.yml and Dockerfile: Asp.net core with linux docker container

Update:

Refer to these steps to deploy project to Azure:

  1. Create a new build definition
  2. Add Docker Compose task (Docker Compose File: **/docker-compose.ci.build.yml; Action: Run a Docker Compose command; Command:run ci-build)
  3. Add Copy Files task (Source Folder: $(System.DefaultWorkingDirectory)/[your project path]/obj/Docker/publish; Contents: **; Target Folder: $(Build.ArtifactStagingDirectory))
  4. Add Publish Build Artifacts task (Path to publish: $(Build.ArtifactStagingDirectory))
  5. Create a new release definitioin
  6. Add Azure App Service Deploy task (Package or folder: $(System.DefaultWorkingDirectory)/[artifact build definition name]/[artifact name]; Uncheck Publish using Web Deploy option)