From 7ac015801a458f23da96795ac5da56b20ac63156 Mon Sep 17 00:00:00 2001 From: Vitaliy Turov Date: Thu, 20 Mar 2025 07:14:14 +0300 Subject: [PATCH] Save state --- .dockerignore | 59 +++++++++++++++++++++++++++++++ Dockerfile | 29 +++++++++++++++ README.md | 35 ++++++++++++++++++ compose.yaml | 20 +++++++++++ deploy/postgresql-deployment.yaml | 58 ++++++++++++++++++++++++++++++ 5 files changed, 201 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 compose.yaml create mode 100644 deploy/postgresql-deployment.yaml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..6c809df --- /dev/null +++ b/.dockerignore @@ -0,0 +1,59 @@ +# .dockerignore file for the project + +# Language-specific patterns +/vendor/ +*.test +.go-cache + +# Development artifacts +.idea/ +.vscode/ +*.swp +*.swo +dist/ +build/ +out/ +test/ +tests/ +*_test.go +debug/ +*.log + +# Version control +.git/ +.gitignore + +# Environment and secrets +.env* +*.env +*.pem +*.key +*.crt +config.local.* +*.local.yml + +# Project-specific patterns +docs/ +*.md +README* +Dockerfile* +docker-compose* +tmp/ +temp/ +*.tmp +.local/ +local/ + +# Exclude the following files from being ignored +!go.mod +!go.sum +!cmd/main.go +!internal/config/config.go +!internal/domain/tasks/task.go +!internal/domain/users/user.go +!internal/persistance/task_manager.go +!internal/persistance/task_repository.go +!internal/persistance/user_repository.go +!database/init/00-users-init.sql +!database/init/01-tasks-init.sql +!deployments/task-manager.yaml \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..06a0905 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +# syntax=docker/dockerfile:1.5 + +# Build stage +ARG GO_VERSION=1.24 +FROM golang:${GO_VERSION} AS builder + +# Set the working directory +WORKDIR /app + +# Cache dependencies and build the application +RUN --mount=type=cache,target=/go/pkg/mod \ + --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=bind,target=. \ + CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /task-manager ./cmd/main.go + +# Final stage +FROM scratch AS final + +# Set the working directory +WORKDIR /app + +# Copy the built binary from the builder stage +COPY --from=builder /task-manager ./ + +# Expose the application port +EXPOSE 8080 + +# Set the default command to run the application +CMD ["./task-manager"] \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..bc6b8dd --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +# Running the Project with Docker + +This section provides instructions to build and run the project using Docker. + +## Requirements + +- Docker version 20.10 or later +- Docker Compose version 1.29 or later + +## Environment Variables + +- `POSTGRES_USER`: Database username (default: `user`) +- `POSTGRES_PASSWORD`: Database password (default: `password`) +- `POSTGRES_DB`: Database name (default: `appdb`) + +## Build and Run Instructions + +1. Clone the repository and navigate to the project root directory. +2. Build and start the services using Docker Compose: + ```bash + docker-compose up --build + ``` +3. Access the application at `http://localhost:8080`. + +## Configuration + +- The application binary is built using Go version 1.24. +- The database service uses the `postgres:latest` image. + +## Exposed Ports + +- Application: `8080` (mapped to `8080` on the host) +- Database: Not exposed to the host + +For further details, refer to the project documentation or contact the maintainers. \ No newline at end of file diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..ff3fa9e --- /dev/null +++ b/compose.yaml @@ -0,0 +1,20 @@ +services: + app: + build: + context: . + ports: + - "8080:8080" + restart: unless-stopped + depends_on: + - database + database: + image: postgres:latest + environment: + POSTGRES_USER: user + POSTGRES_PASSWORD: password + POSTGRES_DB: appdb + ports: + - "5432:5432" + volumes: + - ./database/init:/docker-entrypoint-initdb.d:ro + restart: unless-stopped \ No newline at end of file diff --git a/deploy/postgresql-deployment.yaml b/deploy/postgresql-deployment.yaml new file mode 100644 index 0000000..c0299de --- /dev/null +++ b/deploy/postgresql-deployment.yaml @@ -0,0 +1,58 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: postgres-pvc +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 1Gi + +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: postgres-deployment +spec: + replicas: 1 + selector: + matchLabels: + app: postgres + template: + metadata: + labels: + app: postgres + spec: + containers: + - name: postgres + image: postgres:latest + ports: + - containerPort: 5432 + env: + - name: POSTGRES_DB + value: mydatabase + - name: POSTGRES_USER + value: myuser + - name: POSTGRES_PASSWORD + value: mypassword + volumeMounts: + - name: postgres-storage + mountPath: /var/lib/postgresql/data + volumes: + - name: postgres-storage + persistentVolumeClaim: + claimName: postgres-pvc + +--- +apiVersion: v1 +kind: Service +metadata: + name: postgres-service +spec: + type: ClusterIP + ports: + - port: 5432 + targetPort: 5432 + selector: + app: postgres \ No newline at end of file