Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSTS docker task failed on '&&' token in docker RUN command

I configuring CI for my ASP.NET Core application in VSTS (visual studio online). I've added "docker-compose build" task to build definition but it fails with errors:


    Step 4/9 : RUN dotnet restore QuizService.sln && dotnet publish QuizService.sln -c Release -o obj/Docker/publish
    ---> Running in 7ea0cf1881d1
    ... rence = 'SilentlyContinue'; dotnet restore QuizService.sln && dotnet  ...
    The token '&&' is not a valid statement separator in this version.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : InvalidEndOfLine
    Service 'quizservice' failed to build: The command 'powershell -Command $ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; dotnet restore QuizService.sln && dotnet publish QuizService.sln -c Release -o obj/Docker/publish' returned a non-zero code: 1
    ##[error]Building quizservice
    ##[error]Service 'quizservice' failed to build: The command 'powershell -Command $ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; dotnet restore QuizService.sln && dotnet publish QuizService.sln -c Release -o obj/Docker/publish' returned a non-zero code: 1
    ##[error]C:\ProgramData\Chocolatey\bin\docker-compose.exe failed with return code: 1

The problem is with my dockerfile on line:

RUN dotnet restore QuizService.sln && dotnet publish QuizService.sln -c Release -o obj/Docker/publish

Somehow docker does not understand '&&' operator: The token '&&' is not a valid statement separator. The exception itself seems to be related to powershell rather than to docker. Poweshell has no '&&' syntax but why it starts using powershell for RUN command here instead of cmd.exe?

It works like a charm when I build locally on my dev machine.

Do somebody faced the same problem?

like image 896
Philipp Bocharov Avatar asked Jan 29 '23 16:01

Philipp Bocharov


2 Answers

The problem was in incorrect build agent type. "Hosted VS2017" build agent failed to build project because it uses docker with windows containers (and powershell as a default shell). But on my dev machine I use docker with linux containers (with /bin/sh as a default shell). Choosing the correct build agent type fixed the problem (because /bin/sh understands '&&').

like image 74
Philipp Bocharov Avatar answered Jan 31 '23 08:01

Philipp Bocharov


You can change the shell by using Shell command:

SHELL ["cmd", "/S"", "/C"]

Or changing it by specifying in Run command directly:

RUN /bin/bash -c 'source $HOME/.bashrc; echo $HOME'
like image 31
starian chen-MSFT Avatar answered Jan 31 '23 07:01

starian chen-MSFT