Skip to content

Commit 0abafdf

Browse files
committed
update
1 parent 88d9a1c commit 0abafdf

1 file changed

Lines changed: 86 additions & 102 deletions

File tree

caddy_v2/readme.md

Lines changed: 86 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
![logo](https://i.imgur.com/xmSY5qu.png)
66

7-
[update with this in mind](https://www.reddit.com/r/selfhosted/comments/10r9o4d/reverse_proxies_with_nginx_proxy_manager/)
8-
97
1. [Purpose & Overview](#Purpose--Overview)
108
2. [Caddy as a reverse proxy in docker](#Caddy-as-a-reverse-proxy-in-docker)
119
3. [Caddy more info and various configurations](#Caddy-more-info-and-various-configurations)
1210
4. [Caddy DNS challenge](#Caddy-DNS-challenge)
11+
5. [Other guides](#Other-guides)
12+
13+
*[Older version](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/d973916d56e23bb5564bd9b68e06ec884cfc6af1/caddy_v2)
14+
of this guide - more detailed and handholding for docker noobs.*
1315

1416
# Purpose & Overview
1517

@@ -22,23 +24,23 @@ while `jellyfin.example.com` points to the media server on the network.
2224
* [Forum](https://caddy.community/)
2325
* [Github](https://github.com/caddyserver/caddy)
2426

25-
Caddy is a pretty damn good web server with automatic HTTPS. Written in Go.<br>
27+
Caddy is a pretty damn good web server with automatic HTTPS. Written in Go.
28+
2629
Web servers are build to deal with http traffic, so they are an obvious choice
27-
for the function of reverse proxy.<br>
28-
In this setup Caddy is used mostly as
30+
for the function of reverse proxy. In this setup Caddy is used mostly as
2931
[a TLS termination proxy](https://www.youtube.com/watch?v=H0bkLsUe3no).
3032
Https encrypted tunel ends with it, so that the traffic can be analyzed
3133
and send to a correct webserver based on the settings in `Caddyfile`.
3234

33-
Caddy with its build-in https and and simple config approach
34-
allows even most trivial configs to just work:
35-
35+
Caddy with its build-in automatic https allows configs to be clean and simple
36+
and to just work.
37+
3638
```
37-
nextcloud.{$MY_DOMAIN} {
39+
nextcloud.example.com {
3840
reverse_proxy nextcloud-web:80
3941
}
4042
41-
jellyfin.{$MY_DOMAIN} {
43+
jellyfin.example.com {
4244
reverse_proxy 192.168.1.20:80
4345
}
4446
```
@@ -57,30 +59,18 @@ and only some special casess with extra functionality need extra work.
5759
Caddy will be running as a docker container and will route traffic to other containers,
5860
or machines on the network.
5961

60-
[gurucomputing caddy guide](https://blog.gurucomputing.com.au/reverse-proxies-with-caddy/)
61-
62-
### - Requirements
63-
64-
* have some basic linux knowledge, create folders, create files, edit files, run scripts,...
65-
* have a docker host and some vague docker knowledge
66-
* have port 80 and 443 forwarded on the router/firewall to the docker host
67-
* have a domain, `example.com`, you can buy one for 2€ annually on namecheap
68-
* have correctly set type-A DNS records pointing at your public IP address,
69-
[switching to Cloudflare](https://youtu.be/XQKkb84EjNQ) for DNS managment is recommended
70-
71-
7262
### - Files and directory structure
7363

7464
```
7565
/home/
7666
└── ~/
7767
└── docker/
7868
└── caddy/
79-
├── config/
80-
├── data/
81-
├── .env
82-
├── Caddyfile
83-
└── docker-compose.yml
69+
├── 🗁 config/
70+
├── 🗁 data/
71+
├── 🗋 .env
72+
├── 🗋 Caddyfile
73+
└── 🗋 docker-compose.yml
8474
```
8575

8676
* `config/` - a directory containing configs that Caddy generates,
@@ -98,37 +88,14 @@ the content of these is visible only as root of the docker host.
9888

9989
`docker network create caddy_net`
10090

101-
All the containers and Caddy must be on the same network.
102-
103-
### - Create .env file
91+
All the future containers and Caddy must be on the same network,
92+
ping-able by their hostnames.
10493

105-
You want to change `example.com` to your domain.
94+
### - Create docker-compose.yml and .env file
10695

107-
`.env`
108-
```bash
109-
MY_DOMAIN=example.com
110-
DOCKER_MY_NETWORK=caddy_net
111-
```
112-
113-
Domain names, api keys, email settings, ip addresses, database credentials, ...
114-
whatever is specific for one deployment and different for another,
115-
all of that ideally goes in to the `.env` file.
116-
117-
If `.env` file is present in the directory with the compose file,
118-
it is automatically loaded and these variables will be available
119-
for docker-compose when building the container with `docker-compose up`.
120-
This allows compose files to be moved from system to system more freely
121-
and changes are done to the `.env` file.
122-
123-
Often variable should be available also inside the running container.
124-
For that it must be declared in the `environment` section of the compose file,
125-
as can be seen next in Caddie's `docker-compose.yml`
126-
127-
*extra info:*<br>
128-
`docker-compose config` shows how compose will look
129-
with the variables filled in.
130-
131-
### - Create docker-compose.yml
96+
Basic simple docker compose.<br>
97+
Official caddy image is used. Ports 80 and 443 are pusblished/mapped
98+
on to docker host as Caddy is the one in charge of any traffic coming there.<br>
13299

133100
`docker-compose.yml`
134101
```yml
@@ -139,11 +106,10 @@ services:
139106
container_name: caddy
140107
hostname: caddy
141108
restart: unless-stopped
109+
env_file: .env
142110
ports:
143111
- "80:80"
144112
- "443:443"
145-
environment:
146-
- MY_DOMAIN
147113
volumes:
148114
- ./Caddyfile:/etc/caddy/Caddyfile
149115
- ./data:/data
@@ -155,12 +121,14 @@ networks:
155121
external: true
156122
```
157123
158-
* port 80 and 443 are pusblished for http and https
159-
* MY_DOMAIN variable is passed in to the container so that it can be used
160-
in `Caddyfile`
161-
* the `Caddyfile` is bind-mounted from the docker host
162-
* directories `data` and `config` are bind mounted so that their content persists
163-
* the same network is joined as for all other containers
124+
`.env`
125+
```php
126+
MY_DOMAIN=example.com
127+
DOCKER_MY_NETWORK=caddy_net
128+
```
129+
130+
You obviously want to change `example.com` to your domain.
131+
164132

165133
### - Create Caddyfile
166134

@@ -179,36 +147,37 @@ b.{$MY_DOMAIN} {
179147
}
180148
```
181149
182-
`a` and `b` are the subdomains `a.example.com` and `b.example.com`,
183-
can be named whatever.
184-
For them to work they must have type-A DNS record
185-
pointing at your public ip set on Cloudflare, or wherever the domains DNS is managed.<br>
150+
`a` and `b` are the subdomains, can be named whatever.<br>
151+
For them to work they **must have type-A DNS record set**, that points
152+
at your public ip set on Cloudflare, or wherever the domains DNS is managed.<br>
153+
154+
Can test if correctly set with online dns lookup tools,
155+
[like this one.](https://mxtoolbox.com/DNSLookup.aspx)
186156
187-
The value of `{$MY_DOMAIN}` is provided by the compose and the `.env` file.<br>
157+
The value of `{$MY_DOMAIN}` is provided by the `.env` file.<br>
188158
The subdomains point at docker containers by their **hostname** and **exposed port**.
189-
So every docker container you spin should have hostname definied.<br>
190-
Commented out is the staging url for let's encrypt.
159+
So every docker container you spin should have hostname definied and be on
160+
`caddy_net`, or some other named custom network, as the default bridge docker network
161+
[does not provide](https://docs.docker.com/network/bridge/)
162+
automatic DNS resolution between containers.<br>
163+
Commented out is the staging url for let's encrypt, used for testing.
191164
192-
### - Setup some docker containers
165+
<details>
166+
<summary><h3>Setup some docker containers</h3></summary>
193167
194-
Something light and easy to setup to route to.<br>
195-
Assuming for this testing these compose files are in the same directory with Caddy,
196-
so they make use of the same `.env` file and so be on the same network.
168+
Something light to setup to route to that has a webpage to show.<br>
169+
Not bothering with an `.env` file here.
197170
198171
Note the lack of published/mapped ports in the compose,
199172
as they will be accessed only through Caddy, which has it's ports published.<br>
200-
And since the containers and Caddy are all on the same bridge docker network,
201-
they can access each other on any port.<br>
202-
Exposed ports are just documentation,
203-
[don't confuse expose and publish](https://maximorlov.com/exposing-a-port-in-docker-what-does-it-do/).
173+
Containers on the same bridge docker network can access each other on any port.<br>
204174
205175
*extra info:*<br>
206176
To know which ports containers have exposed - `docker ps`, or
207177
`docker port <container-name>`, or use [ctop](https://github.com/bcicen/ctop).
208178
209179
`whoami-compose.yml`
210180
```yaml
211-
version: "3.7"
212181
services:
213182
214183
whoami:
@@ -218,13 +187,12 @@ services:
218187
219188
networks:
220189
default:
221-
name: $DOCKER_MY_NETWORK
190+
name: caddy_net
222191
external: true
223192
```
224193

225194
`nginx-compose.yml`
226195
```yaml
227-
version: "3.7"
228196
services:
229197

230198
nginx:
@@ -234,18 +202,24 @@ services:
234202

235203
networks:
236204
default:
237-
name: $DOCKER_MY_NETWORK
205+
name: caddy_net
238206
external: true
239207
```
240-
### - editing hosts file
241208
242-
You are on your local network and you are likely running the docker host
243-
inside the same network.<br>
244-
If that's the case then shit will not work without editing the hosts file.<br>
245-
Reason being that when you write that `a.example.com` in to your browser,
246-
you are asking google's DNS for `a.example.com` IP address.
247-
It will give you your own public IP, and most routers/firewalls wont allow
248-
this loopback, where your requests should go out and then right back.
209+
</details>
210+
211+
---
212+
---
213+
214+
<details>
215+
<summary><h3>Editing hosts file</h3></summary>
216+
217+
If the docker host is with you on your local network then you need to deal
218+
with bit of an issue.
219+
When you write that `a.example.com` in to your browser, you are asking
220+
internet DNS server for IP address of `a.example.com`.
221+
DNS servers will reply with your own public IP, and most consumer routers
222+
wont allow this loopback, where your requests should go out and then right back.
249223

250224
So just [edit](https://support.rackspace.com/how-to/modify-your-hosts-file/)
251225
`hosts` as root/administrator,
@@ -256,24 +230,28 @@ adding whatever is the local IP of the docker host and the hostname:
256230
192.168.1.222 b.{$MY_DOMAIN}
257231
```
258232

233+
You can test what are the replies for DNS requests with the command
234+
`nslookup a.example.com`, works in linux and windows.
235+
259236
If it is just quick testing one can use Opera browser
260237
and enable the build in VPN.<br>
261238

262-
One can also run a dns/dhcp server on the network, to solve this for all
263-
devices.<br>
264-
Here's a [guide-by-example for dnsmasq](
265-
https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/dnsmasq).
266-
267-
### - Run it all
239+
This edit of a host file affects only that one machine on the network.
240+
To solve it for all devices theres need to to run dns server on the network,
241+
or running a tier higher firewall/router.
242+
* [Here's](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/dnsmasq)
243+
a guide-by-example for dnsmasq.
244+
* [Here's](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/opnsense)
245+
a guide-by-example for opnsense firewall
268246

269-
Caddy
247+
</details>
270248

271-
* `docker-compose up -d`
249+
---
250+
---
272251

273-
Services
252+
### - Run it all
274253

275-
* `docker-compose -f whoami-compose.yml up -d`
276-
* `docker-compose -f nginx-compose.yml up -d`
254+
Run all the containers.
277255

278256
Give it time to get certificates, checking `docker logs caddy` as it goes,
279257
then visit the urls. It should lead to the services with https working.
@@ -740,3 +718,9 @@ dick with the Caddyfile this much. Just one global line declaration.
740718
But the effort went sideways.<br>
741719
So I myself do not even bother with wildcard when the config ends up looking
742720
complex and ugly.
721+
722+
723+
# Other guides
724+
725+
* [gurucomputing caddy guide](https://blog.gurucomputing.com.au/reverse-proxies-with-caddy/)
726+
*

0 commit comments

Comments
 (0)