From 184465b13c3e0223ea76945987351e79825034ed Mon Sep 17 00:00:00 2001 From: bifidokk Date: Sat, 20 Apr 2024 14:52:49 +0300 Subject: [PATCH] Add docker --- .docker/.env | 3 +++ .docker/app/Dockerfile | 16 +++++++++++ .docker/app/Dockerfile.dev | 10 +++++++ .docker/bot_dev.env.dist | 5 ++++ .docker/docker-compose.dev.yml | 45 +++++++++++++++++++++++++++++++ .docker/docker-compose.yml | 43 +++++++++++++++++++++++++++++ .docker/migrations/Dockerfile | 3 +++ .docker/migrations/Dockerfile.dev | 3 +++ .docker/postgres/init/dbinit.sh | 7 +++++ .gitignore | 4 ++- Makefile | 5 ++++ migrations/.gitkeep | 0 12 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 .docker/.env create mode 100644 .docker/app/Dockerfile create mode 100644 .docker/app/Dockerfile.dev create mode 100644 .docker/bot_dev.env.dist create mode 100644 .docker/docker-compose.dev.yml create mode 100644 .docker/docker-compose.yml create mode 100644 .docker/migrations/Dockerfile create mode 100644 .docker/migrations/Dockerfile.dev create mode 100755 .docker/postgres/init/dbinit.sh create mode 100644 Makefile create mode 100644 migrations/.gitkeep diff --git a/.docker/.env b/.docker/.env new file mode 100644 index 0000000..381d6ea --- /dev/null +++ b/.docker/.env @@ -0,0 +1,3 @@ +COMPOSE_PROJECT_NAME=whisper-bot + +POSTGRES_MIGRATION_DSN="postgres://postgres:postgres@postgres:5432/whisperbot?sslmode=disable" \ No newline at end of file diff --git a/.docker/app/Dockerfile b/.docker/app/Dockerfile new file mode 100644 index 0000000..6a67f73 --- /dev/null +++ b/.docker/app/Dockerfile @@ -0,0 +1,16 @@ +FROM golang:1.22 as builder + +WORKDIR /app +COPY go.mod go.sum ./ +RUN go mod download +COPY . ./ + +RUN go build ./cmd/main.go + +FROM alpine:latest +RUN apk add --no-cache ca-certificates libc6-compat + +WORKDIR /app +COPY --from=builder /app . + +CMD ["./main"] \ No newline at end of file diff --git a/.docker/app/Dockerfile.dev b/.docker/app/Dockerfile.dev new file mode 100644 index 0000000..e83630a --- /dev/null +++ b/.docker/app/Dockerfile.dev @@ -0,0 +1,10 @@ +FROM golang:1.22 + +WORKDIR /app + +COPY . . +RUN go mod download && go mod verify + +RUN go build ./cmd/main.go + +CMD ["./main"] \ No newline at end of file diff --git a/.docker/bot_dev.env.dist b/.docker/bot_dev.env.dist new file mode 100644 index 0000000..f38a048 --- /dev/null +++ b/.docker/bot_dev.env.dist @@ -0,0 +1,5 @@ +APP_TOKEN= +APP_WEBHOOK_URL= +APP_WEBHOOK_PATH="/bot/" +APP_BASE_URL=":9000" +APP_OPENAI_TOKEN= \ No newline at end of file diff --git a/.docker/docker-compose.dev.yml b/.docker/docker-compose.dev.yml new file mode 100644 index 0000000..29acd52 --- /dev/null +++ b/.docker/docker-compose.dev.yml @@ -0,0 +1,45 @@ +version: '3' + +services: + postgres: + image: postgres:14.1 + container_name: db + ports: + - "5432:5432" + restart: always + environment: + POSTGRES_PASSWORD: postgres + volumes: + - ./db/:/var/lib/postgresql/data/:rw + - ./postgres/init:/docker-entrypoint-initdb.d + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres"] + interval: 5s + timeout: 5s + retries: 5 + + app: + container_name: app + build: + context: ../ + dockerfile: ./.docker/app/Dockerfile.dev + ports: + - "8080:8080" + restart: always + depends_on: + postgres: + condition: service_healthy + env_file: + - bot_dev.env + + migrations: + container_name: migrations + build: + context: ../ + dockerfile: ./.docker/migrations/Dockerfile.dev + command: migrate apply --url ${POSTGRES_MIGRATION_DSN} --dir "file://migrations" + depends_on: + postgres: + condition: service_healthy + env_file: + - bot_dev.env \ No newline at end of file diff --git a/.docker/docker-compose.yml b/.docker/docker-compose.yml new file mode 100644 index 0000000..c1d71d2 --- /dev/null +++ b/.docker/docker-compose.yml @@ -0,0 +1,43 @@ +version: '3' + +services: + postgres: + image: postgres:14.1 + container_name: db + environment: + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + POSTGRES_DB: ${POSTGRES_DB} + POSTGRES_USER: ${POSTGRES_USER} + PGDATA: /var/lib/postgresql/data/ + volumes: + - ./db:/var/lib/postgresql/data/:rw + healthcheck: + test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"] + interval: 5s + timeout: 5s + retries: 5 + env_file: + - .env.prod + + app: + container_name: app + image: bifidokk/whisper-bot-app:latest + ports: + - ${API_PORT}:8080 + restart: always + depends_on: + postgres: + condition: service_healthy + env_file: + - .env.prod + + migrations: + container_name: migrations + image: bifidokk/whisper-bot-migrations:latest + command: migrate apply --url ${POSTGRES_MIGRATION_DSN} --dir "file://migrations" + depends_on: + postgres: + condition: service_healthy + env_file: + - .env.prod + \ No newline at end of file diff --git a/.docker/migrations/Dockerfile b/.docker/migrations/Dockerfile new file mode 100644 index 0000000..7bc6788 --- /dev/null +++ b/.docker/migrations/Dockerfile @@ -0,0 +1,3 @@ +FROM arigaio/atlas:latest + +COPY migrations /migrations \ No newline at end of file diff --git a/.docker/migrations/Dockerfile.dev b/.docker/migrations/Dockerfile.dev new file mode 100644 index 0000000..7bc6788 --- /dev/null +++ b/.docker/migrations/Dockerfile.dev @@ -0,0 +1,3 @@ +FROM arigaio/atlas:latest + +COPY migrations /migrations \ No newline at end of file diff --git a/.docker/postgres/init/dbinit.sh b/.docker/postgres/init/dbinit.sh new file mode 100755 index 0000000..4cac3c5 --- /dev/null +++ b/.docker/postgres/init/dbinit.sh @@ -0,0 +1,7 @@ +#!/bin/bash +set -e +export PGPASSWORD=postgres; +psql -v ON_ERROR_STOP=1 --username "postgres" <<-EOSQL + CREATE DATABASE whisperbot; + GRANT ALL PRIVILEGES ON DATABASE whisperbot TO "postgres"; +EOSQL \ No newline at end of file diff --git a/.gitignore b/.gitignore index 2597719..c0e06a6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /.idea /.vscode -__debug_* \ No newline at end of file +__debug_* +bot_dev.env +/.docker/db \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bb5b450 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +up: + @docker-compose --file .docker/docker-compose.dev.yml up --build -d --remove-orphans + +down: + @docker-compose --file .docker/docker-compose.dev.yml down \ No newline at end of file diff --git a/migrations/.gitkeep b/migrations/.gitkeep new file mode 100644 index 0000000..e69de29