evgeny
evgeny

Reputation: 1135

Docker-compose and .dockerignore

There is small config of docker-compose with Dockerfiles and .dockerignore files I have in subdirectories. I can't understand why my images after build include everything even I have some items to ignore in .dockerignore. Here is what I have

docker-compose.yml
docker
├── app
│   ├── Dockerfile
│   └── .dockerignore
└── web
    ├── Dockerfile
    ├── nginx.conf
    └── .dockerignore

Example of .dockerignore:

**/.dockerignore
docker-compose.yml
log
**/log
../../log

I just run this as follows:

docker-compose up -d --build --no-cache

None of the above ignored.. What am I doing wrong?

docker-compose version 1.23.2, build 1110ad01

Adding Dockerfile for the app service (see docker-compose). Docker-compose packs two images with ROR app and nginx. For both dockerfiles I have dockerignore. So my understanding is that I can say what exactly I want in each. But it is not. How can I ignore some of the files in package? Or should I put my Dockerfile and dockerignore in different paths since build works only inside the context.. I am confused.

FROM ruby:2.4.1
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs imagemagick
# Configuring main directory
RUN mkdir -p /var/www/example.com
WORKDIR /var/www/example.com
# Setting env up
ENV RAILS_ENV='production'
ENV RACK_ENV='production'

# Adding gems
COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN bundle install --jobs 20 --retry 5 --without development test
# Adding project files
COPY . .

EXPOSE 3000
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]

So I thought that COPY . . checks .dockerignore first.. no?

Upvotes: 42

Views: 64511

Answers (5)

Ben
Ben

Reputation: 62384

.dockerignore is the expected syntax in the project root, however, nowadays Docker will look for an ignore file in the same directory as the Dockerfile, but it must be named accordingly. I don't know about Docker Compose excluding files when mounting, but for building containers your ignore files will be applied if it's structured like this:

docker-compose.yml
docker
├── app
│   ├── Dockerfile
│   └── Dockerfile.dockerignore
└── web
    ├── Dockerfile
    ├── nginx.conf
    └── Dockerfile.dockerignore

Upvotes: 1

theTypan
theTypan

Reputation: 5877

I struggled with this until I came across this article which helped me understand what was happening in my case. If you are binding everything to the container e.g .:/app or .:/usr/src/app as it is common during development for hot reloading, we are completely ignoring .dockerignore since the bind mounts are attached after the image is built, even files that were ignored are now mounted to the container. I removed the binding and files specified in .dockerignore were ignored.

I still bind everything during development but when building production images, I do not

Upvotes: 28

fguillen
fguillen

Reputation: 38772

What worked for me is to put the .dockerignore at the root of my app, where the docker-compose.yml is.

Upvotes: 6

h-rai
h-rai

Reputation: 3964

The .dockerignore needs to be in the root directory/context folder as you specified in the docker-compose.yml

Upvotes: 15

grapes
grapes

Reputation: 8636

You have 2 Dockerfiles, which have their separate contexts. In each context (folder) you have .dockerignore, and that is correct. But .dockerignore does not work outside of build context, that means using ../../ is wrong.

I guess you are supposing these files to apply to docker folder, but in fact they apply to docker/app and docker/web respectively.

The build process is following:

First docker cli packs the context (folder with Dockerfile if not specified explicitly) and sends it to docker daemon. Nothing outside this folder is sent. It is written explicitly for COPY command, which is used to put the context items into container:

The src path must be inside the context of the build; you cannot COPY ../something /something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon.

If there is .dockerignore, the ignored files are excluded before packing.

Upvotes: 32

Related Questions