Skip to content

Commit 7f2887e

Browse files
committed
Add Caddy example with Caddyfile using docker-gen
1 parent 53c8ef4 commit 7f2887e

File tree

6 files changed

+64
-3
lines changed

6 files changed

+64
-3
lines changed

README.md

+15-3
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ The Node code is at [./caddy/app/index.js](./caddy/app/index.js) and [./nginx/ap
1919

2020
It also has a reverse-proxy set up using Caddy and nginx respectively which
2121

22-
## Caddy Example
22+
## Caddy Example with Caddyfile
2323

2424
> Caddy is the HTTP/2 web server with automatic HTTPS.
2525
>
2626
> https://caddyserver.com/
2727
28-
See [./caddy](./caddy), uses https://github.com/abiosoft/caddy-docker Docker image.
28+
See [./caddy](./caddy), uses [https://github.com/abiosoft/caddy-docker](https://github.com/abiosoft/caddy-docker) Docker image.
2929

3030
To run it:
3131

@@ -38,13 +38,25 @@ Then either nagivate to https://foo.test or `curl https://foo.test`.
3838

3939
> Note: the nginx example needs to be stopped before starting the Caddy example
4040
41+
## Caddy example with docker-gen
42+
43+
> Caddy is the HTTP/2 web server with automatic HTTPS.
44+
>
45+
> https://caddyserver.com/
46+
47+
This image leverages [Docker-gen](https://github.com/jwilder/docker-gen) to "Generate files from docker container meta-data".
48+
49+
Which allows us to build the configuration for the reverse proxy using labels on the target container (without a Caddyfile, that's the file generated from the labels).
50+
51+
See [./caddy-gen](./caddy-gen), uses [https://github.com/abiosoft/caddy-docker](https://github.com/wemake-services/caddy-gen) Docker image.
52+
4153
## nginx Example
4254

4355
> [nginx](https://www.nginx.com/) is a web server which can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache.
4456
>
4557
> Wikipedia
4658
47-
See [./nginx](./nginx), uses https://github.com/jwilder/nginx-proxy Docker image.
59+
See [./nginx](./nginx), uses [https://github.com/jwilder/nginx-proxy](https://github.com/jwilder/nginx-proxy) Docker image.
4860

4961
```sh
5062
cd nginx

caddy-gen/app/Dockerfile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM node:10
2+
3+
WORKDIR /node/app
4+
5+
COPY ./index.js /node/app
6+
7+
ENV NODE_ENV production
8+
9+
ENV PORT 8080
10+
EXPOSE 8080
11+
12+
CMD ["node", "index.js"]

caddy-gen/app/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const http = require('http')
2+
3+
const server = http.createServer((req, res) => {
4+
res.writeHead(200, { 'Content-Type': 'text/plain' });
5+
res.end('Hello world');
6+
});
7+
8+
server.listen(process.env.PORT)

caddy-gen/certs/.gitkeep

Whitespace-only changes.

caddy-gen/docker-compose.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: '3'
2+
services:
3+
caddy:
4+
image: "wemakeservices/caddy-gen:latest"
5+
volumes:
6+
- /var/run/docker.sock:/tmp/docker.sock:ro # needs socket to read events
7+
- ./certs:/root/certs # to sync mkcert certificated to Caddy
8+
ports:
9+
- "443:2015"
10+
depends_on:
11+
- web
12+
13+
web:
14+
build: ./app
15+
labels:
16+
- "virtual.host=foo.test"
17+
- "virtual.port=8080"
18+
- "virtual.tls-email=/root/certs/foo.test.pem /root/certs/foo.test-key.pem"
19+
20+

init.sh

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ echo """
1515
cp ./foo.test.pem ./caddy/certs
1616
cp ./foo.test-key.pem ./caddy/certs
1717

18+
echo """
19+
==================================================
20+
Copying foo.test certificates to ./caddy-gen/certs
21+
==================================================
22+
"""
23+
24+
cp ./foo.test.pem ./caddy-gen/certs
25+
cp ./foo.test-key.pem ./caddy-gen/certs
26+
1827
echo """
1928
==================================================
2029
Copying foo.test certificates to ./nginx/certs

0 commit comments

Comments
 (0)