Browse code

Updates the Dockerfile to use multi-stage

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Brian Goff authored on 2017/09/30 06:09:14
Showing 1 changed files
... ...
@@ -32,67 +32,13 @@
32 32
 # the case. Therefore, you don't have to disable it anymore.
33 33
 #
34 34
 
35
-FROM debian:stretch
36
-
35
+FROM buildpack-deps:stretch AS base
37 36
 # allow replacing httpredir or deb mirror
38 37
 ARG APT_MIRROR=deb.debian.org
39 38
 RUN sed -ri "s/(httpredir|deb).debian.org/$APT_MIRROR/g" /etc/apt/sources.list
40 39
 
41
-# Packaged dependencies
42
-RUN apt-get update && apt-get install -y \
43
-	apparmor \
44
-	apt-utils \
45
-	aufs-tools \
46
-	automake \
47
-	bash-completion \
48
-	binutils-mingw-w64 \
49
-	bsdmainutils \
50
-	btrfs-tools \
51
-	build-essential \
52
-	cmake \
53
-	createrepo \
54
-	curl \
55
-	dpkg-sig \
56
-	gcc-mingw-w64 \
57
-	git \
58
-	iptables \
59
-	jq \
60
-	less \
61
-	libapparmor-dev \
62
-	libcap-dev \
63
-	libdevmapper-dev \
64
-	libnet-dev \
65
-	libnl-3-dev \
66
-	libprotobuf-c0-dev \
67
-	libprotobuf-dev \
68
-	libseccomp-dev \
69
-	libsystemd-dev \
70
-	libtool \
71
-	libudev-dev \
72
-	mercurial \
73
-	net-tools \
74
-	pigz \
75
-	pkg-config \
76
-	protobuf-compiler \
77
-	protobuf-c-compiler \
78
-	python-backports.ssl-match-hostname \
79
-	python-dev \
80
-	python-mock \
81
-	python-pip \
82
-	python-requests \
83
-	python-setuptools \
84
-	python-websocket \
85
-	python-wheel \
86
-	tar \
87
-	thin-provisioning-tools \
88
-	vim \
89
-	vim-common \
90
-	xfsprogs \
91
-	zip \
92
-	--no-install-recommends \
93
-	&& pip install awscli==1.10.15
94 40
 
95
-# Install Go
41
+FROM base AS golang
96 42
 # IMPORTANT: If the version of Go is updated, the Windows to Linux CI machines
97 43
 #            will need updating, to avoid errors. Ping #docker-maintainers on IRC
98 44
 #            with a heads-up.
... ...
@@ -100,18 +46,30 @@ RUN apt-get update && apt-get install -y \
100 100
 ENV GO_VERSION 1.9.4
101 101
 RUN curl -fsSL "https://golang.org/dl/go${GO_VERSION}.linux-amd64.tar.gz" \
102 102
 	| tar -xzC /usr/local
103
+ENV PATH=/usr/local/go/bin:/go/bin:$PATH GOPATH=/go
103 104
 
104
-ENV PATH /go/bin:/usr/local/go/bin:$PATH
105
-ENV GOPATH /go
106 105
 
106
+FROM base AS criu
107 107
 # Install CRIU for checkpoint/restore support
108 108
 ENV CRIU_VERSION 3.6
109
-RUN mkdir -p /usr/src/criu \
109
+# Install dependancy packages specific to criu
110
+RUN apt-get update && apt-get install -y \
111
+	libnet-dev \
112
+	libprotobuf-c0-dev \
113
+	libprotobuf-dev \
114
+	libnl-3-dev \
115
+	libcap-dev \
116
+	protobuf-compiler \
117
+	protobuf-c-compiler \
118
+	python-protobuf \
119
+	&& mkdir -p /usr/src/criu \
110 120
 	&& curl -sSL https://github.com/checkpoint-restore/criu/archive/v${CRIU_VERSION}.tar.gz | tar -C /usr/src/criu/ -xz --strip-components=1 \
111 121
 	&& cd /usr/src/criu \
112 122
 	&& make \
113
-	&& make install-criu
123
+	&& make PREFIX=/opt/criu install-criu
114 124
 
