Search code examples
dockergitlab-ci

dockerignore doesn't work even though it's written correctly


My .dockerignore file

.gitlab-ci.yml
CHANGELOG
VERSION
README.md

My Dockerfile

FROM php:7.4-apache

#COPY SOURCE CODE
COPY . /var/www/html/ 

#INSTALL MYSQL EXTENSION
RUN docker-php-ext-install mysqli

My .gitlab-ci.yml on build stage

build image:
  stage: build
  rules:
    - if: $CI_COMMIT_TAG
    - if: $CI_OPEN_MERGE_REQUESTS
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH 
      when: never
    - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
  image: docker:latest
  services:
    - docker:dind
  dependencies:
    - generate tag
  before_script:
    - docker login -u "$DOCKER_USER" -p "$DOCKER_PASSWORD"
  script:
    - docker build --compress -t "$DOCKER_USER"/"$DOCKER_IMAGE_NAME":"$TAG" .
    - docker push "$DOCKER_USER"/"$DOCKER_IMAGE_NAME":"$TAG"

My file check results

$ ls
CHANGELOG
Dockerfile
README.md
VERSION
application
index.php
system
testci.sql
vendor

Why does this happen even when I have followed the guidelines on https://docs.docker.com/engine/reference/builder/#dockerignore-file ? Are there exceptions to certain files?


Solution

  • No there is no exception except lines starting with !. My guess is that there is some mistake between the path of the .dockerignore and the root of the build context. To work in this case everything must be in the same directory. You can troubleshoot it out by adding extra wildcard characters * for example (*/*/CHANGELOG).

    However, it could be simpler and safer to put everything that needs to be included in the build context in a dedicated folder (for example a docker sub-directory). With this setup you do not need .dockerignore (except to ignore the Dockerfile for example) and you will avoid to copy files (added during the life of the project and not properly ignored) unintentionally in the image. The consequences can be bigger image size, unnecessary rebuilds, secret exposure, etc.