docker: use one Dockerfile for both production and debug builds

This commit is contained in:
William
2021-05-20 10:26:25 +08:00
committed by William
parent 2f3312e8a1
commit ed8e2cd492
5 changed files with 76 additions and 45 deletions

View File

@@ -1,3 +1,7 @@
.github
.idea
config
Makefile
docker
docs
README.md
.golangci.yml

2
.gitignore vendored
View File

@@ -10,4 +10,4 @@ config/config.docker.toml
log
# Binary file
sphinx
sphinx*

View File

@@ -1,47 +1,66 @@
# Project name.
PROJECT_NAME:=sphinx
# SSH private key set up.
CURRENT_USER?=william
PRIVATE_KEY_FILE?=id_ed25519
PRIVATE_KEY_PATH?=github=/home/$(CURRENT_USER)/.ssh/$(PRIVATE_KEY_FILE)
PROJECT_DIR?=/home/$(CURRENT_USER)/go/src/github.com/SB-IM/sphinx
CURRENT_USER ?= william
PRIVATE_KEY_FILE ?= id_ed25519
PRIVATE_KEY_PATH ?= github=$(shell getent passwd "$(CURRENT_USER)" | cut -d: -f6)/.ssh/$(PRIVATE_KEY_FILE)
# Enable docker buildkit.
DOCKER_BUILDKIT = 1
# Project image repo.
IMAGE?=ghcr.io/sb-im/sphinx:latest-dev
IMAGE ?= ghcr.io/sb-im/sphinx:debug
# OCI platform.
OCI_PLATFORM ?= linux/arm64
# Docker-compose file.
DOCKER_COMPOSE_FILE?=docker/docker-compose.yml
DOCKER_COMPOSE_FILE ?= docker/docker-compose.yml
# Docker-compose service.
SERVICE?=
SERVICE ?=
# Version info for binaries
GIT_REVISION := $(shell git rev-parse --short HEAD)
GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
GIT_TAG := $(shell git describe --tags)
# go build flags.
VPREFIX := github.com/SB-IM/sphinx/cmd/build
GO_LDFLAGS := -X $(VPREFIX).Branch=$(GIT_BRANCH) -X $(VPREFIX).Version=$(GIT_TAG) -X $(VPREFIX).Revision=$(GIT_REVISION) -X $(VPREFIX).BuildUser=$(shell whoami)@$(shell hostname) -X $(VPREFIX).BuildDate=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
GO_FLAGS := -ldflags "-extldflags \"-static\" -s -w $(GO_LDFLAGS)" -a -installsuffix cgo
# See: https://golang.org/doc/gdb#Introduction
DEBUG_GO_FLAGS := -race -gcflags "all=-N -l" -ldflags "-extldflags \"-static\" $(GO_LDFLAGS)"
ifeq ($(OCI_PLATFORM), linux/arm64)
# Fro production build.
IMAGE := $(IMAGE)-arm64
else ifeq ($(OCI_PLATFORM), linux/amd64)
# For debug build.
IMAGE := $(IMAGE)-amd64
GO_FLAGS := DEBUG_GO_FLAGS
endif
.PHONY: run
run:
@DEBUG_MQTT_CLIENT=false go run -race ./cmd --debug livestream -c config/config.dev.toml
@DEBUG_MQTT_CLIENT=false go run -race ./cmd --debug $(SERVICE) -c config/config.dev.toml
.PHONY: sphinx
sphinx:
@go build -o $(PROJECT_NAME) ./cmd
@CGO_ENABLED=0 GOOS=linux go build $(GO_FLAGS) -o $@ ./cmd
sphinx-hookstream:
@CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build $(GO_FLAGS) -o $@ ./cmd
.PHONY: lint
lint:
@golangci-lint run ./...
# binfmt is the prequisite for docker cross platform build.
binfmt:
@docker run --privileged --rm tonistiigi/binfmt --install all
.PHONY: image
image:
@docker build \
--ssh $(PRIVATE_KEY_PATH) \
-t $(IMAGE)-amd64 \
-f docker/Dockerfile.dev .
.PHONY: image-arm64
image-arm64:
@docker buildx build \
--platform linux/arm64 \
--platform $(OCI_PLATFORM) \
--ssh $(PRIVATE_KEY_PATH) \
-t $(IMAGE)-arm64 \
-f docker/Dockerfile .
-t $(IMAGE) \
-f docker/Dockerfile \
.
.PHONY: push
push:
@@ -60,14 +79,14 @@ down:
logs:
@docker-compose -f $(DOCKER_COMPOSE_FILE) logs -f $(SERVICE)
.PHONY: broker
broker:
@docker run -d --rm --name mosquitto -p 1883:1883 -p 9001:9001 -v $(PROJECT_DIR)/config/mosquitto.conf:/mosquitto/config/mosquitto.conf eclipse-mosquitto:2
.PHONY: run-mosquitto
run-mosquitto:
@docker run -d --rm --name mosquitto -p 1883:1883 -p 9001:9001 -v $$PWD/config/mosquitto.conf:/mosquitto/config/mosquitto.conf eclipse-mosquitto:2
.PHONY: stop-broker
stop-broker:
.PHONY: stop-mosquitto
stop-mosquitto:
@docker stop mosquitto
.PHONY: clean
clean:
@rm -rf sphinx
@rm -rf sphinx*

View File

@@ -1,13 +1,12 @@
FROM golang:1.16-alpine
FROM golang:1.16-alpine AS builder
# See: https://docs.github.com/en/packages/guides/connecting-a-repository-to-a-container-image#connecting-a-repository-to-a-container-image-on-the-command-line
LABEL org.opencontainers.image.source=https://github.com/SB-IM/sphinx
RUN apk update && apk add --no-cache \
build-base \
git \
openssh-client \
ca-certificates \
&& update-ca-certificates
openssh-client
WORKDIR /src
@@ -23,15 +22,24 @@ RUN --mount=type=ssh,id=github git config --global url."git@github.com:".instead
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -tags timetzdata -ldflags="-w -s" -a -installsuffix cgo -o sphinx ./cmd
RUN make sphinx
FROM scratch
FROM alpine AS bin
COPY --from=0 /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=0 /src/sphinx /
RUN apk add --no-cache ca-certificates
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /src/sphinx /usr/bin/sphinx
RUN addgroup -g 10001 -S sphinx && \
adduser -u 10001 -S sphinx -G sphinx
USER sphinx
EXPOSE 5004/udp 5005/udp
ENV DEBUG_MQTT_CLIENT=false
VOLUME [ "/config" ]
VOLUME [ "/etc/sphinx" ]
ENTRYPOINT [ "/sphinx" ]
ENTRYPOINT [ "/usr/bin/sphinx" ]

View File

@@ -7,12 +7,12 @@ services:
- --debug
- livestream
- -c
- /config/config.toml
- /etc/sphinx/config.toml
ports:
- "5004:5004/udp"
- "5005:5005/udp"
volumes:
- ../config/config.docker.toml:/config/config.toml:ro
- ../config/config.docker.toml:/etc/sphinx/config.toml:ro
- /etc/machine-id:/etc/machine-id:ro
depends_on:
- mosquitto