在Docker文件中运行Shell脚本文件。

huangapple go评论67阅读模式
英文:

Run shell script file inside docker file

问题

我已创建一个Dockerfile
FROM alpine
RUN apk update --no-cache && apk add --no-cache --upgrade bash && apk add curl
COPY test-start.sh ./test-start.sh
RUN chmod +x ./test-start.sh
RUN sh ./test-start.sh
并且我创建了test-start.sh文件
#!/bin/bash
code=$(curl https://someendpoint)

if [ $code == "200" ]
then
  exit 0
else
  exit 1
fi
我想在创建Docker镜像时执行这个Shell脚本,如果得到200响应,则继续构建镜像,否则停止构建镜像并抛出错误。
现在,当我尝试构建Docker镜像时,我得到一个错误
[+] Building 10.9s (10/10) FINISHED
 => [internal] load build definition from Dockerfile.dev                                                           0.1s
 => => transferring dockerfile: 1.18kB                                                                             0.0s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [internal] load metadata for docker.io/library/alpine:latest                                                   1.9s
 => [auth] library/alpine:pull token for registry-1.docker.io                                                      0.0s
 => [internal] load build context                                                                                  0.1s
 => => transferring context: 35B                                                                                   0.0s
 => CACHED [1/5] FROM docker.io/library/alpine@sha256:69665d02cb32192e52e07644d76bc6f25abeb5410edc1c7a81a10ba3f0e  0.0s
 => [2/5] RUN apk update --no-cache && apk add --no-cache --upgrade bash && apk add curl                           6.0s
 => [3/5] COPY test-start.sh ./test-start.sh                                                                       0.2s
 => [4/5] RUN chmod +x ./test-start.sh                                                                             0.7s
 => ERROR [5/5] RUN sh ./test-start.sh                                                                             1.9s
------
 > [5/5] RUN sh ./test-start.sh:
#10 0.790   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
#10 0.790                                  Dload  Upload   Total   Spent    Left  Speed
100     5  100     5    0     0      4      0  0:00:01  0:00:01 --:--:--     4
: not found/test-start.sh: line 3:
#10 1.810 ./test-start.sh: line 9: syntax error: unexpected "fi" (expecting "then")
------
executor failed running [/bin/sh -c sh ./test-start.sh]: exit code: 2
我不明白这个错误是什么,以及如何解决它。
请告诉我,如果我在这里做错了什么,因为我是新手,正在学习如何做这件事。
谢谢!
英文:

I have created a Dockerfile

FROM alpine
RUN apk update --no-cache && apk add --no-cache --upgrade bash && apk add curl
COPY test-start.sh ./test-start.sh
RUN chmod +x ./test-start.sh
RUN sh ./test-start.sh

And I have created the test-start.sh file

#!/bin/bash
code=$(curl https://someendpoint)

if [ $code == "200" ]
then
  exit 0
else
  exit 1
fi

What I want to do is execute this shell script while creating the docker image and if get 200 response then continue building the image else stop building the image and throw error.
Now when I am trying to build the docker image I am getting an error

[+] Building 10.9s (10/10) FINISHED
 => [internal] load build definition from Dockerfile.dev                                                           0.1s
 => => transferring dockerfile: 1.18kB                                                                             0.0s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [internal] load metadata for docker.io/library/alpine:latest                                                   1.9s
 => [auth] library/alpine:pull token for registry-1.docker.io                                                      0.0s
 => [internal] load build context                                                                                  0.1s
 => => transferring context: 35B                                                                                   0.0s
 => CACHED [1/5] FROM docker.io/library/alpine@sha256:69665d02cb32192e52e07644d76bc6f25abeb5410edc1c7a81a10ba3f0e  0.0s
 => [2/5] RUN apk update --no-cache && apk add --no-cache --upgrade bash && apk add curl                           6.0s
 => [3/5] COPY test-start.sh ./test-start.sh                                                                       0.2s
 => [4/5] RUN chmod +x ./test-start.sh                                                                             0.7s
 => ERROR [5/5] RUN sh ./test-start.sh                                                                             1.9s
------
 > [5/5] RUN sh ./test-start.sh:
#10 0.790   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
#10 0.790                                  Dload  Upload   Total   Spent    Left  Speed
100     5  100     5    0     0      4      0  0:00:01  0:00:01 --:--:--     4
: not found/test-start.sh: line 3:
#10 1.810 ./test-start.sh: line 9: syntax error: unexpected "fi" (expecting "then")
------
executor failed running [/bin/sh -c sh ./test-start.sh]: exit code: 2

I don't get what the error is and how it is to be resolved.

Also please let me know if I am doing anything wrong here as I am new to this and learning how this is to be done.

Thank You!

答案1

得分: 1

syntax error: unexpected "fi" (expecting "then") 看起来很奇怪。即使未引用的 $code 也不应导致这种错误。

请检查您的脚本是否包含Windows行尾符号(例如,使用 file test-start.sh)。如果需要的话,将文件转换为Linux行尾符号(例如,使用 dos2unix test-start.sh)。

之后,您可能会遇到另一个错误,例如 [: too many arguments。如果是这种情况,请查看 Cornuz 的评论

变量 code 不包含HTTP状态,而是curl的标准输出,即HTTP正文(例如,如果您正在查询一个网站,则是 <HTML>...</HTML>)。首先,引用您的变量,然后使用 curl -s -o /dev/null -w "%{http_code}" http://... 告诉 curl 仅输出HTTP状态码。

顺便说一下:if [ ... ]; then exit 0; else exit 1; fi 有点多余。您可以只使用 [ ... ]

#!/bin/bash
code=$(curl -s -o /dev/null -w "%{http_code}" http://...)
[[ "$code" = "200" ]]
英文:

syntax error: unexpected "fi" (expecting "then") seems strange. Even the unquoted $code shouldn't result in such an error.

Please check your script for Windows line endings (e.g. using file test-start.sh). If needed, convert your file to Linux line endings (e.g. using dos2unix test-start.sh).

After that, you may get another error, e.g. [: too many arguments. If that is the case, have a look at Cornuz' comment.
The variable code does not contain the http status, but curl's stdout which is the http body (e.g. <HTML>...</HTML> if you are querying a website). First, quote your variable; then tell curl to output only the http status code using curl -s -o /dev/null -w "%{http_code}" http://....

By the way: if [ ... ]; then exit 0; else exit 1; fi is a bit redundant. You can just use the [ ... ].

#!/bin/bash
code=$(curl curl -s -o /dev/null -w "%{http_code}" http://...)
[[ "$code" = "200" ]]

huangapple
  • 本文由 发表于 2023年2月26日 23:17:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定