Skip to content

Instantly share code, notes, and snippets.

@norio-nomura
Last active June 23, 2024 04:58
Show Gist options
  • Save norio-nomura/edf224977880e6e7598e76b1330f2990 to your computer and use it in GitHub Desktop.
Save norio-nomura/edf224977880e6e7598e76b1330f2990 to your computer and use it in GitHub Desktop.
How to attach a shell to a stage during `docker buildx build`

How to attach a shell to a stage during docker buildx build

  1. Build this gist with --build-arg SLEEP=1
docker buildx build https://gist.github.com/edf224977880e6e7598e76b1330f2990.git --build-arg SLEEP=1

e.g.

$ docker buildx build https://gist.github.com/edf224977880e6e7598e76b1330f2990.git --build-arg SLEEP=1
[+] Building 12.0s (5/6)                                                                                                     docker:lima-docker
 => [internal] load git source https://gist.github.com/edf224977880e6e7598e76b1330f2990.git                                                2.8s
 => [internal] load metadata for docker.io/library/ubuntu:latest                                                                           0.0s
 => [builder 1/2] FROM docker.io/library/ubuntu:latest@sha256:2e863c44b718727c860746568e1d54afd13b2fa71b160f5cd9058fc436217b30             1.9s
 => => resolve docker.io/library/ubuntu:latest@sha256:2e863c44b718727c860746568e1d54afd13b2fa71b160f5cd9058fc436217b30                     1.9s
 => CACHED [builder 2/2] RUN false || [ -n "1" ] # Ignore error if SLEEP is set                                                            0.0s 
 => CACHED [builder-sleep 1/2] RUN echo 'echo This shell is running in "builder-sleep" stage.' > ~/.bashrc                                 0.0s 
 => [builder-sleep 2/2] RUN pidns=$(readlink /proc/self/ns/pid|sed -E 's/pid:\[([0-9]+)\]/\1/') &&     cat <<EOT  && sleep 999999          7.3s 
 => => # Execute the following command to attach a shell to this build stage:                                                                  
 => => # - lima user:                                                                                                                          
 => => #     limactl shell docker bash -c 'sudo nsenter --all --target=$(lsns|awk "/^4026532471/{print \$4}") bash'                            
 => => # - Docker for Mac user:                                                                                                                
 => => #     docker run -it --privileged --pid=host --rm ubuntu bash -c 'nsenter --all --target=$(lsns|awk "/^4026532471/{print \$4}") bash'   
  1. Copy and paste the command line from the build logs to execute and attach the shell to the build stage.
    Since the value passed to 'target' in the command line varies with each build, it needs to be copied from the logs.

e.g.

$ limactl shell docker bash -c 'sudo nsenter --all --target=$(lsns|awk "/^4026532471/{print \$4}") bash'
This shell is running in builder-sleep stage.
root@buildkitsandbox:/# uname -a
Linux buildkitsandbox 6.8.0-35-generic #35-Ubuntu SMP PREEMPT_DYNAMIC Tue May 21 07:52:29 UTC 2024 aarch64 aarch64 aarch64 GNU/Linux
root@buildkitsandbox:/# 
ARG SLEEP
ARG _SLEEP_SELECTOR=${SLEEP:+-sleep}
# This is a stage that will be used to build the target executable
FROM ubuntu AS builder
ARG SLEEP
RUN false || [ -n "${SLEEP}" ] # Ignore error if SLEEP is set
# This is a stage to wait for attaching a shell
FROM builder AS builder-sleep
RUN echo 'echo This shell is running in "builder-sleep" stage.' > ~/.bashrc
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=cache,target=/root/.cache \
pidns=$(readlink /proc/self/ns/pid|sed -E 's/pid:\[([0-9]+)\]/\1/') && \
cat <<EOT && sleep 999999
Execute the following command to attach a shell to this build stage:
- lima user:
limactl shell docker bash -c 'sudo nsenter --all --target=\$(lsns|awk "/^$pidns/{print \\\$4}") bash'
- Docker for Mac user:
docker run -it --privileged --pid=host --rm ubuntu bash -c 'nsenter --all --target=\$(lsns|awk "/^$pidns/{print \\\$4}") bash'
EOT
# This is a final stage
FROM builder${_SLEEP_SELECTOR} AS final
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment