Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 30cc436

Browse filesBrowse files
author
Takashi Matsuo
committed
allow docker in docker
1 parent 365f2e4 commit 30cc436
Copy full SHA for 30cc436

File tree

Expand file treeCollapse file tree

2 files changed

+42
-15
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+42
-15
lines changed

‎.kokoro/docker/Dockerfile

Copy file name to clipboardExpand all lines: .kokoro/docker/Dockerfile
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,17 @@ ENV PATH /google-cloud-sdk/bin:$PATH
161161
# Enable redis-server on boot.
162162
RUN sudo systemctl enable redis-server.service
163163

164+
# Create a user and allow sudo
165+
ARG UID=0
166+
ARG GID=0
167+
ARG USERNAME=kbuilder
168+
ARG DOCKER_GID=999
169+
170+
# Allow access docker socker in the host.
171+
RUN groupadd -g ${DOCKER_GID} "host-docker"
172+
RUN groupadd -g ${GID} "${USERNAME}"
173+
RUN useradd -d /h -u ${UID} -g ${GID} ${USERNAME}
174+
RUN adduser "${USERNAME}" "host-docker"
175+
RUN echo "${USERNAME} ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
176+
164177
CMD ["python3.6"]

‎.kokoro/trampoline_v2.sh

Copy file name to clipboardExpand all lines: .kokoro/trampoline_v2.sh
+29-15Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,28 @@ else
180180
fi
181181

182182

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+
183194
update_cache="false"
184195
if [[ "${TRAMPOLINE_IMAGE_SOURCE:-none}" != "none" ]]; then
185196
# Build the Docker image from the source.
186197
context_dir=$(dirname "${TRAMPOLINE_IMAGE_SOURCE}")
187198
docker_build_flags=(
188199
"-f" "${TRAMPOLINE_IMAGE_SOURCE}"
189200
"-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}"
190205
)
191206
if [[ "${has_cache}" == "true" ]]; then
192207
docker_build_flags+=("--cache-from" "${TRAMPOLINE_IMAGE}")
@@ -207,13 +222,6 @@ else
207222
fi
208223
fi
209224

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-
217225
# We use an array for the flags so they are easier to document.
218226
docker_flags=(
219227
# Remove the container after it exists.
@@ -233,21 +241,27 @@ docker_flags=(
233241
# Tells scripts whether they are running as part of CI or not.
234242
"--env" "RUNNING_IN_CI=${RUNNING_IN_CI:-no}"
235243

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
237245
# 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}"
239248

240249
# Pass down the USER.
241-
"--env" "USER=${docker_user}"
250+
"--env" "USER=${user_name}"
242251

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}"
247258

248259
# Mount the temporary home directory.
249260
"--volume" "${tmphome}:/h"
250261
"--env" "HOME=/h"
262+
263+
# Allow docker in docker.
264+
"--volume" "/var/run/docker.sock:/var/run/docker.sock"
251265
)
252266

253267
# Add an option for nicer output if the build gets a tty.
@@ -270,7 +284,7 @@ if [[ $# -ge 1 ]]; then
270284
readonly commands=("${@:1}")
271285
else
272286
log_yellow "Running the tests in a Docker container."
273-
readonly commands=("/v/${TRAMPOLINE_BUILD_FILE}")
287+
readonly commands=("${PWD}/${TRAMPOLINE_BUILD_FILE}")
274288
fi
275289

276290
echo docker run "${docker_flags[@]}" "${TRAMPOLINE_IMAGE}" "${commands[@]}"

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.