运行 在 gitlab 中作为 Docker 容器工作 CI - Docker 虫洞模式

running job as a Docker Container in gitlab CI - Docker wormhole pattern

我正在尝试 运行 在 Docker 容器内进行测试,以下是我的 gitlab-ci.yml

image: openjdk:11
include:
  - project: 'xxxxxxx/sdlc'
    file: '/sdlc.yml'

services:
  - docker:dind

gradle test:
  stage: test
  image: docker:latest
  script:
    - ls -la
    - docker run -t --rm
      -v /var/run/docker.sock:/var/run/docker.sock
      -v "$(pwd)":"$(pwd)"
      -w "$(pwd)"
      -u 0:0
      openjdk:11 ls -la

在我的脚本中,我使用 'ls -la' 作为我的第一个命令来列出我当前目录中可用的内容,如果当前目录在下一步中作为容器内的卷安装,它应该列出相同的。但我什么也没看到。有人可以指出我做错了什么吗?

$ ls -la
total 1752
drwxrwxrwx    9 root     root          4096 May 16 21:59 .
drwxrwxrwx    4 root     root          4096 May 16 21:59 ..
drwxrwxrwx    6 root     root          4096 May 16 21:59 .git
-rw-rw-rw-    1 root     root           182 May 16 21:59 .gitignore
-rw-rw-rw-    1 root     root           787 May 16 21:59 .gitlab-ci.yml
-rw-rw-rw-    1 root     root          4406 May 16 21:59 README.md
-rw-rw-rw-    1 root     root          5351 May 16 21:59 build.gradle
-rw-rw-rw-    1 root     root          9176 May 16 21:59 checkstyle-config.xml
drwxrwxrwx    2 root     root          4096 May 16 21:59 docker
drwxrwxrwx    4 root     root          4096 May 16 21:59 dto
drwxrwxrwx    3 root     root          4096 May 16 21:59 gradle
-rwxrwxrwx    1 root     root          5296 May 16 21:59 gradlew
-rw-rw-rw-    1 root     root          2260 May 16 21:59 gradlew.bat
drwxrwxrwx    2 root     root          4096 May 16 21:59 project
-rw-r--r--    1 root     root       1612517 May 16 21:59 sdlc_enforcer
drwxr-xr-x    3 root     root          4096 May 16 21:59 sdlc_enforcer_dir
-rw-rw-rw-    1 root     root            48 May 16 21:59 settings.gradle
drwxrwxrwx    4 root     root          4096 May 16 21:59 src
-rw-rw-rw-    1 root     root           771 May 16 21:59 whitesource-fs-agent.config
$ docker run -t --rm -v /var/run/docker.sock:/var/run/docker.sock -v "$(pwd)":"$(pwd)" -w "$(pwd)" -u 0:0 openjdk:11 ls -la
Unable to find image 'openjdk:11' locally
11: Pulling from library/openjdk
c5e155d5a1d1: Already exists
221d80d00ae9: Already exists
4250b3117dca: Already exists
3b7ca19181b2: Already exists
1eadaf4c0dff: Already exists
3541530b8726: Pulling fs layer
b5e4c938e30a: Pulling fs layer
a116edcafe05: Pulling fs layer
adf32feffaff: Pulling fs layer
adf32feffaff: Waiting
a116edcafe05: Verifying Checksum
a116edcafe05: Download complete
3541530b8726: Verifying Checksum
3541530b8726: Download complete
b5e4c938e30a: Verifying Checksum
b5e4c938e30a: Download complete
3541530b8726: Pull complete
b5e4c938e30a: Pull complete
a116edcafe05: Pull complete
adf32feffaff: Verifying Checksum
adf32feffaff: Download complete
adf32feffaff: Pull complete
Digest: sha256:768387c0f1e7ec229bc53bd9a8dabb7b81b84d366cb3954cf00ea8400ecadb01
Status: Downloaded newer image for openjdk:11
total 12
drwxr-xr-x. 2 root root 4096 May 16 19:43 .
drwxr-xr-x. 3 root root 4096 May 16 21:59 ..
Job succeeded

参考资料: https://www.testcontainers.org/supported_docker_environment/continuous_integration/dind_patterns/

尝试使用 java 图像而不是 docker:latest 来完成 gradle test 作业。然后 Gitlab 应该将存储库克隆到 java 容器中,您会看到它们以 ls -la.

列出

虽然目录可能会有所不同,但也许您可以更轻松地使其以这种方式工作...

编辑: 关于您的评论:

如果您将 openjdk:11 指定为图像,则不需要 docker run command anymore。舞台配置将是:

include:
  - project: '****/sdlc'
    file: '/sdlc.yml'

stages: 
  - test

gradle test:
  stage: test
  image: openjdk:11
  script:
    - pwd
    - ./gradlew clean test

pwd 将显示 openjdk 容器内的工作目录。

编辑: 更新了上面的管道配置。