Skip to content

Instantly share code, notes, and snippets.

@caius
Last active July 13, 2022 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caius/c70d113624c731223caec937965d9f56 to your computer and use it in GitHub Desktop.
Save caius/c70d113624c731223caec937965d9f56 to your computer and use it in GitHub Desktop.

Output

Running on host machine

Running with RUNTIME_SECRET unset in the shell.

$ caddy run
$ http localhost:8080
HTTP/1.1 200 OK
Content-Length: 178
Content-Type: text/html; charset=utf-8
Date: Wed, 13 Jul 2022 11:01:11 GMT
Server: Caddy

<!DOCTYPE html>
<html>
  <head>
    <title>Caddy Template</title>
  </head>
  <body>

    <h1>Hello World.</h1>

    <p>
      The current secret is 
    </p>

  </body>
</html>

Running with RUNTIME_SECRET set to a value in the shell before running Caddy

$ RUNTIME_SECRET=zomgwtfbbqsekrit caddy run
$ http localhost:8080
HTTP/1.1 200 OK
Content-Length: 194
Content-Type: text/html; charset=utf-8
Date: Wed, 13 Jul 2022 11:02:26 GMT
Server: Caddy

<!DOCTYPE html>
<html>
  <head>
    <title>Caddy Template</title>
  </head>
  <body>

    <h1>Hello World.</h1>

    <p>
      The current secret is zomgwtfbbqsekrit
    </p>

  </body>
</html>

Running inside Docker

Build the docker image first, reuse it for both examples

$ docker build -t caddy-template:dev .

Without anything set for RUNTIME_SECRET

$ docker run -it --rm -p 8080:8080 caddy-template:dev
$ http localhost:8080
HTTP/1.1 200 OK
Content-Length: 178
Content-Type: text/html; charset=utf-8
Date: Wed, 13 Jul 2022 11:07:04 GMT
Server: Caddy

<!DOCTYPE html>
<html>
  <head>
    <title>Caddy Template</title>
  </head>
  <body>

    <h1>Hello World.</h1>

    <p>
      The current secret is 
    </p>

  </body>
</html>

Setting the secret value to something in the docker container env

$ docker build -t caddy-template:dev .
$ docker run -it --rm -p 8080:8080 -e RUNTIME_SECRET=zomgwtfbbqsekrit caddy-template:dev
$ http localhost:8080
HTTP/1.1 200 OK
Content-Length: 194
Content-Type: text/html; charset=utf-8
Date: Wed, 13 Jul 2022 11:08:55 GMT
Server: Caddy

<!DOCTYPE html>
<html>
  <head>
    <title>Caddy Template</title>
  </head>
  <body>

    <h1>Hello World.</h1>

    <p>
      The current secret is zomgwtfbbqsekrit
    </p>

  </body>
</html>
{
auto_https off
}
:8080 {
root * .
file_server
templates
}
{
auto_https off
}
:8080 {
root * /var/www/html
file_server
templates
}
FROM caddy:2.5.2 AS main
COPY Caddyfile.docker /etc/caddy/Caddyfile
COPY index.html /var/www/html/
<!DOCTYPE html>
<html>
<head>
<title>Caddy Template</title>
</head>
<body>
<h1>Hello World.</h1>
<p>
The current secret is {{ env "RUNTIME_SECRET" }}
</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment