From ae3b74ad2a9775a787fc0161a1cbd48bce5c05a7 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Mon, 27 Jun 2022 22:31:57 -0500 Subject: [PATCH] Docker build refactor (#568) --- .dockerignore | 4 ++- .github/workflows/build.yml | 33 ++++++++++++++++++++++++ .github/workflows/release.yml | 47 +++++++++++++++++++++++++++++++++++ Dockerfile | 34 +++++++++++++++++-------- Makefile | 2 +- docker-compose.yml | 41 ++++++++++++++++++++++++++++++ 6 files changed, 148 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/release.yml create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore index f59ec20..cca6534 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1 +1,3 @@ -* \ No newline at end of file +.github +bin/ +./pgweb diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..61d0e18 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,33 @@ +name: build + +on: + - push + +env: + GO_VERSION: 1.18 + CGO_ENABLED: 0 + IMAGE_REPOSITORY: sosedoff/pgweb + +jobs: + docker-build: + name: docker images + runs-on: ubuntu-latest + timeout-minutes: 20 + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Configure docker build context + uses: docker/setup-buildx-action@v2 + + - name: Build docker images + uses: docker/build-push-action@v2 + with: + context: . + push: false + tags: pgweb:latest + platforms: linux/amd64,linux/arm64,linux/arm/v5,linux/arm/v7 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..60d972d --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,47 @@ +name: release + +on: + push: + tags: + - "v*" + +env: + GO_VERSION: 1.18 + CGO_ENABLED: 0 + IMAGE_REPOSITORY: sosedoff/pgweb + +jobs: + docker-release: + name: Publish Docker images + runs-on: ubuntu-latest + timeout-minutes: 20 + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Configure docker build context + uses: docker/setup-buildx-action@v1 + + - name: Set reference tags + id: refs + run: | + echo ::set-output name=SOURCE_NAME::${GITHUB_REF#refs/*/} + echo ::set-output name=SOURCE_BRANCH::${GITHUB_REF#refs/heads/} + echo ::set-output name=SOURCE_TAG::${GITHUB_REF#refs/tags/v} + + - name: Login to Docker + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Build and push docker images + uses: docker/build-push-action@v2 + with: + context: . + push: true + tags: | + ${{ env.IMAGE_REPOSITORY }}:${{ steps.refs.outputs.SOURCE_TAG }} + ${{ env.IMAGE_REPOSITORY }}:latest + platforms: linux/amd64,linux/arm64,linux/arm/v5,linux/arm/v7 diff --git a/Dockerfile b/Dockerfile index 4d3ecda..0dfeb88 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,29 @@ -FROM alpine:3.15 -LABEL maintainer="Dan Sosedoff " -ENV PGWEB_VERSION 0.11.11 +# ------------------------------------------------------------------------------ +# Builder Stage +# ------------------------------------------------------------------------------ +FROM golang:1.18-buster AS build + +WORKDIR /build +ADD . /build + +RUN go mod download +RUN make build + +# ------------------------------------------------------------------------------ +# Release Stage +# ------------------------------------------------------------------------------ +FROM debian:buster-slim RUN \ - apk update && \ - apk add --no-cache ca-certificates openssl postgresql wget && \ + apt-get update && \ + apt-get install -y ca-certificates openssl postgresql && \ update-ca-certificates && \ - rm -rf /var/cache/apk/* && \ - cd /tmp && \ - wget -q https://github.com/sosedoff/pgweb/releases/download/v$PGWEB_VERSION/pgweb_linux_amd64.zip && \ - unzip pgweb_linux_amd64.zip -d /usr/bin && \ - mv /usr/bin/pgweb_linux_amd64 /usr/bin/pgweb && \ - rm -f pgweb_linux_amd64.zip + apt-get clean autoclean && \ + apt-get autoremove --yes && \ + rm -rf /var/lib/{apt,dpkg,cache,log}/ + +COPY --from=build /build/pgweb /usr/bin/pgweb EXPOSE 8081 + CMD ["/usr/bin/pgweb", "--bind=0.0.0.0", "--listen=8081"] diff --git a/Makefile b/Makefile index a5323a7..2b9bfdb 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -TARGETS = darwin/amd64 darwin/arm64 linux/amd64 linux/386 windows/amd64 windows/386 +TARGETS = darwin/amd64 darwin/arm64 linux/amd64 linux/arm64 windows/amd64 GIT_COMMIT = $(shell git rev-parse HEAD) BUILD_TIME = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ" | tr -d '\n') GO_VERSION = $(shell go version | awk {'print $$3'}) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e51a1ca --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,41 @@ +--- +version: "3.9" + +services: + postgres: + container_name: pgweb-postgres + image: postgres:14 + ports: + - 5433:5432 + volumes: + - data:/var/lib/postgresql/data + environment: + POSTGRES_DB: pgweb + POSTGRES_PASSWORD: pgweb + POSTGRES_USER: pgweb + healthcheck: + test: pg_isready -U pgweb -h 127.0.0.1 + interval: 5s + networks: + - pgweb + + pgweb: + container_name: pgweb + image: sosedoff/pgweb:latest + environment: + DATABASE_URL: postgres://pgweb:pgweb@pgweb-postgres:5432/pgweb?sslmode=disable + ports: + - 8081:8081 + networks: + - pgweb + depends_on: + postgres: + condition: service_healthy + +volumes: + data: + name: pgweb_postgres + +networks: + pgweb: + name: pgweb