@@ -180,13 +180,28 @@ else
180
180
fi
181
181
182
182
183
+ # The default user for a Docker container has uid 0 (root). To avoid
184
+ # creating root-owned files in the build directory we tell docker to
185
+ # use the current user ID.
186
+ user_uid=" $( id -u) "
187
+ user_gid=" $( id -g) "
188
+ user_name=" $( id -un) "
189
+
190
+ # To allow docker in docker, we add the user to the docker group in
191
+ # the host os.
192
+ docker_gid=$( cut -d: -f3 < <( getent group docker) )
193
+
183
194
update_cache=" false"
184
195
if [[ " ${TRAMPOLINE_IMAGE_SOURCE:- none} " != " none" ]]; then
185
196
# Build the Docker image from the source.
186
197
context_dir=$( dirname " ${TRAMPOLINE_IMAGE_SOURCE} " )
187
198
docker_build_flags=(
188
199
" -f" " ${TRAMPOLINE_IMAGE_SOURCE} "
189
200
" -t" " ${TRAMPOLINE_IMAGE} "
201
+ " --build-arg" " UID=${user_uid} "
202
+ " --build-arg" " GID=${user_gid} "
203
+ " --build-arg" " USERNAME=${user_name} "
204
+ " --build-arg" " DOCKER_GID=${docker_gid} "
190
205
)
191
206
if [[ " ${has_cache} " == " true" ]]; then
192
207
docker_build_flags+=(" --cache-from" " ${TRAMPOLINE_IMAGE} " )
@@ -207,13 +222,6 @@ else
207
222
fi
208
223
fi
209
224
210
- # The default user for a Docker container has uid 0 (root). To avoid
211
- # creating root-owned files in the build directory we tell docker to
212
- # use the current user ID.
213
- docker_uid=" $( id -u) "
214
- docker_gid=" $( id -g) "
215
- docker_user=" $( id -un) "
216
-
217
225
# We use an array for the flags so they are easier to document.
218
226
docker_flags=(
219
227
# Remove the container after it exists.
@@ -233,21 +241,27 @@ docker_flags=(
233
241
# Tells scripts whether they are running as part of CI or not.
234
242
" --env" " RUNNING_IN_CI=${RUNNING_IN_CI:- no} "
235
243
236
- # Run the docker script and this user id. Because the docker image gets to
244
+ # Run the docker script with the user id. Because the docker image gets to
237
245
# write in ${PWD} you typically want this to be your user id.
238
- " --user" " ${docker_uid} :${docker_gid} "
246
+ # Also to allow docker in docker, we use docker gid on the host.
247
+ " --user" " ${user_uid} :${docker_gid} "
239
248
240
249
# Pass down the USER.
241
- " --env" " USER=${docker_user } "
250
+ " --env" " USER=${user_name } "
242
251
243
- # Mount the project directory inside the Docker container.
244
- " --volume" " ${PWD} :/v"
245
- " --workdir" " /v"
246
- " --env" " PROJECT_ROOT=/v"
252
+ # Mount the project directory inside the Docker container. To
253
+ # allow docker in docker correctly mount the volume, we use the
254
+ # same path for the volume.
255
+ " --volume" " ${PWD} :${PWD} "
256
+ " --workdir" " ${PWD} "
257
+ " --env" " PROJECT_ROOT=${PWD} "
247
258
248
259
# Mount the temporary home directory.
249
260
" --volume" " ${tmphome} :/h"
250
261
" --env" " HOME=/h"
262
+
263
+ # Allow docker in docker.
264
+ " --volume" " /var/run/docker.sock:/var/run/docker.sock"
251
265
)
252
266
253
267
# Add an option for nicer output if the build gets a tty.
@@ -270,7 +284,7 @@ if [[ $# -ge 1 ]]; then
270
284
readonly commands=(" ${@: 1} " )
271
285
else
272
286
log_yellow " Running the tests in a Docker container."
273
- readonly commands=(" /v /${TRAMPOLINE_BUILD_FILE} " )
287
+ readonly commands=(" ${PWD} /${TRAMPOLINE_BUILD_FILE} " )
274
288
fi
275
289
276
290
echo docker run " ${docker_flags[@]} " " ${TRAMPOLINE_IMAGE} " " ${commands[@]} "
0 commit comments