Luís de Sousa
Luís de Sousa

Reputation: 6842

Install Bash on scratch Docker image

I am presently working with a third party Docker image whose Dockerfile is based on the empty image, starting with the FROM scratch directive.

How can Bash be installed on such image? I tried adding some extra commands to the Dockerfile, but apparently the RUN directive itself requires Bash.

Upvotes: 9

Views: 16754

Answers (2)

Adiii
Adiii

Reputation: 59966

The question is old but I see a similar question and came here, SO posting to deal with such case below.

I am presently working with a third party Docker image whose Dockerfile is based on the empty image, starting with the FROM scratch directive.

As mentioned by @David there is nothing in such image that is based on scratch, If the image is based on the scratch image they just copy the binaries to image and that's it.

So the hack around with such image is to copy the binaries into your extend image and use them in your desired docker image.

For example postgres_exporter

FROM scratch
ARG binary
COPY $binary /postgres_exporter
EXPOSE 9187
ENTRYPOINT [ "/postgres_exporter" ]

So this is based on scratch and I can not install bash or anything else I can just copy binaries to run.

So here is the work, use them as the multi-stage base image, copy the binaries and installed packages in your docker images.

Below we need to add wait-for-it

FROM wrouesnel/postgres_exporter 
# use the above base image
FROM debian:7.11-slim
RUN useradd -u 20001 postgres_exporter
USER postgres_exporter
#copy binires 
COPY --from=0 /postgres_exporter /postgres_exporter
EXPOSE 9187
COPY wait-for-it.sh wait-for-it.sh
USER root
RUN chmod +x wait-for-it.sh
USER postgres_exporter
RUN pwd
ENTRYPOINT ["./wait-for-it.sh", "db:5432", "--", "./postgres_exporter"]

Upvotes: -1

David Maze
David Maze

Reputation: 159040

When you start a Docker image FROM scratch you get absolutely nothing. Usually the way you work with one of these is by building a static binary on your host (or these days in an earlier Dockerfile build stage) and then COPY it into the image.

FROM scratch
COPY mybinary /
ENTRYPOINT ["/mybinary"]

Nothing would stop you from creating a derived image and COPYing additional binaries into it. Either you'd have to specifically build a static binary or install a full dynamic library environment.

If you're doing this to try to debug the container, there is probably nothing else in the image. One thing this means is that the set of things you can do with a shell is pretty boring. The other is that you're not going to have the standard tool set you're used to (there is not an ls or a cp). If you can live without bash's various extensions, BusyBox is a small tool designed to be statically built and installed in limited environments that provides minimal versions of most of these standard tools.

Upvotes: 1

Related Questions