I would like to create 2 different Docker images from 2 different folders.
My repo has the following structure:
.gitlab-ci.yml
node_app/
src/
package.json
Dockerfile
php_app/
src/
composer.json
Dockerfile
I have the following code in .gitlab-ci.yml
image: docker:stable
services:
- docker:dind
stages:
- build
- test
variables:
DOCKER_HOST: tcp://docker:2375
DOCKER_DRIVER: overlay2
before_script:
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
.build:
stage: build
script:
- |
IMAGE_TAGGED=$CI_REGISTRY_IMAGE/$CONTAINER_NAME:$CI_COMMIT_REF_SLUG
IMAGE_LATEST=$CI_REGISTRY_IMAGE/$CONTAINER_NAME:latest
docker build \
--pull \
--tag $IMAGE_TAGGED \
--tag $IMAGE_LATEST \
--file $CONTAINER_NAME/Dockerfile \
$CONTAINER_NAME
docker push $IMAGE_TAGGED
docker push $IMAGE_LATEST
build node app:
extends: .build
variables:
CONTAINER_NAME: "node_app"
build php app:
extends: .build
variables:
CONTAINER_NAME: "php_app"
.test:
before_script:
- echo 'testing app'
image: $CI_REGISTRY_IMAGE/$CONTAINER_NAME:$CI_COMMIT_REF_SLUG
stage: test
test node app:
extends: .test
variables:
CONTAINER_NAME: "node_app"
script:
- ls -la
# - run test scripts
test php app:
extends: .test
variables:
CONTAINER_NAME: "php_app"
script:
- ls -la
# - run test scripts
The problem is that the docker build context is not the application folder, but the root of the entire repository, thus container the code of all apps.
The result of the test jobs ls -la
command are:
total 40
drwxrwxrwx 7 root root 4096 Aug 6 08:46 .
drwxrwxrwx 4 root root 4096 Aug 6 08:46 ..
drwxrwxrwx 6 root root 4096 Aug 6 08:46 .git
-rw-rw-rw- 1 root root 6 Aug 6 08:46 .gitignore
drwxrwxrwx 2 root root 4096 Aug 6 08:46 .gitlab
-rw-rw-rw- 1 root root 5705 Aug 6 08:46 .gitlab-ci.yml
drwxrwxrwx 3 root root 4096 Aug 6 08:46 .idea
drwxrwxrwx 7 root root 4096 Aug 6 08:46 node_app
drwxrwxrwx 3 root root 4096 Aug 6 08:46 php_app
If I run the exact same docker commands locally, the images build just fine, with the correct build context. However, inside Gitlab Runner the build context doesn't seem to do anything.
I've checked the official Docker build reference if there is anything related to this, but I can't find anything. The only mention of an exception when using build context is:
You cannot specify the build-context directory (myfolder in the examples above) when using BuildKit as builder (DOCKER_BUILDKIT=1). Support for this feature is tracked in buildkit#1684.
but I'm not using that variable, and Gitlab also doesn't set it.
Also, the official Docker reference gives this example :
$ cd /home/me/myapp/some/dir/really/deep
$ docker build -f /home/me/myapp/dockerfiles/debug /home/me/myapp
$ docker build -f ../../../../dockerfiles/debug /home/me/myapp
These two docker build commands do the exact same thing. They both use the contents of the debug file instead of looking for a Dockerfile and will use /home/me/myapp as the root of the build context. Note that debug is in the directory structure of the build context, regardless of how you refer to it on the command line.
Any suggestions are welcome!
I think the solution for this is not related to Docker build at all. Look at your test node app job
. You are running ls
command in the folder which is used for cloning your whole repo. So in the fact you are running ls
inside your image, inside the folder with the content of your repo.
This is the default behaviour of the GitLab CI job. It will clone your repo to the newly created folder and that's why you are seeing this output. Try to disable it with GIT_STRATEGY: none
variable for both of your tests jobs or try to run ls /somewhereelse
.
If you want to run your tests command for the content of your node/php image, it would be better to remove image:
from the job and run it on the default pipeline image as docker run $CI_REGISTRY_IMAGE/$CONTAINER_NAME:latest run test scripts