diff options
author | 2024-08-05 21:27:28 -0700 | |
---|---|---|
committer | 2024-08-05 21:55:38 -0700 | |
commit | 34b09d6291eb272ea5e69a79c572de7637e0a122 (patch) | |
tree | 69859db855cc5497f6386bc6db689afc998f9fe8 | |
parent | 0686dc6088cb2779272a49ac05d79cb40debb61b (diff) | |
download | ibd-trader-34b09d6291eb272ea5e69a79c572de7637e0a122.tar.gz ibd-trader-34b09d6291eb272ea5e69a79c572de7637e0a122.tar.zst ibd-trader-34b09d6291eb272ea5e69a79c572de7637e0a122.zip |
Adds Docker build
-rw-r--r-- | backend/.dockerignore | 32 | ||||
-rw-r--r-- | backend/.github/workflows/docker.yaml | 74 | ||||
-rw-r--r-- | backend/Dockerfile | 79 |
3 files changed, 185 insertions, 0 deletions
diff --git a/backend/.dockerignore b/backend/.dockerignore new file mode 100644 index 0000000..9e03c48 --- /dev/null +++ b/backend/.dockerignore @@ -0,0 +1,32 @@ +# Include any files or directories that you don't want to be copied to your +# container here (e.g., local build artifacts, temporary files, etc.). +# +# For more help, visit the .dockerignore file reference guide at +# https://docs.docker.com/go/build-context-dockerignore/ + +**/.DS_Store +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/bin +**/charts +**/docker-compose* +**/compose.y*ml +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md diff --git a/backend/.github/workflows/docker.yaml b/backend/.github/workflows/docker.yaml new file mode 100644 index 0000000..cf6aec9 --- /dev/null +++ b/backend/.github/workflows/docker.yaml @@ -0,0 +1,74 @@ +name: Docker + +on: + # schedule: + # - cron: '45 13 * * *' + push: + branches: [ "main" ] + # Publish semver tags as releases. + tags: [ 'v*.*.*' ] + pull_request: + branches: [ "main" ] + +env: + # Use docker.io for Docker Hub if empty + REGISTRY: ghcr.io + # github.repository as <account>/<repo> + IMAGE_NAME: ${{ github.repository }} + + +jobs: + build: + + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + submodules: recursive + token: ${{ secrets.PAT_TOKEN }} + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + # Set up BuildKit Docker container builder to be able to build + # multi-platform images and export cache + # https://github.com/docker/setup-buildx-action + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + # Login against a Docker registry except on PR + # https://github.com/docker/login-action + - name: Log into registry ${{ env.REGISTRY }} + if: github.event_name != 'pull_request' + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + # Extract metadata (tags, labels) for Docker + # https://github.com/docker/metadata-action + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + # Build and push Docker image with Buildx (don't push on PR) + # https://github.com/docker/build-push-action + - name: Build and push Docker image + id: build-and-push + uses: docker/build-push-action@v6 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..caef54a --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,79 @@ +# syntax=docker/dockerfile:1 +################################################################################ +# Create a stage for building the application. +ARG GO_VERSION=1.22.5 +FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS build +WORKDIR /src + +# Download dependencies as a separate step to take advantage of Docker's caching. +# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds. +# Leverage bind mounts to go.sum and go.mod to avoid having to copy them into +# the container. +RUN --mount=type=cache,target=/go/pkg/mod/ \ + --mount=type=bind,source=go.sum,target=go.sum \ + --mount=type=bind,source=go.mod,target=go.mod \ + go mod download -x + +# This is the architecture you're building for, which is passed in by the builder. +# Placing it here allows the previous steps to be cached across architectures. +ARG TARGETARCH + +# Specify the versions of the tools to use. +ARG BUF_VERSION=1.35.1 +ARG MOCKGEN_VERSION=0.4.0 + +# Install required tools for build +RUN go install "go.uber.org/mock/mockgen@v${MOCKGEN_VERSION}" && \ + curl -sSL "https://github.com/bufbuild/buf/releases/download/v${BUF_VERSION}/buf-$(uname -s)-$(uname -m)" \ + -o "/usr/local/bin/buf" && \ + chmod +x /usr/local/bin/buf && \ + buf --version + +# Bind mounts are read-only, so we copy the source code into the container. +COPY . . + +# Build the application. +# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds. +RUN --mount=type=cache,target=/go/pkg/mod/ \ + go generate -v ./... && \ + CGO_ENABLED=0 GOARCH=$TARGETARCH go build -o /bin/server ./cmd + +################################################################################ +# Create a new stage for running the application that contains the minimal +# runtime dependencies for the application. This often uses a different base +# image from the build stage where the necessary files are copied from the build +# stage. +FROM alpine:latest AS final + +# Install any runtime dependencies that are needed to run your application. +# Leverage a cache mount to /var/cache/apk/ to speed up subsequent builds. +RUN --mount=type=cache,target=/var/cache/apk \ + apk --update add \ + ca-certificates \ + tzdata \ + tini \ + && \ + update-ca-certificates + +# Create a non-privileged user that the app will run under. +# See https://docs.docker.com/go/dockerfile-user-best-practices/ +ARG UID=10001 +RUN adduser \ + --disabled-password \ + --gecos "" \ + --home "/nonexistent" \ + --shell "/sbin/nologin" \ + --no-create-home \ + --uid "${UID}" \ + appuser +USER appuser + +# Copy the executable from the "build" stage. +COPY --from=build /bin/server /bin/ + +# Expose the port that the application listens on. +EXPOSE 8000 + +# What the container should run when it is started. +ENTRYPOINT ["/sbin/tini", "--"] +CMD ["/bin/server"] |