Skip to content

Commit bc4d40d

Browse files
committed
initial commit: add nginx and caddy example
add README add misc config files add init.sh
0 parents  commit bc4d40d

14 files changed

+218
-0
lines changed

.editorconfig

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.md]
13+
trim_trailing_whitespace = false
14+

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.pem
2+
*.crt
3+
*.key

LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
Copyright (c) 2019 Hugo Di Francesco
3+
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21+
OR OTHER DEALINGS IN THE SOFTWARE.
22+

README.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Docker Compose local HTTPS
2+
3+
> Examples of local HTTPS with docker-compose + mkcert
4+
5+
## Prerequisites
6+
7+
- [mkcert](https://github.com/FiloSottile/mkcert)
8+
- [Docker Desktop](https://www.docker.com/products/docker-desktop)
9+
10+
Run `mkcert -install`
11+
12+
Run `./init.sh` (creates local certificates using mkcert and copies them to where the Caddy and nginx examples expect them)
13+
14+
## The setup
15+
16+
Each example has a barebones Node app running on the official Node 10 Docker image running on port 8080.
17+
18+
The Node code is at [./caddy/app/index.js](./caddy/app/index.js) and [./nginx/app/index.js](./caddy/app/index.js).
19+
20+
It also has a reverse-proxy set up using Caddy and nginx respectively which
21+
22+
## Caddy Example
23+
24+
> Caddy is the HTTP/2 web server with automatic HTTPS.
25+
>
26+
> https://caddyserver.com/
27+
28+
See [./caddy](./caddy), uses https://github.com/abiosoft/caddy-docker Docker image.
29+
30+
To run it:
31+
32+
```sh
33+
cd caddy
34+
docker-compose up
35+
```
36+
37+
Then either nagivate to https://foo.test or `curl https://foo.test`.
38+
39+
> Note: the nginx example needs to be stopped before starting the Caddy example
40+
41+
## nginx Example
42+
43+
> [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.
44+
>
45+
> Wikipedia
46+
47+
See [./nginx](./nginx), uses https://github.com/jwilder/nginx-proxy Docker image.
48+
49+
```sh
50+
cd nginx
51+
docker-compose up
52+
```
53+
54+
Then either nagivate to https://foo.test or `curl https://foo.test`.
55+
56+
> Note: the Caddy example needs to be stopped before starting the nginx example

caddy/Caddyfile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
foo.test {
2+
log stdout
3+
# Mkcert - https://github.com/FiloSottile/mkcert
4+
tls /root/certs/foo.test.pem /root/certs/foo.test-key.pem
5+
6+
proxy / http://web:8080 {
7+
transparent
8+
header_upstream X-Marotagem true
9+
header_upstream Host "foo.test"
10+
}
11+
12+
}

caddy/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/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/certs/.gitkeep

Whitespace-only changes.

caddy/docker-compose.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: '3'
2+
services:
3+
caddy:
4+
image: "abiosoft/caddy:latest"
5+
volumes:
6+
- ./certs:/root/certs # to sync mkcert certificated to Caddy
7+
- ./Caddyfile:/etc/Caddyfile # to mount custom Caddyfile
8+
ports:
9+
- "443:2015"
10+
depends_on:
11+
- web
12+
13+
web:
14+
build: ./app
15+
16+

init.sh

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#! /bin/bash
2+
echo """
3+
==================================================
4+
Creating certificate for foo.test
5+
==================================================
6+
"""
7+
mkcert foo.test
8+
9+
echo """
10+
==================================================
11+
Copying foo.test certificates to ./caddy/certs
12+
==================================================
13+
"""
14+
15+
cp ./foo.test.pem ./caddy/certs
16+
cp ./foo.test-key.pem ./caddy/certs
17+
18+
echo """
19+
==================================================
20+
Copying foo.test certificates to ./caddy/certs
21+
==================================================
22+
"""
23+
24+
cp ./foo.test.pem ./nginx/certs/foo.test.crt
25+
cp ./foo.test-key.pem ./nginx/certs/foo.test.key
26+
27+
echo """
28+
==================================================
29+
Add the following to /etc/hosts file:
30+
31+
127.0.0.1 foo.test
32+
==================================================
33+
"""
34+

nginx/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"]

nginx/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)

nginx/certs/.gitkeep

Whitespace-only changes.

nginx/docker-compose.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: '3'
2+
services:
3+
nginx-proxy:
4+
image: jwilder/nginx-proxy
5+
container_name: nginx-proxy
6+
ports:
7+
- "80:80"
8+
- "443:443"
9+
volumes:
10+
- /var/run/docker.sock:/tmp/docker.sock:ro
11+
- ./certs:/etc/nginx/certs
12+
depends_on:
13+
- web
14+
15+
web:
16+
build: ./app
17+
environment:
18+
- VIRTUAL_HOST=foo.test
19+
- VIRTUAL_PORT=8080
20+
21+

0 commit comments

Comments
 (0)