brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: docker build can read the Dockerfile from stdin

Docker
~/tips/tip-docker-build-dockerfile-stdin

docker build -t myimage:latest - <<EOF

Builds an image from a Dockerfile passed through a pipe or a here-document, with or without a context.

For a quick test image I don’t want to create a folder and a file. Docker can read the Dockerfile from standard input, and a here-document makes it a one-command experiment.

docker build -t myimage:latest - <<EOF
FROM busybox
RUN echo "hello world"
EOF

With - as the context there is no build context at all, which the docs call an empty context. You can also pipe a file in with docker build - < Dockerfile.

If the Dockerfile needs local files, give it a context and read only the Dockerfile from stdin with -f-:

docker build -t myimage:latest -f- . <<EOF
FROM busybox
COPY somefile.txt ./
RUN cat /somefile.txt
EOF

Here the current directory is the context, so COPY somefile.txt works. The docs also show a remote context, docker build -f- <URL> with a Git repository, and building directly from the URL of a remote Dockerfile.

The caveat is the first form. Without a filesystem context, instructions like COPY cannot refer to local files, so that build fails. Use -f- . when you need your files. I’d also keep anything long in a real file in version control, because a here-document in shell history is a poor place to store a recipe.

Further reading: Docker build context

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link