HereDocker - blitterated/docker-dev-env GitHub Wiki

Docker build heredoc inception

Who needs a Dockerfile? Or files? Or even a build context? Hand docker build a heredoc that contains a heredoc. Run the following to build an image ready to go with an index.html.

Note that the # syntax=docker/dockerfile:1.3-labs line is necessary so docker build understands the heredoc syntax given to RUN. Also notice that the RUN command is multi-line, and no backslashes, \, are necessary.

docker build -t pyception -<<EOB
# syntax=docker/dockerfile:1.3-labs
FROM python
RUN mkdir /web
WORKDIR /web
RUN cat <<"EOF" > index.html
<html>
  <head>
    <title>whirled peas</title>
  </head>
  <body>
    <h1>Hello Whirled!</h1>
  </body>
</html>
EOF
ENTRYPOINT ["python", "-m", "http.server"]
EOB

Quickly run a container and curl a result:

PYCID=$(docker run -d --rm -p 8000:8000 pyception)
sleep 1 # let the container spin up
curl http://localhost:8000
docker kill --signal=SIGINT $PYCID

Do the same on a different port: 9999

PYCID=$(docker run -d --rm -p 9999:9999 pyception 9999)
sleep 1 # let the container spin up
curl http://localhost:9999
docker kill --signal=SIGINT $PYCID

In both of the above, we can't simply call docker stop. That's because docker stop sends a SIGTERM to PID 1. If the process hasn't shut down within 10 seconds a SIGKILL is sent to the kernel instead.

Since Python's http.server was never meant to be run as a daemon, it doesn't respond to SIGTERM. It's made to be run in the foreground, and terminated with Ctrl-c, which sends a SIGINT to the process. Hence, we use docker kill --signal=SIGINT to do the same.

Troubleshooting commands

Override the entrypoint to run a shell instead:

docker run -it --rm -p 8000:8000 --entrypoint /bin/bash pyception

Start a simple web server once in the container:

python -m http.server

References

⚠️ **GitHub.com Fallback** ⚠️