125
+
126
+FROM golang AS registry
115 127
 # Install two versions of the registry. The first is an older version that
116 128
 # only supports schema1 manifests. The second is a newer version that supports
117 129
 # both. This allows integration-cli tests to cover push/pull with both schema1
... ...
@@ -129,6 +87,9 @@ RUN set -x \
129 129
 		go build -buildmode=pie -o /usr/local/bin/registry-v2-schema1 github.com/docker/distribution/cmd/registry \
130 130
 	&& rm -rf "$GOPATH"
131 131
 
132
+
133
+
134
+FROM golang AS notary
132 135
 # Install notary and notary-server
133 136
 ENV NOTARY_VERSION v0.5.0
134 137
 RUN set -x \
... ...
@@ -141,18 +102,18 @@ RUN set -x \
141 141
 		go build -buildmode=pie -o /usr/local/bin/notary github.com/docker/notary/cmd/notary \
142 142
 	&& rm -rf "$GOPATH"
143 143
 
144
+
145
+
146
+FROM base AS docker-py
144 147
 # Get the "docker-py" source so we can run their integration tests
145 148
 ENV DOCKER_PY_COMMIT 8b246db271a85d6541dc458838627e89c683e42f
146
-# To run integration tests docker-pycreds is required.
147 149
 RUN git clone https://github.com/docker/docker-py.git /docker-py \
148 150
 	&& cd /docker-py \
149
-	&& git checkout -q $DOCKER_PY_COMMIT \
150
-	&& pip install docker-pycreds==0.2.1 \
151
-	&& pip install -r test-requirements.txt
151
+	&& git checkout -q $DOCKER_PY_COMMIT
152 152
 
153
-# Install yamllint for validating swagger.yaml
154
-RUN pip install yamllint==1.5.0
155 153
 
154
+
155
+FROM golang AS swagger
156 156
 # Install go-swagger for validating swagger.yaml
157 157
 ENV GO_SWAGGER_COMMIT c28258affb0b6251755d92489ef685af8d4ff3eb
158 158
 RUN set -x \
... ...
@@ -162,49 +123,145 @@ RUN set -x \
162 162
 	&& go build -o /usr/local/bin/swagger github.com/go-swagger/go-swagger/cmd/swagger \
163 163
 	&& rm -rf "$GOPATH"
164 164
 
165
-# Set user.email so crosbymichael's in-container merge commits go smoothly
166
-RUN git config --global user.email 'docker-dummy@example.com'
167
-
168
-# Add an unprivileged user to be used for tests which need it
169
-RUN groupadd -r docker
170
-RUN useradd --create-home --gid docker unprivilegeduser
171
-
172
-VOLUME /var/lib/docker
173
-WORKDIR /go/src/github.com/docker/docker
174
-ENV DOCKER_BUILDTAGS apparmor seccomp selinux
175
-
176
-# Let us use a .bashrc file
177
-RUN ln -sfv $PWD/.bashrc ~/.bashrc
178
-# Add integration helps to bashrc
179
-RUN echo "source $PWD/hack/make/.integration-test-helpers" >> /etc/bash.bashrc
180 165
 
166
+FROM golang AS frozen-images
167
+RUN apt-get update && apt-get install -y jq ca-certificates --no-install-recommends
181 168
 # Get useful and necessary Hub images so we can "docker load" locally instead of pulling
182
-COPY contrib/download-frozen-image-v2.sh /go/src/github.com/docker/docker/contrib/
183
-RUN ./contrib/download-frozen-image-v2.sh /docker-frozen-images \
169
+COPY contrib/download-frozen-image-v2.sh /
170
+RUN /download-frozen-image-v2.sh /docker-frozen-images \
184 171
 	buildpack-deps:jessie@sha256:dd86dced7c9cd2a724e779730f0a53f93b7ef42228d4344b25ce9a42a1486251 \
185 172
 	busybox:1.27-glibc@sha256:8c8f261a462eead45ab8e610d3e8f7a1e4fd1cd9bed5bc0a0c386784ab105d8e \
186 173
 	debian:jessie@sha256:287a20c5f73087ab406e6b364833e3fb7b3ae63ca0eb3486555dc27ed32c6e60 \
