Skip to content

Instantly share code, notes, and snippets.

@mmorejon
Created September 11, 2021 19:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mmorejon/91c41178dbe248fba1134ea6ab5f65ae to your computer and use it in GitHub Desktop.
Save mmorejon/91c41178dbe248fba1134ea6ab5f65ae to your computer and use it in GitHub Desktop.
Improving our Dockerfiles using Heredoc

Improving our Dockerfiles using Heredoc - Docker Community All-Hands

Dockerfiles used in the lab

dockerfile-simple file

# base image
FROM ubuntu:20.04

# install jq
RUN apt-get update \
    && apt-get install -y jq \
    && rm -rf /var/lib/apt/lists/*

dockerfile-with-heredoc file

# syntax=docker/dockerfile:1.3-labs

# base image
FROM ubuntu:20.04

# install jq
RUN <<EOF
  apt-get update
  apt-get install -y jq
  rm -rf /var/lib/apt/lists/*
EOF

dockerfile-python file

# syntax=docker/dockerfile:1.3-labs

# base image
FROM python:3.9.7-alpine

# using heredoc to create hello file
RUN python3 <<EOF > /hello
print("Hello")
print("World!")
EOF

dockerfile-nginx file

# syntax=docker/dockerfile:1.3-labs

# base image
FROM nginx:1.21.1

# using heredoc to create index.html file
COPY <<EOF /usr/share/nginx/html/index.html
Welcome to Community All-Hands
EOF

# create robots and humans files
COPY <<robots.txt <<humans.txt /usr/share/nginx/html/
User-agent: *
Disallow:
robots.txt
Manuel Morejon (Cloud Engineer)
https://mmorejon.io
@morejon85
humans.txt

Example 1

docker image build --tag dockerfile-simple --file dockerfile-simple .
docker image build --tag dockerfile-with-heredoc --file dockerfile-with-heredoc .
docker image ls --filter \"reference=dockerfile-*\"
docker image history dockerfile-simple
docker image history dockerfile-with-heredoc

Example 2

docker image build --tag dockerfile-python --file dockerfile-python .
docker container run --rm -it dockerfile-python cat /hello

Example 3

docker image build --tag dockerfile-nginx --file dockerfile-nginx .
docker container run --rm --detach --publish 8080:80 dockerfile-nginx
curl localhost:8080
curl localhost:8080/robots.txt
curl localhost:8080/humans.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment