From 8de1b5b8670c5eab9cde5ca5a522df304b68096e Mon Sep 17 00:00:00 2001 From: Boonyarit Rousamran Date: Thu, 27 Jun 2024 12:58:11 +0700 Subject: [PATCH] add docker file --- Dockerfile | 28 ++++++++++++++++++++++++++++ docker-compose/go-groceries-api.yml | 18 ++++++++++++++++++ docker-compose/mysql.yml | 6 +++--- pkg/env/env.go | 3 ++- 4 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose/go-groceries-api.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f2cc663 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,28 @@ +FROM golang:1.22-alpine3.19 AS builder + +RUN apk update && \ + apk add --no-cache \ + git + +WORKDIR /app + +COPY go.mod go.sum ./ +COPY . ./ + +RUN CGO_ENABLED=0 GOOS=linux go build -ldflags '' -a -installsuffix cgo -o go-groceries-api ./cmd/api + +FROM alpine:latest + +RUN apk update && \ + apk add --no-cache ca-certificates && \ + apk add --no-cache tzdata + +ENV TZ=Asia/Bangkok + +WORKDIR /root/ + +COPY --from=builder /app/go-groceries-api /root/api/main + +EXPOSE 8081 + +ENTRYPOINT ["./api/main"] diff --git a/docker-compose/go-groceries-api.yml b/docker-compose/go-groceries-api.yml new file mode 100644 index 0000000..5ee7f8a --- /dev/null +++ b/docker-compose/go-groceries-api.yml @@ -0,0 +1,18 @@ +version: '3.1' +services: + go-groceries-api: + image: go-groceries + container_name: go-groceries-api + restart: always + environment: + APP_NAME: groceries_api + APP_MYSQL_HOST: mysql + APP_MYSQL_PORT: 3306 + APP_MYSQL_DB_NAME: grocery + APP_MYSQL_USERNAME: root + APP_MYSQL_PASSWORD: root + APP_SRV_SERVER_ADDR: :8081 + ports: + - "8081:8081" + +## docker compose -f [FILE_NAME] up diff --git a/docker-compose/mysql.yml b/docker-compose/mysql.yml index 910596b..efd10a3 100644 --- a/docker-compose/mysql.yml +++ b/docker-compose/mysql.yml @@ -9,12 +9,12 @@ services: MYSQL_PASSWORD: root MYSQL_ROOT_PASSWORD: root ports: - - 3306:3306 + - "3306:3306" phpmyadmin: image: phpmyadmin ports: - - 8080:80 + - "8080:80" depends_on: - db -## docker compose -f [FILE_NAME] up \ No newline at end of file +## docker compose -f [FILE_NAME] up diff --git a/pkg/env/env.go b/pkg/env/env.go index ecb800a..ffca855 100644 --- a/pkg/env/env.go +++ b/pkg/env/env.go @@ -1,12 +1,13 @@ package env import ( + "fmt" "github.com/joho/godotenv" ) func MustLoadEnv() { err := godotenv.Load() if err != nil { - panic(err) + fmt.Println("cannot find .env file") } }