The 'Hello World' of Dockerfiles for Kubernetes
29 Jun 2024 // programming
At work, I've been setting up custom Docker containers on a Kubernetes pipeline.
Since there was so much to get right, I first wanted to deploy a simple Docker container to see if the pipeline was working.
So I went around looking for simple examples - Python, Node, etc. - and eventually I
found a little Nginx Dockerfile
that serves a "Hello World" from an index.html
on
port 80:
FROM nginx:alpine
COPY index.html /usr/share/nginx/html
This was pretty good but it had one issue: as nginx
runs in the background,
Kubernetes will just stop the container after launching.
So to fix that, we'll have to run nginx
in the foreground:
nginx -g 'daemon off;'
And by inlining the index.html
, this results in a very convenient single
file Dockerfile
:
FROM nginx:alpine
RUN echo "<body>Hello World!</body>" > /usr/share/nginx/html/index.html
CMD ["nginx", "-g", "daemon off;"]
Finally, to test the Dockerfile
locally, here's a nifty run.sh
:
name="$(docker image ls | grep web-server-test | awk '{print $1;}')"
if [ $name ]; then
echo "Deleting container $name"
docker image rm $name
fi
docker build -f Dockerfile . -t web-server-test
docker run -p 80:80 web-server-test