0

I am trying to run tests inside a Docker container and the following is my 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

In my script, I am using 'ls -la' as my first command to list what is available in my current directory and if the current directory is mounted as a volume inside the container in the next step, it should list the same. But I am seeing nothing. Can someone point what I am doing wrong?

$ 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

References: https://www.testcontainers.org/supported_docker_environment/continuous_integration/dind_patterns/

chom
  • 651
  • 3
  • 9
  • 23
  • possible duplicate: [Docker in Docker cannot mount volume](https://stackoverflow.com/questions/31381322/docker-in-docker-cannot-mount-volume) – matanper May 19 '19 at 12:12
  • please notice the next note in your reference: The 'local' source code directory must be volume mounted at the same path inside the container that Testcontainers runs in, so that Testcontainers is able to set up the correct volume mounts for the containers it spawns. – matanper May 19 '19 at 12:51

1 Answers1

0

Try using the java image instead of docker:latest for the gradle test job. Gitlab should then clone the repository into the java container and you'd see them listed with ls -la.

The dir would likely be different though, but maybe you can easier make it work that way...

Edit: Regarding your comment:

If you specify the openjdk:11 as image you don't need the docker run command anymore. The stage config would then be:

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

stages: 
  - test

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

pwd will show the workdir within the openjdk container.

Edit: Updated the pipeline config above.

dustin.schultz
  • 13,076
  • 7
  • 54
  • 63
rflume
  • 848
  • 5
  • 10
  • Used `openjdk:11` and got the following error: `$ 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 /bin/bash: line 104: docker: command not found ERROR: Job failed: command terminated with exit code 1` – chom May 19 '19 at 19:16
  • I need to run the tests inside a docker. So the command would become `docker run -t --rm -v /var/run/docker.sock:/var/run/docker.sock -v "$(pwd)":"$(pwd)" -w "$(pwd)" -u 0:0 openjdk:11 ./gradlew clean test`. So need to use `docker` to mount my current directory to run tests. – chom May 20 '19 at 00:31
  • Have you tried adding `- ./gradlew clean test` to the stage's `script`? It *should* work because GitLab clones the repository into the container and changes to workdir to that directory containing all files. A volume mount is therefore obsolete and the command is run directly in the openjdk container which is used to ececute the stage's script. – rflume May 20 '19 at 09:17
  • I tried and got this error: `$ docker run -t --rm -v /var/run/docker.sock:/var/run/docker.sock -v "$(pwd)":"$(pwd)" -w "$(pwd)" -u 0:0 openjdk:11 ./gradlew clean test /bin/bash: line 104: docker: command not found ERROR: Job failed: command terminated with exit code 1`. Do you think docker:dind is not working and hence I am getting this error? – chom May 20 '19 at 15:28
  • I don't think you'd need the `docker:dind` service or `docker:latest` image at all. check my updated answer, I updated the pipeline config. I can't test it unfortunately, but that should be sufficient to run the test stage – rflume May 20 '19 at 18:09
  • Yes, that solved that issue. Thanks a lot for your help! I ran into another issue -https://stackoverflow.com/questions/56551917/how-fix-mounting-issue-while-using-docker-compose-yml-on-test-containers. Please check if you can figure out what is causing that issue. – chom Jun 11 '19 at 22:17
  • https://stackoverflow.com/questions/56674613/reading-a-file-from-resources-not-working-in-gitlab-ci/56674956#56674956 – chom Jun 19 '19 at 21:01