DEV Community

klo2k
klo2k

Posted on

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!

Top comments (0)