Skip to content

Commit 700f9cc

Browse files
Merge pull request #4 from laironacosta/feature/dockerizer
feat: dockerizing go app and using pg embed database
2 parents 515eae1 + 81613e1 commit 700f9cc

File tree

4 files changed

+86
-3
lines changed

4 files changed

+86
-3
lines changed

.env

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Postgres
2+
DB_HOST=db-postgres:5432
3+
DB_USER=postgres
4+
DB_PASSWORD=postgres
5+
DB_NAME=ms_gin_go

Dockerfile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Start from golang base image
2+
FROM golang:alpine as builder
3+
4+
# Add Maintainer info
5+
LABEL maintainer="Lairon Acosta <lairon14@gmail.com>"
6+
7+
# Install git.
8+
# Git is required for fetching the dependencies.
9+
RUN apk update && apk add --no-cache git
10+
11+
# Set the current working directory inside the container
12+
WORKDIR /app
13+
14+
# Copy go mod and sum files
15+
COPY go.mod go.sum ./
16+
17+
# Download all dependencies. Dependencies will be cached if the go.mod and the go.sum files are not changed
18+
RUN go mod download
19+
20+
# Copy the source from the current directory to the working Directory inside the container
21+
COPY . .
22+
23+
# Build the Go app
24+
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
25+
26+
# Start a new stage from scratch
27+
FROM alpine:latest
28+
RUN apk --no-cache add ca-certificates
29+
30+
WORKDIR /root/
31+
32+
# Copy the Pre-built binary file from the previous stage. Observe we also copied the .env file
33+
COPY --from=builder /app/main .
34+
COPY --from=builder /app/.env .
35+
36+
# Expose port 8080 to the outside world
37+
EXPOSE 8080
38+
39+
#Command to run the executable
40+
CMD ["./main"]

docker-compose.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
version: '3'
2+
networks:
3+
ms-go-network:
4+
driver: bridge
5+
services:
6+
db-postgres:
7+
image: postgres:latest
8+
container_name: full_db_postgres
9+
environment:
10+
- POSTGRES_USER=${DB_USER}
11+
- POSTGRES_PASSWORD=${DB_PASSWORD}
12+
- POSTGRES_DB=${DB_NAME}
13+
ports:
14+
- '5432:5432'
15+
restart: on-failure
16+
volumes:
17+
- database_postgres:/var/lib/postgresql/data
18+
networks:
19+
- ms-go-network
20+
app:
21+
container_name: full_app
22+
build: .
23+
ports:
24+
- 8080:8080
25+
restart: on-failure
26+
volumes:
27+
- api:/usr/src/app/
28+
env_file:
29+
- .env
30+
depends_on:
31+
- db-postgres
32+
networks:
33+
- ms-go-network
34+
volumes:
35+
api:
36+
database_postgres:

main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ import (
88
repo "github.com/laironacosta/ms-gin-go/repository"
99
"github.com/laironacosta/ms-gin-go/router"
1010
"github.com/laironacosta/ms-gin-go/services"
11+
"os"
1112
)
1213

1314
func main() {
1415
gin := gin.Default()
1516

1617
db := pgdb.NewPgDB(&pg.Options{
17-
User: "postgres",
18-
Password: "postgres",
19-
Database: "pg-db-go",
18+
Addr: os.Getenv("DB_HOST"),
19+
User: os.Getenv("DB_USER"),
20+
Password: os.Getenv("DB_PASSWORD"),
21+
Database: os.Getenv("DB_NAME"),
2022
})
2123

2224
userRepo := repo.NewUserRepository(db)

0 commit comments

Comments
 (0)