Skip to content

Commit e545ded

Browse files
committed
Describe the new way to log to Google Cloud Logging.
1 parent a629236 commit e545ded

File tree

4 files changed

+57
-14
lines changed

4 files changed

+57
-14
lines changed

README.md

+31-14
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ following setup:
2222
* All docker containers are started via a docker-compose.yml file. Each
2323
of those gets their own subdirectory with the docker-compose.yml
2424
file alongside any additional configuration and data volumes needed.
25-
* I run caddy with the docker-proxy and caddy-tls-redis plugins in a
25+
* I run caddy with the docker-proxy and caddy-storage-redis plugins in a
2626
container as front end proxy.
2727
* Individual containers for the services use caddy docker proxy label
2828
fragments for configuration in the individual docker-compose.yml
@@ -46,9 +46,6 @@ override.conf like this:
4646
```
4747
[Unit]
4848
After=tailscaled.service
49-
50-
[Service]
51-
Environment="GOOGLE_APPLICATION_CREDENTIALS=/home/adminuser/.serviceaccts/hosting-XXXXXX-XXXXXXXXXXXX.json"
5249
```
5350

5451
The After= section makes sure that docker starts after tailscale is
@@ -85,31 +82,51 @@ ExecStart=/usr/bin/sh -c "/usr/bin/tailscale up; echo tailscale-up"
8582
Experimenting with systemd-resolved might also reduce the number of
8683
overwrites to the resolv.conf file.
8784

88-
GOOGLE_APPLICATION_CREDENTIALS injects the credentials of
89-
a service account that has log and error reporting permissions on a
90-
Google Cloud project. I modify the docker daemon config in
91-
/etc/docker/dameon.json like this:
85+
## Logging to Google Cloud Logging (Stackdriver)
86+
87+
The Google Cloud configuration is optional if you like to use journalctl
88+
on the individual hosts.
89+
90+
I used to use the gcplogs log driver built into docker, but I am really
91+
switching all my projects to structured json based logging and was looking
92+
for ways to directly feed that into google cloud logging. The docker gpclogs driver does not do this, but I found the project
93+
[ngcplogs](https://github.com/nanoandrew4/ngcplogs)
94+
that modified the gcplogs driver driver to extract the structured log info.
95+
96+
This driver is a docker plgin and is installed like this:
97+
98+
````
99+
docker plugin install nanoandrew4/ngcplogs:linux-arm64-v1.3.0
100+
````
101+
102+
The driver is configured as usual in /etc/docker/dameon.json
103+
like this:
92104

93105
```
94106
{
95-
"log-driver": "gcplogs",
107+
"log-driver": "nanoandrew4/ngcplogs:linux-arm64-v1.3.0",
96108
"log-opts": {
109+
"exclude-timestamp" : "true",
97110
"gcp-project": "hosting-XXXXXX",
98111
"gcp-meta-name": "myservername"
112+
"credentials-json" : "your_json_escaped_credentials.json_file_content"
99113
}
100114
}
101115
```
102116

103-
The Google Cloud configuration is optional if you like to use journalctl
104-
on the individual hosts.
117+
The escaped json string for the Google service account with log writing permissions can be gnerated with the json-escape.go program like this:
118+
119+
```
120+
go run json-escape.go </path/to/my-service-acct.json
121+
```
105122

106123
## Caddy
107124

108125
The root directory of this repo contains the Dockerfile and a
109126
build-docker.sh script to build the container that runs caddy with the
110-
docker-proxy, tls-redis and caddy-dns/cloudflare plugins. I do build both
111-
AMD64 and ARM64 versions of each of my containers as my linux systems
112-
use both of these architectures.
127+
docker-proxy, caddy-storage-redis and caddy-dns/cloudflare plugins. I do
128+
build both AMD64 and ARM64 versions of each of my containers as my linux
129+
systems use both of these architectures.
113130

114131
The caddy subdirectory showcases a typical caddy configuration. I do run
115132
caddy in its container with ports forwarded for port 80 and 443 TCP and

go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/jum/caddy-docker-proxy-redis
2+
3+
go 1.22.3
4+
5+
require github.com/PurpleSec/escape v1.0.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/PurpleSec/escape v1.0.0 h1:25crJCsGmePlY6sTggm+qTg0xABcb7A0nUFgNas+N+U=
2+
github.com/PurpleSec/escape v1.0.0/go.mod h1:y7jqOGecytNh1ROko233Z91ER9NHNuepiLgZtkrDMME=

json-escape.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"io"
5+
"os"
6+
7+
"github.com/PurpleSec/escape"
8+
)
9+
10+
func main() {
11+
buf, err := io.ReadAll(os.Stdin)
12+
if err != nil {
13+
panic(err)
14+
}
15+
_, err = os.Stdout.WriteString(escape.JSON(string(buf)))
16+
if err != nil {
17+
panic(err)
18+
}
19+
}

0 commit comments

Comments
 (0)