diff --git a/.env b/.env new file mode 100644 index 0000000..7742721 --- /dev/null +++ b/.env @@ -0,0 +1,5 @@ +# Postgres +DB_HOST=db-postgres:5432 +DB_USER=postgres +DB_PASSWORD=postgres +DB_NAME=ms_gin_go \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..494cd88 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,40 @@ +# Start from golang base image +FROM golang:alpine as builder + +# Add Maintainer info +LABEL maintainer="Lairon Acosta " + +# Install git. +# Git is required for fetching the dependencies. +RUN apk update && apk add --no-cache git + +# Set the current working directory inside the container +WORKDIR /app + +# Copy go mod and sum files +COPY go.mod go.sum ./ + +# Download all dependencies. Dependencies will be cached if the go.mod and the go.sum files are not changed +RUN go mod download + +# Copy the source from the current directory to the working Directory inside the container +COPY . . + +# Build the Go app +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main . + +# Start a new stage from scratch +FROM alpine:latest +RUN apk --no-cache add ca-certificates + +WORKDIR /root/ + +# Copy the Pre-built binary file from the previous stage. Observe we also copied the .env file +COPY --from=builder /app/main . +COPY --from=builder /app/.env . + +# Expose port 8080 to the outside world +EXPOSE 8080 + +#Command to run the executable +CMD ["./main"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..a52e2bc --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,36 @@ +version: '3' +networks: + ms-go-network: + driver: bridge +services: + db-postgres: + image: postgres:latest + container_name: full_db_postgres + environment: + - POSTGRES_USER=${DB_USER} + - POSTGRES_PASSWORD=${DB_PASSWORD} + - POSTGRES_DB=${DB_NAME} + ports: + - '5432:5432' + restart: on-failure + volumes: + - database_postgres:/var/lib/postgresql/data + networks: + - ms-go-network + app: + container_name: full_app + build: . + ports: + - 8080:8080 + restart: on-failure + volumes: + - api:/usr/src/app/ + env_file: + - .env + depends_on: + - db-postgres + networks: + - ms-go-network +volumes: + api: + database_postgres: \ No newline at end of file diff --git a/main.go b/main.go index d9de76c..d6c32c1 100644 --- a/main.go +++ b/main.go @@ -8,15 +8,17 @@ import ( repo "github.com/laironacosta/ms-gin-go/repository" "github.com/laironacosta/ms-gin-go/router" "github.com/laironacosta/ms-gin-go/services" + "os" ) func main() { gin := gin.Default() db := pgdb.NewPgDB(&pg.Options{ - User: "postgres", - Password: "postgres", - Database: "pg-db-go", + Addr: os.Getenv("DB_HOST"), + User: os.Getenv("DB_USER"), + Password: os.Getenv("DB_PASSWORD"), + Database: os.Getenv("DB_NAME"), }) userRepo := repo.NewUserRepository(db)