Re-organize Dockerfile to improve incremental builds. (#10212)

- Always run apt update before any other apt command. (This fixes
  incremental builds failing if a remote package is updated.)
- Only copy dependency lists before installing dependencies. (This means
  editing code doesn't force all dependencies to be re-downloaded.)
- Delete cache in the same layer that it is created. (Otherwise,
  deleting cache *increases* the size of the image on non-squashed
  builds.)
- Move the installation of some static dependencies to *before* Mastodon
  code is imported to Docker.
This commit is contained in:
Ben Lubar 2019-03-08 09:12:48 -06:00 committed by Eugen Rochko
parent 42e733681a
commit d36fcb54c4

View File

@ -19,7 +19,8 @@ RUN echo "Etc/UTC" > /etc/localtime && \
# Install jemalloc # Install jemalloc
ENV JE_VER="5.1.0" ENV JE_VER="5.1.0"
RUN apt -y install autoconf && \ RUN apt update && \
apt -y install autoconf && \
cd ~ && \ cd ~ && \
wget https://github.com/jemalloc/jemalloc/archive/$JE_VER.tar.gz && \ wget https://github.com/jemalloc/jemalloc/archive/$JE_VER.tar.gz && \
tar xf $JE_VER.tar.gz && \ tar xf $JE_VER.tar.gz && \
@ -33,7 +34,8 @@ RUN apt -y install autoconf && \
ENV RUBY_VER="2.6.1" ENV RUBY_VER="2.6.1"
ENV CPPFLAGS="-I/opt/jemalloc/include" ENV CPPFLAGS="-I/opt/jemalloc/include"
ENV LDFLAGS="-L/opt/jemalloc/lib/" ENV LDFLAGS="-L/opt/jemalloc/lib/"
RUN apt -y install build-essential \ RUN apt update && \
apt -y install build-essential \
bison libyaml-dev libgdbm-dev libreadline-dev \ bison libyaml-dev libgdbm-dev libreadline-dev \
libncurses5-dev libffi-dev zlib1g-dev libssl-dev && \ libncurses5-dev libffi-dev zlib1g-dev libssl-dev && \
cd ~ && \ cd ~ && \
@ -51,13 +53,14 @@ RUN apt -y install build-essential \
ENV PATH="${PATH}:/opt/ruby/bin:/opt/node/bin" ENV PATH="${PATH}:/opt/ruby/bin:/opt/node/bin"
RUN npm install -g yarn && \ RUN npm install -g yarn && \
gem install bundler gem install bundler && \
apt update && \
apt -y install git libicu-dev libidn11-dev \
libpq-dev libprotobuf-dev protobuf-compiler
COPY . /opt/mastodon COPY Gemfile* package.json yarn.lock /opt/mastodon/
RUN apt -y install git libicu-dev libidn11-dev \ RUN cd /opt/mastodon && \
libpq-dev libprotobuf-dev protobuf-compiler && \
cd /opt/mastodon && \
bundle install -j$(nproc) --deployment --without development test && \ bundle install -j$(nproc) --deployment --without development test && \
yarn install --pure-lockfile yarn install --pure-lockfile
@ -83,9 +86,6 @@ RUN apt update && \
useradd -m -u $UID -g $GID -d /opt/mastodon mastodon && \ useradd -m -u $UID -g $GID -d /opt/mastodon mastodon && \
echo "mastodon:`head /dev/urandom | tr -dc A-Za-z0-9 | head -c 24 | mkpasswd -s -m sha-256`" | chpasswd echo "mastodon:`head /dev/urandom | tr -dc A-Za-z0-9 | head -c 24 | mkpasswd -s -m sha-256`" | chpasswd
# Copy over masto source from building and set permissions
COPY --from=build-dep --chown=mastodon:mastodon /opt/mastodon /opt/mastodon
# Install masto runtime deps # Install masto runtime deps
RUN apt -y --no-install-recommends install \ RUN apt -y --no-install-recommends install \
libssl1.1 libpq5 imagemagick ffmpeg \ libssl1.1 libpq5 imagemagick ffmpeg \
@ -93,11 +93,9 @@ RUN apt -y --no-install-recommends install \
file ca-certificates tzdata libreadline7 && \ file ca-certificates tzdata libreadline7 && \
apt -y install gcc && \ apt -y install gcc && \
ln -s /opt/mastodon /mastodon && \ ln -s /opt/mastodon /mastodon && \
gem install bundler gem install bundler && \
rm -rf /var/cache && \
# Clean up more dirs rm -rf /var/lib/apt
RUN rm -rf /var/cache && \
rm -rf /var/apt
# Add tini # Add tini
ENV TINI_VERSION="0.18.0" ENV TINI_VERSION="0.18.0"
@ -106,6 +104,10 @@ ADD https://github.com/krallin/tini/releases/download/v${TINI_VERSION}/tini /tin
RUN echo "$TINI_SUM tini" | sha256sum -c - RUN echo "$TINI_SUM tini" | sha256sum -c -
RUN chmod +x /tini RUN chmod +x /tini
# Copy over masto source, and dependencies from building, and set permissions
COPY --chown=mastodon:mastodon . /opt/mastodon
COPY --from=build-dep --chown=mastodon:mastodon /opt/mastodon /opt/mastodon
# Run masto services in prod mode # Run masto services in prod mode
ENV RAILS_ENV="production" ENV RAILS_ENV="production"
ENV NODE_ENV="production" ENV NODE_ENV="production"