Skip to content
Advertisement

Executing a shell script having docker | docker commands not found

I created a pipeline in Azure with Ubuntu 18.04. My requirement was to run a docker image using bash script and for the same below script was created but on execution I received an error “docker command does not exist and docker: invalid reference format.

test.sh

#!/bin/bash
#
echo "=== docker Images==="
docker images
echo "==== Starut running a jmeter/image ===="
docker run "justb4/jmeter:latest"
echo "==== Finish ===="

Error

Starting: Bash Script scripts/test.sh
==============================================================================
Task         : Bash
Description  : Run a Bash script on macOS, Linux, or Windows
Version      : 3.163.2
Author       : Microsoft Corporation
Help         : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/bash
==============================================================================
Generating script.
Formatted command: bash '/home/vsts/work/1/s/scripts/test.sh' 'justb4/jmeter:latest' mainTest.jmx qa-url 30 1 60 15 2 60 1000
========================== Starting Command Output ===========================
/bin/bash --noprofile --norc /home/vsts/work/_temp/5c641c31-4e55-4ab8-be9e-4cf850432bab.sh
=== docker Images===
docker: 'images
' is not a docker command.
See 'docker --help'
==== Starut running a jmeter/image ====
docker: invalid reference format.
See 'docker run --help'.
==== Finish ====

Finishing: Bash Script scripts/test.sh

To describe in detail, here are the tasks created on az pipeline :-

1. Install a docker

enter image description here

Output of Task1

enter image description here

2. Just for debugging purpose, I added below bash task **with inline commands** to see if docker 

commands work and it worked perfectly fine with no issues. But in task3, when I tried to execute the scripts with the same commands it failed.

enter image description here

Output of Task2

enter image description here

3. Task 3 to execute test.sh script having docker commands

enter image description here

Output of Task3 enter image description here

Advertisement

Answer

The problem was with Windows or DOS-style line endings but while executing in Azure pipeline it did not throw the actual error. Later it was understood that each line was being terminated with a Carriage Return followed by a Line Feed character. If a script file was saved with Windows line endings, Bash sees the file as

#!/bin/bash^M ^M cd “src”^M

having a special character at the end.

Try running dos2unix on the script solved the problem.

http://dos2unix.sourceforge.net/

Or just rewrite the script in your Unix env using vi and test.

Unix uses different line endings so can’t read the file you created on Windows. Hence it is seeing ^M as an illegal character.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement