In Dockerfile, dependency files are often first copied (gemfile / package.json, etc.), and then dependencies are installed. Then the entire application is copied.

Why is it not recommended to just copy the whole folder and install the dependencies?

  • First, the system, then the packets, then, in essence, the “static” is poured out. I am not an expert, but, IMHO, quite logical action. Even before the project, I copy all sorts of nginx things, for example. By the way, the question is really interesting, I didn’t even think about it and didn’t try anything else ... - Colibri
  • 2
    Why is it not recommended to just copy the whole folder and install the dependencies? - this question should be asked to someone who does not recommend something there. - aleksandr barakin

1 answer 1

Good day.

I occasionally have to "pack" rails-applications in docker-images and noted some points for myself.

Answering the question:

Why is it not recommended to just copy the whole folder and install the dependencies?

I suppose, because the assembly of the image will not be as fast as it could be.

Suppose that after building a docker image with your application, you need to continue to refine this application and test its operation in the image.

If your Dockerfile has something like:

... COPY . /app/folder RUN bundle install 

then at each update of any file, docker will copy files and install dependencies again. It takes a lot of time.

And if you write like this:

 COPY Gemfile* /app/folder RUN bundle install ... COPY . /app/folder 

then dependencies will be conditionally already “installed” and the image will be assembled faster.

You can still speed up image building by bundling rails application dependencies.

 bundle package 

The gems will be placed in the / vendor / cache directory and in Dockerfile you can then write this:

 COPY Gemfile* /app/folder COPY vendor/cache ./vendor/cache RUN bundle install --local ... COPY . /app/folder 

With frequent modifications of the project, such a configuration, in my opinion, can significantly reduce the time to build images.

  • Never pack anything. If you need to speed up - break into several images. A set of gems does not change so often. Gems must be installed in the Docker environment. When you need to work with different versions of Ruby at the same time no RVM will help. Gem cache works great with Capistrano, but docker doesn't need this. - diproart 1:16 pm