如何使用 Docker-in-Docker 运行 bash 脚本

问题描述 投票:0回答:1

我有一个用于构建 Docker 映像的 bash 脚本,并且我正在尝试设置我的 GitLab CI,以便每当我推送标签时它都会自动构建映像。

这是我的

.gitlab-ci.yml
文件:

stages:
  - build

variables:
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: ''
  FF_NETWORK_PER_BUILD: 1
  CI_DEBUG_SERVICES: "true"
  GIT_SUBMODULE_STRATEGY: recursive

build:
  image: docker:27.1.0
  stage: build
  services:
    - docker:27.1.0-dind
  before_script:
    - echo '>>>>> DEBUG - install bash'
    - apk add bash
  script:
    - echo '>>>>> DEBUG - build the images'
    - source dockerbuild.sh -t latest
  only:
    - tags

我相信

dind
使用
sh
而不是
bash
所以我先去安装了它(我需要bash来运行我的脚本而不是sh)但我遇到了以下问题:

$ apk add bash
fetch https://dl-cdn.alpinelinux.org/alpine/v3.20/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.20/community/x86_64/APKINDEX.tar.gz
(1/2) Installing readline (8.2.10-r0)
(2/2) Installing bash (5.2.26-r0)
Executing bash-5.2.26-r0.post-install
Executing busybox-1.36.1-r29.trigger
OK: 44 MiB in 75 packages
$ echo '>>>>> DEBUG - build the images'
>>>>> DEBUG - build the images
$ source dockerbuild.sh -t latest
/bin/sh: dockerbuild.sh: line 6: syntax error: bad substitution
/bin/sh: dockerbuild.sh: line 51: pushd: not found

错误中提到的行是:

# Line 6
GIT_VERSION=$(git describe --tags --always --dirty)

# Line 51
pushd $LOCATION >/dev/null
bash docker shell gitlab-ci docker-in-docker
1个回答
0
投票

我可以想到一些可能的补救措施

  1. 在脚本开头添加

    #!/path/to/bash

  2. 编写两个脚本,一个执行您需要的操作,另一个使用 bash 调用它

  3. 更改脚本以使用旧的“sh”语法,因此 $(command) 将翻译为 `command`,然后您不需要 bash

© www.soinside.com 2019 - 2024. All rights reserved.