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!