Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prevent rebuilding whole docker container everytime? improving speed

Dockerizing a Rails application taking ages to rebuild the container. I tried to ADD as far at the end but not possible I think more. Any suggestions on how to improve the rebuild speed of my docker container? Or general suggestions on how to improve the docker file, it takes very long to rebuild every time. Also are there smart ways to check if for example a directory already exist without throwing an error and not be able 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
like image 830
Rubytastic Avatar asked Jul 26 '15 01:07

Rubytastic


1 Answers

You can improve your build speed by:

  • Install all of your requirement as early as possible.
  • Combine all apt-get/yum into a single command, after that clean up the apt/yum cache. It can decrease your image size.

Sample:

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, because it will invalidate the Docker image cache.
  • Avoid put long-running task (e.g: apt-get, download large file, etc.) after ADD/COPY file or directory that is often changed.

Docker take a "snapshot" for each your command. So, when you build a new image from same state (no Dockerfile/file/directory change), it should be fast.

Comment/uncomment Dockerfile in order to reduce apt-get install time might not help you, because it will invalidate your Docker cache.

like image 113
Edward Samuel Avatar answered Sep 30 '22 19:09

Edward Samuel