diff --git a/.gitignore b/.gitignore index 98bed52..0e3ca51 100755 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ compose.yml -config/ -certs/ +config*/ +certs*/ bin/ templates/codemirror/ @@ -13,6 +13,6 @@ log/ go.work.sum -!src/config/ +!src/**/ todo.md \ No newline at end of file diff --git a/README.md b/README.md index 8e8f57b..205d93f 100755 --- a/README.md +++ b/README.md @@ -54,7 +54,9 @@ A lightweight, easy-to-use, and [performant](docs/benchmark_result.md) reverse p 2. Setup `go-proxy` [See here](docs/docker.md) -3. Configure `go-proxy` +3. Setup `docker-socket-proxy` (see [example](docs/docker_socket_proxy.md) other machine that is running docker (if any) + +4. Configure `go-proxy` - with text editor (e.g. Visual Studio Code) - or with web config editor via `http://gp.y.z` diff --git a/docs/docker.md b/docs/docker.md index 24f6f3f..d49f473 100644 --- a/docs/docker.md +++ b/docs/docker.md @@ -95,7 +95,7 @@ | `proxy.stop_timeout` | time to wait for stop command | `10s` | `number[unit]...` | | `proxy.stop_signal` | signal sent to container for `stop` and `kill` methods | docker's default | `SIGINT`, `SIGTERM`, `SIGHUP`, `SIGQUIT` and those without **SIG** prefix | | `proxy..` | set field for specific alias | N/A | N/A | -| `proxy.$.` | set field for specific alias at index (starting from **1**) | N/A | N/A | +| `proxy.$.` | set field for specific alias at index (starting from **1**) | N/A | N/A | | `proxy.*.` | set field for all aliases | N/A | N/A | ### Fields @@ -215,6 +215,8 @@ service_a: ## Docker compose examples +More examples in [here](examples/) + ```yaml volumes: adg-work: diff --git a/docs/docker_socket_proxy.md b/docs/docker_socket_proxy.md new file mode 100644 index 0000000..2c0e4db --- /dev/null +++ b/docs/docker_socket_proxy.md @@ -0,0 +1,59 @@ +## Docker Socket Proxy + +For docker client on other machine, set this up, then add `name: tcp://:2375` to `config.yml` under `docker` section + +```yml +# compose.yml on remote machine (e.g. server1) +services: + docker-proxy: + container_name: docker-proxy + image: ghcr.io/linuxserver/socket-proxy + environment: + - ALLOW_START=1 #optional + - ALLOW_STOP=1 #optional + - ALLOW_RESTARTS=0 #optional + - AUTH=0 #optional + - BUILD=0 #optional + - COMMIT=0 #optional + - CONFIGS=0 #optional + - CONTAINERS=1 #optional + - DISABLE_IPV6=1 #optional + - DISTRIBUTION=0 #optional + - EVENTS=1 #optional + - EXEC=0 #optional + - IMAGES=0 #optional + - INFO=0 #optional + - NETWORKS=0 #optional + - NODES=0 #optional + - PING=1 #optional + - POST=1 #optional + - PLUGINS=0 #optional + - SECRETS=0 #optional + - SERVICES=0 #optional + - SESSION=0 #optional + - SWARM=0 #optional + - SYSTEM=0 #optional + - TASKS=0 #optional + - VERSION=1 #optional + - VOLUMES=0 #optional + volumes: + - /var/run/docker.sock:/var/run/docker.sock + restart: always + tmpfs: + - /run + ports: + - 2375:2375 +``` + +```yml +# config.yml on go-proxy machine +autocert: + ... # your config + +providers: + include: + ... + docker: + ... + server1: tcp://:2375 +``` diff --git a/examples/microbin.yml b/examples/microbin.yml new file mode 100644 index 0000000..a59a34a --- /dev/null +++ b/examples/microbin.yml @@ -0,0 +1,16 @@ +services: + app: + container_name: microbin + cpu_shares: 10 + deploy: + resources: + limits: + memory: 256M + env_file: .env + image: docker.i.sh/danielszabo99/microbin:latest + ports: + - 8080 + restart: unless-stopped + volumes: + - ./data:/app/microbin_data +# microbin.domain.tld diff --git a/examples/siyuan.yml b/examples/siyuan.yml new file mode 100644 index 0000000..6bc5a56 --- /dev/null +++ b/examples/siyuan.yml @@ -0,0 +1,16 @@ +services: + main: + image: b3log/siyuan:v3.1.0 + container_name: siyuan + command: + - --workspace=/siyuan/workspace/ + - --accessAuthCode= + user: 1000:1000 + volumes: + - ./workspace:/siyuan/workspace + restart: unless-stopped + environment: + - TZ=Asia/Hong_Kong + ports: + - 6806 +# siyuan.domain.tld diff --git a/src/docker/client.go b/src/docker/client.go index 3fdc1c6..70b4798 100644 --- a/src/docker/client.go +++ b/src/docker/client.go @@ -21,9 +21,8 @@ type Client struct { } func ParseDockerHostname(host string) (string, E.NestedError) { - if host == common.DockerHostFromEnv { - return host, nil - } else if host == "" { + switch host { + case common.DockerHostFromEnv, "": return "localhost", nil } url, err := E.Check(client.ParseHostURL(host)) diff --git a/src/watcher/docker_watcher.go b/src/watcher/docker_watcher.go index a622914..a61c095 100644 --- a/src/watcher/docker_watcher.go +++ b/src/watcher/docker_watcher.go @@ -2,6 +2,7 @@ package watcher import ( "context" + "fmt" "time" docker_events "github.com/docker/docker/api/types/events" @@ -59,19 +60,28 @@ func (w DockerWatcher) EventsWithOptions(ctx context.Context, options DockerList defer close(eventCh) defer close(errCh) + defer func() { + if w.client.Connected() { + w.client.Close() + } + }() + if !w.client.Connected() { var err E.NestedError - for range 3 { + for { w.client, err = D.ConnectClient(w.host) + attempts := 0 if err != nil { - defer w.client.Close() break } - time.Sleep(1 * time.Second) - } - if err.HasError() { - errCh <- E.FailWith("docker connection", err) - return + attempts++ + errCh <- E.FailWith(fmt.Sprintf("docker connection attempt #%d", attempts), err) + select { + case <-ctx.Done(): + return + default: + time.Sleep(3 * time.Second) + } } }