187 174
 	hello-world:latest@sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c
188 175
 # See also ensureFrozenImagesLinux() in "integration-cli/fixtures_linux_daemon_test.go" (which needs to be updated when adding images to this list)
189 176
 
190
-# Install tomlv, vndr, runc, containerd, tini, proxy dockercli
191
-# Please edit hack/dockerfile/install/<name>.installer to update them.
192
-COPY hack/dockerfile/install hack/dockerfile/install
193
-RUN for i in tomlv vndr tini gometalinter proxy dockercli runc containerd; \
194
-		do hack/dockerfile/install/install.sh $i; \
195
-	done
196
-ENV PATH=/usr/local/cli:$PATH
177
+# Just a little hack so we don't have to install these deps twice, once for runc and once for dockerd
178
+FROM golang AS runtime-dev
179
+RUN apt-get update && apt-get install -y \
180
+	libapparmor-dev \
181
+	libseccomp-dev
182
+
183
+
184
+FROM golang AS tomlv
185
+ENV INSTALL_BINARY_NAME=tomlv
186
+COPY hack/dockerfile/install/install.sh ./install.sh
187
+COPY hack/dockerfile/install/$INSTALL_BINARY_NAME.installer ./
188
+RUN PREFIX=/opt/$INSTALL_BINARY_NAME ./install.sh $INSTALL_BINARY_NAME
189
+
190
+FROM golang AS vndr
191
+ENV INSTALL_BINARY_NAME=vndr
192
+COPY hack/dockerfile/install/install.sh ./install.sh
193
+COPY hack/dockerfile/install/$INSTALL_BINARY_NAME.installer ./
194
+RUN PREFIX=/opt/$INSTALL_BINARY_NAME ./install.sh $INSTALL_BINARY_NAME
195
+
196
+FROM golang AS containerd
197
+RUN apt-get update && apt-get install -y btrfs-tools
198
+ENV INSTALL_BINARY_NAME=containerd
199
+COPY hack/dockerfile/install/install.sh ./install.sh
200
+COPY hack/dockerfile/install/$INSTALL_BINARY_NAME.installer ./
201
+RUN PREFIX=/opt/$INSTALL_BINARY_NAME ./install.sh $INSTALL_BINARY_NAME
202
+
203
+FROM golang AS proxy
204
+ENV INSTALL_BINARY_NAME=proxy
205
+COPY hack/dockerfile/install/install.sh ./install.sh
206
+COPY hack/dockerfile/install/$INSTALL_BINARY_NAME.installer ./
207
+RUN PREFIX=/opt/$INSTALL_BINARY_NAME ./install.sh $INSTALL_BINARY_NAME
208
+
209
+FROM golang AS gometalinter
210
+ENV INSTALL_BINARY_NAME=gometalinter
211
+COPY hack/dockerfile/install/install.sh ./install.sh
212
+COPY hack/dockerfile/install/$INSTALL_BINARY_NAME.installer ./
213
+RUN PREFIX=/opt/$INSTALL_BINARY_NAME ./install.sh $INSTALL_BINARY_NAME
214
+
215
+FROM golang AS dockercli
216
+ENV INSTALL_BINARY_NAME=dockercli
217
+COPY hack/dockerfile/install/install.sh ./install.sh
218
+COPY hack/dockerfile/install/$INSTALL_BINARY_NAME.installer ./
219
+RUN PREFIX=/opt/$INSTALL_BINARY_NAME ./install.sh $INSTALL_BINARY_NAME
220
+
221
+FROM runtime-dev AS runc
222
+ENV INSTALL_BINARY_NAME=runc
223
+COPY hack/dockerfile/install/install.sh ./install.sh
224
+COPY hack/dockerfile/install/$INSTALL_BINARY_NAME.installer ./
225
+RUN PREFIX=/opt/$INSTALL_BINARY_NAME ./install.sh $INSTALL_BINARY_NAME
226
+
227
+FROM base AS tini
228
+RUN apt-get update && apt-get install -y cmake vim-common
229
+COPY hack/dockerfile/install/install.sh ./install.sh
230
+ENV INSTALL_BINARY_NAME=tini
231
+COPY hack/dockerfile/install/$INSTALL_BINARY_NAME.installer ./
232
+RUN PREFIX=/opt/$INSTALL_BINARY_NAME ./install.sh $INSTALL_BINARY_NAME
233
+
197 234
 
