Prevent reinstalling the entire docker container every time? speed improvement

Docking a Rails application that requires age to recover a container. I tried to ADD as far as possible, but can't think anymore. Any suggestions on how to improve the recovery speed of my docker container? Or general advice on how to improve docker file takes a very long time to recover every time. Also are there any sane ways to check if a directory, for example, exists without throwing an error and failing to complete the build?

FROM ruby:2.2.0
EXPOSE 80
EXPOSE 22
ENV RAILS_ENV production

RUN apt-get update -qq && apt-get install -y build-essential

# --------------------------------------
# GEM PRE-REQ
# --------------------------------------
#RUN apt-get install -y libpq-dev
#RUN apt-get install -y libxml2-dev libxslt1-dev #nokigiri
#RUN apt-get install -y libqt4-webkit libqt4-dev xvfb
RUN cd /tmp && git clone https://github.com/maxmind/geoipupdate && cd geoipupdate && ./bootstrap

# --------------------------------------
# HOME FOLDER
# --------------------------------------
WORKDIR                             /srv/my

ADD . /srv/my
ADD ./Gemfile                       /srv/my/Gemfile
ADD ./Gemfile.lock                  /srv/my/Gemfile.lock

#RUN mkdir                           /srv/my
RUN bundle install --without development test
#RUN bundle install foreman


RUN bundle exec rake assets:precompile --trace


# --------------------------------------
# UNICORN AND NGINX
# --------------------------------------

ADD ./config/_server/unicorn_my /etc/init.d/unicorn_my
RUN chmod 755 /etc/init.d/unicorn_my
RUN update-rc.d unicorn_my defaults
ADD ./config/_server/nginx.conf /etc/nginx/sites-available/default

RUN apt-get install -y nginx
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf
#RUN chown -R www-data:www-data /var/lib/nginx ??
ADD ./config/_server/nginx.conf /etc/nginx/my.conf
ADD ./config/_server/my.conf /etc/nginx/sites-enabled/my.conf
ADD ./config/_server/unicorn.rb /srv/my/config/unicorn.rb
ADD ./config/_server/Procfile /srv/my/Procfile

#RUN service unicorn_my start
#RUN foreman start -f ./Procfile

      

+3


source to share


1 answer


You can improve your build speed:

  • Set all your requirements as early as possible.
  • Combine all apt-get

    / yum

    into one command and then clear the apt / yum cache. This can reduce the size of your image.

Example:

RUN \
  apt-get -y update && \
  apt-get -y install curl build-essential nginx && \
  apt-get clean && \
  rm -rf /var/lib/apt/lists/*

      



  • Put ADD

    / COPY

    as late as possible, as this will invalidate the Docker image cache.
  • Avoid putting a long-term task (eg: apt-get

    upload a large file, etc.) after a ADD

    / COPY

    file or directory that changes frequently.

Docker will take a snapshot for each of your commands. So when you create a new image from the same state (without changing the Dockerfile / file / directory) it should be fast.

Commenting / splitting the Dockerfile to reduce the time apt-get install

might not help you because it will invalidate the docker cache.

+8


source







All Articles