简体   繁体   English

如何在 docker 容器内运行已安装的 shell 脚本?

[英]How to run a mounted shell-script inside a docker container?

I'm trying to run a mounted shell-script inside a docker container by following these steps:我正在尝试按照以下步骤在 docker 容器内运行已安装的 shell 脚本:

  1. build stage: build the docker image.构建阶段:构建 docker 镜像。
  2. test stage: mount a directory into the container at runtime with a shell-script file inside.测试阶段:在运行时将一个目录挂载到容器中,其中包含一个 shell-script 文件。
  3. test stage: run the shell-script file from inside the docker.测试阶段:从 docker 内部运行 shell-script 文件。

could someone please explain how this should be done?有人可以解释一下应该如何做吗?

see line: #- ?? HERE I SHOULD RUN THE TEST: /test/check.sh ??见行: #- ?? HERE I SHOULD RUN THE TEST: /test/check.sh ?? #- ?? HERE I SHOULD RUN THE TEST: /test/check.sh ??

services:
    - docker:dind

stages:
    - build
    - test

before_script:
        - docker info

# Build the docker image
build:
    image: docker:latest
    services:
        - docker:dind
    before_script:
        - docker login docker.example.com -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD
    only:
        - master
    script:
        - docker build -t our-docker .
        - docker save our-docker > our-docker.tar
    artifacts:
        paths:
            - our-docker.tar
        expire_in: 1 week

    stage: build


test:
    image: docker:latest
    only:
        - master
    script:
        - docker load < our-docker.tar
        - docker run --volume source="$(pwd)/test",target="/test" our-docker
        #- ?? HERE I SHOULD RUN THE TEST: /test/check.sh ??
    stage: test

First, there was an issue with the docker run command itself:首先, docker run命令本身存在问题:

docker run --volume source="$(pwd)/test",target="/test" our-docker  # buggy

as the syntax to setup a bind-mount is:因为设置绑定安装的语法是:

  • either docker run -v "$PWD/test":"/test" our-docker要么docker run -v "$PWD/test":"/test" our-docker
    ( -v being the short form of --volume ) -v--volume的缩写形式)
  • or docker run --mount type=bind,source="$PWD/test",target="/test" our-dockerdocker run --mount type=bind,source="$PWD/test",target="/test" our-docker

( Note: I replaced above "$(pwd)" with the special shell variable "$PWD" which avoids spinning yet another process.) 注意:我用特殊的 shell 变量"$PWD"替换了上面的"$(pwd)" ,以避免旋转另一个进程。)

Next, you cannot just append the line /test/check.sh after the docker run line because you precisely need to run that command within the context of docker run .接下来,你不能只是追加行/test/check.shdocker run线路,因为你正好需要的范围内运行该命令docker run To this aim, you may want to use the pattern I proposed in this other SO thread: How do I set docker-credential-ecr-login in my PATH before anything else in GitLab CI (which contains more details/remarks about set -e , quotes and shell escaping in the context of that pattern).为此,您可能想使用我在另一个 SO 线程中提出的模式:如何在 GitLab CI 中的任何其他内容之前在我的 PATH 中设置 docker-credential-ecr-login (其中包含有关set -e更多详细信息/备注、引号和外壳在该模式的上下文中转义)。

Wrap-up包起来

More precisely, could you try the following adaptation of your .gitlab-ci.yml ?更准确地说,您可以尝试对.gitlab-ci.yml进行以下改编吗? (I've added some ls commands that should help debugging your configuration): (我添加了一些ls命令来帮助调试您的配置):

services:
  - docker:dind

stages:
  - build
  - test

before_script:
  - docker info

# Build the docker image
build:
  image: docker:latest
  services:
    - docker:dind
  before_script:
    - docker login docker.example.com -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD"
  only:
    - master
  script:
    - docker build -t our-docker .
    - docker save our-docker > our-docker.tar
  artifacts:
    paths:
      - our-docker.tar
    expire_in: 1 week
  stage: build

test:
  image: docker:latest
  # note: use /bin/sh below as this image doesn't provide /bin/bash
  only:
    - master
  script:
    - docker load < our-docker.tar
    - echo "$PWD"
    - ls
    - ls -Rhal test
    - |
      docker run --rm -v "$PWD/test":"/test" our-docker /bin/sh -c "
        set -ex
        ls -Rhal /test
        /test/check.sh
      "
  stage: test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM