Browse Source

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.
Ben Lubar 1 month ago
parent
commit
d36fcb54c4
1 changed files with 17 additions and 15 deletions
  1. 17
    15
      Dockerfile

+ 17
- 15
Dockerfile View File

@@ -19,7 +19,8 @@ RUN	echo "Etc/UTC" > /etc/localtime && \
19 19
 
20 20
 # Install jemalloc
21 21
 ENV JE_VER="5.1.0"
22
-RUN apt -y install autoconf && \
22
+RUN apt update && \
23
+	apt -y install autoconf && \
23 24
 	cd ~ && \
24 25
 	wget https://github.com/jemalloc/jemalloc/archive/$JE_VER.tar.gz && \
25 26
 	tar xf $JE_VER.tar.gz && \
@@ -33,7 +34,8 @@ RUN apt -y install autoconf && \
33 34
 ENV RUBY_VER="2.6.1"
34 35
 ENV CPPFLAGS="-I/opt/jemalloc/include"
35 36
 ENV LDFLAGS="-L/opt/jemalloc/lib/"
36
-RUN apt -y install build-essential \
37
+RUN apt update && \
38
+	apt -y install build-essential \
37 39
 		bison libyaml-dev libgdbm-dev libreadline-dev \
38 40
 		libncurses5-dev libffi-dev zlib1g-dev libssl-dev && \
39 41
 	cd ~ && \
@@ -51,13 +53,14 @@ RUN apt -y install build-essential \
51 53
 ENV PATH="${PATH}:/opt/ruby/bin:/opt/node/bin"
52 54
 
53 55
 RUN npm install -g yarn && \
54
-	gem install bundler
56
+	gem install bundler && \
57
+	apt update && \
58
+	apt -y install git libicu-dev libidn11-dev \
59
+	libpq-dev libprotobuf-dev protobuf-compiler
55 60
 
56
-COPY . /opt/mastodon
61
+COPY Gemfile* package.json yarn.lock /opt/mastodon/
57 62
 
58
-RUN apt -y install git libicu-dev libidn11-dev \
59
-	libpq-dev libprotobuf-dev protobuf-compiler && \
60
-	cd /opt/mastodon && \
63
+RUN cd /opt/mastodon && \
61 64
 	bundle install -j$(nproc) --deployment --without development test && \
62 65
 	yarn install --pure-lockfile
63 66
 
@@ -83,9 +86,6 @@ RUN apt update && \
83 86
 	useradd -m -u $UID -g $GID -d /opt/mastodon mastodon && \
84 87
 	echo "mastodon:`head /dev/urandom | tr -dc A-Za-z0-9 | head -c 24 | mkpasswd -s -m sha-256`" | chpasswd
85 88
 
86
-# Copy over masto source from building and set permissions
87
-COPY --from=build-dep --chown=mastodon:mastodon /opt/mastodon /opt/mastodon
88
-
89 89
 # Install masto runtime deps
90 90
 RUN apt -y --no-install-recommends install \
91 91
 	  libssl1.1 libpq5 imagemagick ffmpeg \
@@ -93,11 +93,9 @@ RUN apt -y --no-install-recommends install \
93 93
 	  file ca-certificates tzdata libreadline7 && \
94 94
 	apt -y install gcc && \
95 95
 	ln -s /opt/mastodon /mastodon && \
96
-	gem install bundler
97
-
98
-# Clean up more dirs
99
-RUN rm -rf /var/cache && \
100
-	rm -rf /var/apt
96
+	gem install bundler && \
97
+	rm -rf /var/cache && \
98
+	rm -rf /var/lib/apt
101 99
 
102 100
 # Add tini
103 101
 ENV TINI_VERSION="0.18.0"
@@ -106,6 +104,10 @@ ADD https://github.com/krallin/tini/releases/download/v${TINI_VERSION}/tini /tin
106 104
 RUN echo "$TINI_SUM tini" | sha256sum -c -
107 105
 RUN chmod +x /tini
108 106
 
107
+# Copy over masto source, and dependencies from building, and set permissions
108
+COPY --chown=mastodon:mastodon . /opt/mastodon
109
+COPY --from=build-dep --chown=mastodon:mastodon /opt/mastodon /opt/mastodon
110
+
109 111
 # Run masto services in prod mode
110 112
 ENV RAILS_ENV="production"
111 113
 ENV NODE_ENV="production"

Loading…
Cancel
Save