235
+
236
+# TODO: Some of this is only really needed for testing, it would be nice to split this up
237
+FROM runtime-dev AS dev
238
+RUN groupadd -r docker
239
+RUN useradd --create-home --gid docker unprivilegeduser
198 240
 # Activate bash completion and include Docker's completion if mounted with DOCKER_BASH_COMPLETION_PATH
199 241
 RUN echo "source /usr/share/bash-completion/bash_completion" >> /etc/bash.bashrc
200 242
 RUN ln -s /usr/local/completion/bash/docker /etc/bash_completion.d/docker
243
+RUN ldconfig
244
+# This should only install packages that are specifically needed for the dev environment and nothing else
245
+# Do you really need to add another package here? Can it be done in a different build stage?
246
+RUN apt-get update && apt-get install -y \
247
+	apparmor \
248
+	aufs-tools \
249
+	bash-completion \
250
+	btrfs-tools \
251
+	iptables \
252
+	jq \
253
+	libdevmapper-dev \
254
+	libudev-dev \
255
+	libsystemd-dev \
256
+	binutils-mingw-w64 \
257
+	g++-mingw-w64-x86-64 \ 
258
+	net-tools \
259
+	pigz \
260
+	python-backports.ssl-match-hostname \
261
+	python-dev \
262
+	python-mock \
263
+	python-pip \
264
+	python-requests \
265
+	python-setuptools \
266
+	python-websocket \
267
+	python-wheel \
268
+	thin-provisioning-tools \
269
+	vim \
270
+	vim-common \
271
+	xfsprogs \
272
+	zip \
273
+	--no-install-recommends
274
+COPY --from=swagger /usr/local/bin/swagger* /usr/local/bin/
275
+COPY --from=frozen-images /docker-frozen-images /docker-frozen-images
276
+COPY --from=gometalinter /opt/gometalinter/ /usr/local/bin/
277
+COPY --from=tomlv /opt/tomlv/ /usr/local/bin/
278
+COPY --from=vndr /opt/vndr/ /usr/local/bin/
279
+COPY --from=tini /opt/tini/ /usr/local/bin/
280
+COPY --from=runc /opt/runc/ /usr/local/bin/
281
+COPY --from=containerd /opt/containerd/ /usr/local/bin/
282
+COPY --from=proxy /opt/proxy/ /usr/local/bin/
283
+COPY --from=dockercli /opt/dockercli /usr/local/cli
284
+COPY --from=golang /usr/local/go /usr/local/go
285
+COPY --from=registry /usr/local/bin/registry* /usr/local/bin/
286
+COPY --from=notary /usr/local/bin/notary* /usr/local/bin/
287
+COPY --from=criu /opt/criu/ /usr/local/
288
+COPY --from=docker-py /docker-py /docker-py
289
+# TODO: This is for the docker-py tests, which shouldn't really be needed for
290
+# this image, but currently CI is expecting to run this image. This should be
291
+# split out into a separate image, including all the `python-*` deps installed
292
+# above.
293
+RUN cd /docker-py \
294
+	&& pip install docker-pycreds==0.2.1 \
295
+	&& pip install -r test-requirements.txt
201 296
 
202
-# Wrap all commands in the "docker-in-docker" script to allow nested containers
203
-ENTRYPOINT ["hack/dind"]
204
-
297
+ENV PATH=/usr/local/cli:$PATH
298
+ENV DOCKER_BUILDTAGS apparmor seccomp selinux
205 299
 # Options for hack/validate/gometalinter
206 300
 ENV GOMETALINTER_OPTS="--deadline=2m"
207
-
301
+WORKDIR /go/src/github.com/docker/docker
302
+VOLUME /var/lib/docker
303
+# Wrap all commands in the "docker-in-docker" script to allow nested containers
304
+ENTRYPOINT ["hack/dind"]
208 305
 # Upload docker source
209 306
 COPY . /go/src/github.com/docker/docker
210
-