From 89fa24775cec6b178b815d6aa94aba21bb8377fe Mon Sep 17 00:00:00 2001 From: ripls56 <84716344+ripls56@users.noreply.github.com> Date: Wed, 31 Jul 2024 22:21:37 +0300 Subject: [PATCH] feat(docker): docker support --- .dockerignore | 32 +++++++++++++++++++++ .env.dev | 10 +++++++ Dockerfile | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 3 +- compose.yaml | 73 +++++++++++++++++++++++------------------------ 5 files changed, 157 insertions(+), 39 deletions(-) create mode 100644 .dockerignore create mode 100644 .env.dev create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..9e03c48 --- /dev/null +++ b/.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/.env.dev b/.env.dev new file mode 100644 index 0000000..34d03e9 --- /dev/null +++ b/.env.dev @@ -0,0 +1,10 @@ +APP_ENV=dev + +POSTGRES_PASSWORD=taskem +POSTGRES_URL=postgres://taskem:${POSTGRES_PASSWORD}@0.0.0.0:54321/taskem + +TOKEN_TTL=1h +REFRESH_TOKEN_TTL=168h +TOKEN_SECRET={w:ae2@:;'aE.VW@FP0g813Lo-J\exKAf(0f5pt"+@V,z\2k(jk{MX?F6WK3|;Z"a,BHr9QiKC/{A'R66*Tc3cIKi,LjDG=c8g.8eF0L.UrGZui7[T3OK|u[FhE!;e@M + +GRPC_PORT=50051 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a274766 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,78 @@ +# syntax=docker/dockerfile:1 + +# Comments are provided throughout this file to help you get started. +# If you need more help, visit the Dockerfile reference guide at +# https://docs.docker.com/go/dockerfile-reference/ + +# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7 + +################################################################################ +# Create a stage for building the application. +ARG GO_VERSION=1.22.0 +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 + +# Build the application. +# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds. +# Leverage a bind mount to the current directory to avoid having to copy the +# source code into the container. +RUN --mount=type=cache,target=/go/pkg/mod/ \ + --mount=type=bind,target=. \ + CGO_ENABLED=0 GOARCH=$TARGETARCH go build -o /bin/server ./cmd/server + +################################################################################ +# 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. +# +# The example below uses the alpine image as the foundation for running the app. +# By specifying the "latest" tag, it will also use whatever happens to be the +# most recent version of that image when you build your Dockerfile. If +# reproducability is important, consider using a versioned tag +# (e.g., alpine:3.17.2) or SHA (e.g., alpine@sha256:c41ab5c992deb4fe7e5da09f67a8804a46bd0592bfdf0b1847dde0e0889d2bff). +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 \ + && \ + 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/ +COPY migrations migrations +# Expose the port that the application listens on. +EXPOSE 50051 + +# What the container should run when it is started. +ENTRYPOINT [ "/bin/server" ] diff --git a/Makefile b/Makefile index d19a40f..dd234fe 100644 --- a/Makefile +++ b/Makefile @@ -17,5 +17,6 @@ gen: gen-win: @powershell -ExecutionPolicy Bypass -File ./scripts/gen_proto.ps1 +# make create-migration name="name" create-migration: - @go run github.com/pressly/goose/v3/cmd/goose postgres "user=stream password=stream dbname=stream sslmode=disable" create $(name) sql \ No newline at end of file + @go run github.com/pressly/goose/v3/cmd/goose postgres "user=taskem password=taskem dbname=taskem sslmode=disable" create $(name) sql \ No newline at end of file diff --git a/compose.yaml b/compose.yaml index 6f35700..64a798a 100644 --- a/compose.yaml +++ b/compose.yaml @@ -16,46 +16,43 @@ services: timeout: 5s retries: 5 -# redis: -# image: redis -# restart: always -# ports: -# - 63798:6379 -# healthcheck: -# test: ["CMD", "redis-cli", "--raw", "incr", "ping"] -# interval: 10s -# timeout: 5s -# retries: 5 + # redis: + # image: redis + # restart: always + # ports: + # - 63798:6379 + # healthcheck: + # test: ["CMD", "redis-cli", "--raw", "incr", "ping"] + # interval: 10s + # timeout: 5s + # retries: 5 -# minio: -# image: quay.io/minio/minio -# restart: always -# command: server /data --console-address ":9001" -# ports: -# - "12200:9000" -# - "9001:9001" -# environment: -# MINIO_ROOT_USER: root -# MINIO_ROOT_PASSWORD: YOUR_PASSWORD -# MINIO_DOMAIN: minio -# networks: -# default: -# aliases: -# - users.minio -# - teams.minio + # minio: + # image: quay.io/minio/minio + # restart: always + # command: server /data --console-address ":9001" + # ports: + # - "12200:9000" + # - "9001:9001" + # environment: + # MINIO_ROOT_USER: root + # MINIO_ROOT_PASSWORD: YOUR_PASSWORD + # MINIO_DOMAIN: minio + # networks: + # default: + # aliases: + # - users.minio + # - teams.minio -# server: -# restart: always -# build: -# context: . -# depends_on: -# - db -# - minio -# - redis -# ports: -# - 50051:50051 -# - 9600:9090 -# - 3000:3000 + server: + env_file: ".env" + build: + context: . + target: final + depends_on: + - db + ports: + - 50051:50051 volumes: db-data: \ No newline at end of file