Adding a project folder other than Dockerfile

I am trying to run a small Rails application in a docker container. I'm getting closer, but I'm struggling with the Docker file.

I added the following command to my Docker file to recursively add all the files to the project folder.

ADD . 

After that I launched

 RUN bundle install --deployment 

However, since my ADD team also adds a Docker file, this means that my image cache breaks every time I edit my Docker file, forcing me to cancel.

According to https://docs.docker.com/reference/builder/#the-dockerignore-file, I can use the .dockerignore file to ignore the Docker file, but this causes the docker build to fail with

 2014/09/17 22:12:46 Dockerfile was excluded by .dockerignore pattern 'Dockerfile' 

How can I easily add a project to my image, but exclude the Dockerfile, so I am not breaking the docker file cache?

+5
source share
2 answers

Your clarification in the comment on @Kuhess's answer, which says that the actual problem is β€œthe invalidation of the docker image cache, resulting in the installation of the package starting again”, is useful to provide you with an answer.

I am using a Dockerfile which looks like this for my rails 4.1. * app. First, ADDing Gemfile *, then starting the package installation and only then adding the rest of the application, the package installation step will be cached if one of the Gemfile * files is not changed.

 FROM ruby:2.1.3 RUN adduser --disabled-password --home=/rails --gecos "" rails RUN gem install bundler --no-ri --no-rdoc RUN gem install -f rake --no-ri --no-rdoc RUN mkdir /myapp WORKDIR /myapp ADD Gemfile /myapp/Gemfile ADD Gemfile.lock /myapp/Gemfile.lock RUN bundle install ADD . /myapp RUN chown -R rails:rails /myapp USER rails EXPOSE 3000 ENV RAILS_ENV production CMD bundle exec rails server -p 3000 

My Dockerfile is located in the root directory of the application, but changes in it (or any other part of the application) do not break the image cache for installing the package, because they are added after they are launched. The only thing that violates it is the changes to the Gemfile *, which is true.

FYI, my .dockerignore file looks like this:

 .git log vendor/bundle 
+3
source

Github has a problem: https://github.com/docker/docker/issues/7969

Your main concern is cache eviction due to ADD and Dockerfile modifications. One of the companions explains that at the moment the .dockerignore file .dockerignore not designed to deal with it:

It -.dockerignore - skips some files when you upload your context from the client to the daemon, and the daemon needs a Docker file to create the image. So the main idea (at the moment) .dockerignore skips large dirs for faster context loading, and not for pure context. full comment on github

I am afraid that the image cache will break when you ADD Dockerfile until these lines are changed.

Perhaps one way to deal with the cache is to place all the files you want to add to a different directory than the Dockerfile :

 . β”œβ”€β”€ Dockerfile └── files_to_add/ 

Then, if you are ADD files_to_add , the Dockerfile will not be included and the cache will not be displayed.

But I do not think this trick is a solution. I also want to have a Dockerfile next to other files at the root of my projects.

+1
source

Source: https://habr.com/ru/post/1202812/


All Articles