Building Docker image from stdin/pipe

klo2k

klo2k

Posted on February 28, 2023

Building Docker image from stdin/pipe

Using - as docker build parameter, you can pipe in any text as your Dockerfile.

I use this trick to quickly test parts of a Dockerfile.

e.g.:

# Build the image, with '-' + heredoc as input
# Works the same with pipe input
docker build --tag klo2k/test - <<'EOT'
FROM ubuntu:latest

# Some complicated looking stuff you wanna try out quickly
RUN <<'EOS' /bin/bash
  echo "${HOSTNAME}" > /tmp/out
EOS

CMD echo "Build: $(cat /tmp/out) Run: ${HOSTNAME}"
EOT

# Run
docker run --rm -it klo2k/test
Enter fullscreen mode Exit fullscreen mode

Example Output:

Build: buildkitsandbox Run: a16cd7dc16ed
Enter fullscreen mode Exit fullscreen mode

Hope you find this useful!

💖 💪 🙅 🚩
klo2k
klo2k

Posted on February 28, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Building Docker image from stdin/pipe
docker Building Docker image from stdin/pipe

February 28, 2023

Getting started with Docker
docker Getting started with Docker

September 20, 2020