You can copy the busybox
into a running container and then run it there.
[[TLDR]]
docker run -d --rm --name busybox busybox:musl sleep 100
docker cp busybox:/bin/busybox ./
chmod +x busybox
docker cp busybox mycontainer1:/busybox
docker exec -it mycontainer1 /busybox mkdir -p /usr/bin /bin /usr/sbin
docker exec -it mycontainer1 /busybox --install
docker exec -it mycontainer1 /busybox sh
(make sure busybox
is statically linked with file busybox
)
[[EXPLAINED]]
To run a shell in a Docker container built FROM scratch
using BusyBox, follow these steps:
1. Extract BusyBox from a Docker Image
- Run a temporary BusyBox container:
docker run -d --rm --name busybox busybox:musl sleep 100
- Copy the BusyBox binary from the temporary container and make it executable:
docker cp busybox:/bin/busybox ./
chmod +x busybox
- Verify that BusyBox is statically linked:
file busybox
The output should indicate that BusyBox is statically linked:
busybox: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, BuildID[sha1]=...
If it is dynamically linked, you might encounter the following errors when running it inside other containers.
OCI runtime exec failed: exec failed: unable to start container process:
exec /busybox: no such file or directory: unknown
If you still experience the issue while running other images, make sure BusyBox platform matches yours
FROM --platform=$BUILDPLATFORM golang
<...>
docker build --build-arg BUILDPLATFORM=linux/arm64 --build-arg opts="CGO_ENABLED=0 GOOS=linux GOARCH=arm64" -t myuser/example:arm64 .
Note:
- Earlier, extracting BusyBox from the
busybox:latest
image provided a static binary. However, recent versions of busybox:latest
might give you a dynamically linked binary instead. Therefore, it's crucial to ensure you get a statically linked version (check image descriptions at https://hub.docker.com/_/busybox). At the time of writing both busybox:uclibc
and busybox:musl
images contained statically linked BusyBox.
- Alternatively, you can download a statically linked BusyBox binary directly from https://busybox.net
2. Copy BusyBox to Your Target Container
- Copy the BusyBox binary into your running container (e.g.,
mycontainer1
):
docker cp busybox mycontainer1:/busybox
3. (Optional) Install BusyBox Functions
- Create necessary directories and install BusyBox tools:
docker exec -it mycontainer1 /busybox mkdir -p /usr/bin /bin /usr/sbin
docker exec -it mycontainer1 /busybox --install
This command installs BusyBox functions (such as ls
, mkdir
, etc.) into the appropriate directories within the container, allowing you to use various utilities provided by BusyBox.
4. Run a Shell in the Container
- Execute a shell using BusyBox in your target container:
docker exec -it mycontainer1 /busybox sh
docker run --entrypoint
would do, too, since I just wish to check if the files have been copied. – Antakiya