Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for running master as a docker container #13

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ Alternatively, you can download any of the master/node pre-compiled binaries fro

Please refer to [env.md](docs/env.md) to get guidance on how to properly configure master/node usage.

### Docker

The tool was designed to be deployed using the binaries released on GitHub. However, if you want to partially containerize the zero-monitor master agent, you can do it via docker:

```bash
# First, build the image
./tools/build-docker

# Finally, start a container
./tool/run-docker
```

## Tech Stack

- Go (echo v4, html/template)
Expand Down
29 changes: 29 additions & 0 deletions deployments/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This is the base specification for building master agent as a docker image.
# Use [tools/build-docker] to easily build the image.
# Use [tools/run-docker] to easily run the container.

FROM golang:1.23.1-alpine AS builder
WORKDIR /zero-monitor

COPY go.mod ./
COPY go.sum ./

RUN go mod download

COPY cmd/ ./cmd/
COPY internal/ ./internal/
COPY pkg/ ./pkg/
COPY public/ ./public/
COPY .version .version

RUN version="$(cat .version)"
RUN build_pkg="github.com/guackamolly/zero-monitor/internal/build"
RUN GOOS=linux CGO_ENABLED=0 go build -ldflags="-X $build_pkg.version=$version -X $build_pkg.release=true -extldflags=-static -s -w" -o master ./cmd/master/master.go

FROM scratch
WORKDIR /zero-monitor

COPY --from=builder /zero-monitor /zero-monitor
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

CMD [ "/zero-monitor/master" ]
6 changes: 6 additions & 0 deletions tools/build-docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

docker_tag=zero-monitor:latest
docker build \
-f ./deployments/Dockerfile \
. -t $docker_tag
41 changes: 41 additions & 0 deletions tools/run-docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

pwd="$(dirname "$BASH_SOURCE")"
docker_tag="$(cat $pwd/build-docker | grep "docker_tag=" | cut -d "=" -f 2)"

# Pass -d flag to run container in background as a daemon.
daemon_flag=""
if [ "$1" = '-d' ]; then
daemon_flag='-d'
fi

# Ensure volume for app directory exists.
docker volume create zero-monitor

# If .env is not present, expose bootstrapped ports.
if [ ! -f .env ]; then
docker run $daemon_flag \
--mount type=bind,src=/etc/machine-id,dst=/etc/machine-id \
-v zero-monitor:/.config/zero-monitor \
-p 8080:8080 \
-p 36113:36113 \
-t $docker_tag

exit $?
fi

# (WARNING): Expert docker user only.
# If .env is present, use values defined there instead of bootstrapped configuration.
# Paths defined in .env file must be absolute!
export $(grep -v '^#' .env | xargs)
docker run $daemon_flag \
--mount type=bind,src=/etc/machine-id,dst=/etc/machine-id \
--mount type=bind,src=$bolt_db_path,dst=$bolt_db_path \
--mount type=bind,src=$mq_transport_pem_key,dst=$mq_transport_pem_key \
--mount type=bind,src=$server_tls_crt_fp,dst=$server_tls_crt_fp \
--mount type=bind,src=$server_tls_key_fp,dst=$server_tls_key_fp \
-v zero-monitor:/.config/zero-monitor \
-p $server_port:$server_port \
-p $mq_sub_port:$mq_sub_port \
--env-file .env \
-t $docker_tag