Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Postgres
DB_HOST=db-postgres:5432
DB_USER=postgres
DB_PASSWORD=postgres
DB_NAME=ms_gin_go
40 changes: 40 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Start from golang base image
FROM golang:alpine as builder

# Add Maintainer info
LABEL maintainer="Lairon Acosta <lairon14@gmail.com>"

# 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"]
36 changes: 36 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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:
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down