Skip to content

Commit 5f1dfa3

Browse files
committed
feat: modify Docker
1 parent 1b78f69 commit 5f1dfa3

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

Dockerfile

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
1-
FROM golang:1.18-alpine
1+
FROM golang:1.18-alpine as builder
22

33
LABEL maintainer="Putu Krisna Andyartha"
44

55
RUN apk update && apk add --no-cache git
66

7-
WORKDIR /go/src/app
7+
WORKDIR /app
8+
9+
COPY go.mod go.sum ./
10+
11+
RUN go mod download
12+
813
COPY . .
914

10-
RUN go get -d -v ./...
11-
RUN go install -v ./...
12-
RUN go build -o /bin/myapp
15+
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
16+
17+
FROM golang:1.18-alpine
18+
RUN apk --no-cache add ca-certificates
19+
20+
WORKDIR /root/
21+
22+
COPY --from=builder /app/main .
23+
COPY --from=builder /app/.env .
24+
25+
EXPOSE 8080
1326

14-
CMD [ "/bin/myapp" ]
27+
CMD ["./main"]

config/database-config.go

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ func SetupDatabaseConnection() *gorm.DB {
2020
dbUser := os.Getenv("DB_USER")
2121
dbPass := os.Getenv("DB_PASS")
2222
dbHost := os.Getenv("DB_HOST")
23+
24+
print(dbHost)
25+
2326
dbName := os.Getenv("DB_NAME")
2427

2528
dsn := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=utf8mb4&parseTime=True&loc=Local", dbUser, dbPass, dbHost, dbName)

docker-compose.yml

+30-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
version: '3.8'
22
services:
3+
app:
4+
container_name: golang-api
5+
build: .
6+
ports:
7+
- 8080:8080
8+
restart: on-failure
9+
volumes:
10+
- ./api:/usr/src/app/
11+
depends_on:
12+
- db
13+
networks:
14+
- go-app
15+
316
db:
417
image: mariadb
518
environment:
@@ -9,6 +22,9 @@ services:
922
- 3306:3306
1023
volumes:
1124
- ./database:/var/lib/mysql
25+
networks:
26+
- go-app
27+
1228
phpmyadmin:
1329
image: phpmyadmin/phpmyadmin
1430
container_name: phymyadmin
@@ -22,6 +38,9 @@ services:
2238
- /sessions
2339
links:
2440
- db
41+
networks:
42+
- go-app
43+
2544
portainer:
2645
image: portainer/portainer-ce:latest
2746
container_name: portainer
@@ -31,11 +50,20 @@ services:
3150
- /var/run/docker.sock:/var/run/docker.sock:ro
3251
- ./portainer-data:/data
3352
ports:
34-
- 9000:9000
53+
- 9000:9000
54+
networks:
55+
- go-app
56+
3557
cache:
3658
image: redis:alpine
3759
restart: always
3860
volumes:
3961
- ./redis-data:/data
4062
ports:
41-
- 6379:6379
63+
- 6379:6379
64+
networks:
65+
- go-app
66+
67+
networks:
68+
go-app:
69+
driver: bridge

0 commit comments

Comments
 (0)