Skip to content
This repository was archived by the owner on Aug 6, 2025. It is now read-only.

Commit 5ded721

Browse files
authored
Merge pull request #4 from reload/mkcert
Add mkcert for certificates
2 parents 434bce6 + 0d1cd2f commit 5ded721

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,11 @@ RUN \
2121
a2enconf allow-override-all && \
2222
a2enconf php-fpm
2323

24+
RUN \
25+
mkdir /cert && \
26+
mkdir /mkcert && \
27+
curl -sSL https://github.com/FiloSottile/mkcert/releases/download/v1.3.0/mkcert-v1.3.0-linux-amd64 -o /usr/local/bin/mkcert && \
28+
chmod +x /usr/local/bin/mkcert
29+
2430
EXPOSE 80
2531
EXPOSE 443

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,89 @@
11
# Apache FPM container based on phusion
22
Simple apache-vhost that serves content from /var/www/web - php-requests are
33
proxied to a linked fpm-container named "fpm" on port 9000.
4+
5+
6+
# Dory
7+
8+
Use the following if you use [dory](https://github.com/FreedomBen/dory) (much of the same applies for [nginx-proxy](https://github.com/jwilder/nginx-proxy) and [dinghy-http-proxy](https://github.com/codekitchen/dinghy-http-proxy) which Dory is based on).
9+
10+
```yaml
11+
environment:
12+
VIRTUAL_HOST: example.docker
13+
VIRTUAL_PORT: 80
14+
# Use the following if you want to handle redirects from http to https yourself.
15+
HTTPS_METHOD: noredirect
16+
17+
```
18+
19+
## Using autogenerated certificates
20+
This require Dory version 1.0.3, use `dory upgrade` to upgrade
21+
22+
If you don't already have a Dory configuration-file, have it generate one by running `dory config` then update the update `ssl_certs_dir` configuration:
23+
24+
```yaml
25+
nginx_proxy:
26+
enabled: true
27+
container_name: dory_dinghy_http_proxy
28+
https_enabled: true
29+
# Update the follow line to point at the dev_certificates
30+
ssl_certs_dir: <your homedir>/.local/share/dev_certificates
31+
```
32+
33+
Then mount that directory into the apache-fpm container so that its auto-generated certificates will be accessible to Dory.
34+
35+
```yaml
36+
volumes:
37+
- '${HOME}/.local/share/dev_certificates:/cert:rw'
38+
```
39+
40+
Then follow the steps in the mkcert sections to specify which certificates to generate and have your OS trust them.
41+
42+
# mkcert
43+
44+
This image has [mkcert](https://github.com/FiloSottile/mkcert)
45+
builtin.
46+
47+
Install `mkcert` on your host machine and generate and install a root
48+
certificate by running `mkcert -install` on your host machine.
49+
50+
Then you add the generated CAROOT as a volume (the path on the host
51+
machine is the output of `mkcert -CAROOT`).
52+
53+
In your `docker-compose.yml` supply one or more host names to be be
54+
used for HTTPS. Host names will be search for in these location and in
55+
this order:
56+
57+
1. environment variable `MKCERT_DOMAINS` (several hostnames separated
58+
by space is possible, you can even supply a wildcard domain),
59+
1. the environment variable `VIRTUAL_HOST` (as used by [Dinghy HTTP
60+
Proxy](https://github.com/codekitchen/dinghy-http-proxy)), or
61+
1. the output of `hostname -f` in the container (which can be set with
62+
the `hostname` and `domainname` options).
63+
64+
```yaml
65+
volumes:
66+
- '${HOME}/Library/Application Support/mkcert:/mkcert/mac:ro'
67+
- '${HOME}/.local/share/mkcert:/mkcert/linux:ro'
68+
69+
environment:
70+
MKCERT_DOMAINS: "example.docker *.example.docker local.docker"
71+
72+
hostname: example
73+
domainname: docker
74+
```
75+
76+
# Full example configuration
77+
78+
```yaml
79+
volumes:
80+
- '${HOME}/Library/Application Support/mkcert:/mkcert/mac:ro'
81+
- '${HOME}/.local/share/mkcert:/mkcert/linux:ro'
82+
- '${HOME}/.local/share/dev_certificates:/cert:rw'
83+
84+
environment:
85+
MKCERT_DOMAINS: "example.docker *.example.docker local.docker"
86+
VIRTUAL_HOST: example.docker
87+
VIRTUAL_PORT: 80
88+
HTTPS_METHOD: noredirect
89+
```

files/etc/my_init.d/mkcert.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# Try to locate `rootCA.pem` and `rootCA-key.pem` in a folder beneath
6+
# `/mkcert`.
7+
CAROOT="$(find /mkcert -type d -exec sh -c '[ -f "$0"/rootCA.pem ] && [ -f "$0"/rootCA-key.pem ]' '{}' \; -print)"
8+
export CAROOT
9+
10+
# If no root CA found just exit now without generating any
11+
# certificates.
12+
if [[ -z "${CAROOT}" ]]; then
13+
exit 0;
14+
fi
15+
16+
# If no VIRTUAL_HOST is set use `hostname -f` as fallback.
17+
VIRTUAL_HOST="${VIRTUAL_HOST:-$(hostname -f)}"
18+
19+
# Dinghys wildcard syntax is prefixing only with a dot (as in
20+
# `.example.com`). We rewrite those to use an asterisk as expected by
21+
# mkcert (`*.example.com`).
22+
VIRTUAL_HOST="${VIRTUAL_HOST/#./*.}"
23+
24+
# If on MKCERT_DOMAINS is set use VIRTUAL_HOST as fallback.
25+
MKCERT_DOMAINS="${MKCERT_DOMAINS:-${VIRTUAL_HOST}}"
26+
27+
# If we couldn't find any domain names just exit now without
28+
# generating any certificates.
29+
if [[ -z "${MKCERT_DOMAINS}" ]]; then
30+
exit 0;
31+
fi
32+
33+
# Split a space separated string into a bash array.
34+
IFS=' ' read -r -a MKCERT_DOMAINS <<< "${MKCERT_DOMAINS}"
35+
36+
# Install the CA certificate in the Docker containers system trust
37+
# store. Mostly we do that to ignore warnings about the CA not being
38+
# installed when generating certificates later (but also to trust the
39+
# certificates from within).
40+
/usr/local/bin/mkcert -install
41+
42+
# Run `mkcert` to generate certificate and key.
43+
/usr/local/bin/mkcert -cert-file /etc/ssl/certs/ssl-cert-snakeoil.pem -key-file /etc/ssl/private/ssl-cert-snakeoil.key "${MKCERT_DOMAINS[@]}"
44+
45+
# Expose the generated certificate in /cert named after the first
46+
# domain name (compatible with Dory / nginx-proxy).
47+
echo "Copying certficate(s) and key(s) into /cert:"
48+
for domain in "${MKCERT_DOMAINS[@]}"
49+
do
50+
# Strip wildcard.
51+
domain="${domain#\*\.}"
52+
cp -v /etc/ssl/certs/ssl-cert-snakeoil.pem "/cert/${domain}.crt"
53+
cp -v /etc/ssl/private/ssl-cert-snakeoil.key "/cert/${domain}.key"
54+
done

0 commit comments

Comments
 (0)