Pravar Agrawal Technology & Travel

Add Busybox to Container

While working on the kubernetes, few times we run into a situation where we need to access the shell inside a pod’s container. And to do that we use,

$ kubectl exec -it <pod_name> -- /bin/sh

In this case, as we have invoked the already existing shell so the access works like a butter. But, what if there is no /bin/sh in the container? The easiest way to do that is to copy the busybox executable into a container at the build time. Since, those are just 1Mib we won’t be paying much of a price for few unix utilities and shell access.

Let’s see how we can achieve the same:

FROM golang:1.11-alpine AS build

COPY main.go go.* /src/
RUN go build -o /bin/demo

FROM scratch
COPY --from=build /bin/demo /bin/demo
COPY --from=busybox:1.29 /bin/busybox /bin/busybox
ENTRYPOINT ["/bin/busybox"]

As we can see above, we copied a file from public image of busybox:1.29 and similarly it’s possible to copy a file from any image of our chose say, alpine etc. Now let’s get a shell into our kubernetes pod’s container:

$ kubectl exec -it pod_name -- /bin/busybox sh

So, instead of executing a /bin/sh directly, we invoked /bin/busybox sh to get an access to a shell. Hope this helps!