防止每次重build整个docker集装箱? 提高速度

Docker化一个Rails应用程序,花费时间来重build容器。 我试图添加到最后,但不可能我想更多。 任何关于如何提高docker集装箱重build速度的build议? 或者有关如何改进docker文件的一般build议,每次重build都需要很长的时间。 也有智能的方法来检查,例如一个目录已经存在,而不会抛出一个错误,不能完成构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 

您可以通过以下方式提高构build速度:

  • 尽可能早地安装您的所有需求。
  • 将所有apt-get / yum合并为一个命令,然后清理apt / yumcaching。 它可以减less你的图像大小。

样品:

 RUN \ apt-get -y update && \ apt-get -y install curl build-essential nginx && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* 
  • ADD / COPY尽可能晚,因为它会使Docker镜像caching失效。
  • 避免在经常改变的ADD / COPY文件或目录之后放置长时间运行的任务(例如: apt-get ,下载大文件等)。

Docker为每一个命令取一个“快照”。 所以,当你从相同的状态(没有Dockerfile /文件/目录改变)build立一个新的图像,它应该是快速的。

评论/取消注释Dockerfile为了减lessapt-get install时间可能不会帮助你,因为它会使你的Dockercaching失效。