【问题标题】:Executing a shell script having docker | docker commands not found执行具有 docker | 的 shell 脚本找不到 docker 命令
【发布时间】:2020-05-25 07:30:43
【问题描述】:

我使用 Ubuntu 18.04 在 Azure 中创建了一个管道。我的要求是使用 bash 脚本运行 docker 映像,并创建了以下相同的脚本,但在执行时我收到错误“docker 命令不存在且 docker:无效的参考格式。

test.sh

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

错误

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://docs.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

为了详细描述,这里是在 az pipeline 上创建的任务:-

1. Install a docker

Output of Task1

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

命令工作,它工作得非常好,没有任何问题。但是在task3中,当我尝试执行 使用相同命令的脚本失败了。

Task2 的输出

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

Task3 的输出

【问题讨论】:

  • 我复制粘贴,一切都很好。您确定您的 test.sh 文件中的 dockerimages 之间没有任何多余的字符吗?
  • 感谢您对此进行调查并尝试一下。没有空格和其他字符。请您确认一下您是如何执行此脚本的。是在 Ubuntu 映像上还是通过 Azure 管道,与我在步骤中提到的方式相同。
  • 我从 Azure Pipelines 运行脚本,并将 agent Specification 设置为 ubuntu-18.04 ` 步骤:-任务:Bash@3 displayName: 'Bash Script test.sh' 输入:targetType: filePath filePath: 。 /stackoverflow/36/test.sh `
  • 你没有安装docker,不是必须的吗?我的任务与您上面提到的完全相同。仍然完整的 YAML 文件在这里:- 你能找出任何错误吗?池:名称:Azure Pipelines 步骤:-任务:DockerInstaller@0 displayName:'Install Docker 17.09.0-ce' -任务:Bash@3 displayName:'Bash Script scripts/test.sh' 输入:targetType:filePath filePath:。 /scripts/test.sh
  • 此池中的图像预装了一些软件。例如,如果您不安装 docker a,您将使用已安装的版本,这在他的情况下是 Docker version 3.0.11+azure, build eb310fca49568dccd87c6136f774ef6fff2a1b51。但我做得更多。我在安装 Docker version 17.09.0-ce, build afdb6d4 之前和之后运行了 test.sh。在这两种情况下,它都对我有用。

标签: linux azure ubuntu azure-devops ubuntu-18.04


【解决方案1】:

问题在于 Windows 或 DOS 样式的行尾,但在 Azure 管道中执行时,它没有引发实际错误。后来了解到,每行都以回车符结尾,然后是换行符。如果脚本文件以 Windows 行结尾保存,Bash 将文件视为

#!/bin/bash^M ^M cd "src"^M

最后有一个特殊字符。

尝试在脚本上运行 dos2unix 解决了问题。

http://dos2unix.sourceforge.net/

或者只是在你的 Unix 环境中使用 vi 和 test 重写脚本。

Unix 使用不同的行尾,因此无法读取您在 Windows 上创建的文件。因此它将 ^M 视为非法字符。

【讨论】:

  • 所以,额外的字符 :) 很高兴你找到它。
  • @Tarun Narang 感谢您在这里分享您的解决方案,请您接受您的解决方案as the answer?因此,对于遇到相同问题的其他成员轻松找到解决方案将很有帮助。祝你有美好的一天:)
猜你喜欢
  • 2020-06-24
  • 1970-01-01
  • 1970-01-01
  • 2017-08-10
  • 2021-12-29
  • 2018-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多