Skip to content

Commit f2d09ef

Browse files
committed
Dockerize the api
1 parent ee5b350 commit f2d09ef

File tree

6 files changed

+118
-9
lines changed

6 files changed

+118
-9
lines changed

.env

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Postgres Live
2-
DB_HOST=127.0.0.1
2+
DB_HOST=postgres
33
DB_DRIVER=postgres
4-
API_SECRET=98hbun98h #This is used when creating a JWT. It can be anything you want
4+
API_SECRET=98hbun98h #Used for creating a JWT. Can be anything
55
DB_USER=steven
66
DB_PASSWORD=
77
DB_NAME=fullstack_api
88
DB_PORT=5432 #Default postgres port
99

1010
# Postgres Test
11-
TestDbHost=127.0.0.1
11+
TestDbHost=postgres_test
1212
TestDbDriver=postgres
1313
TestApiSecret=98hbun98h
1414
TestDbUser=steven
@@ -17,16 +17,16 @@ TestDbName=fullstack_api_test
1717
TestDbPort=5432
1818

1919
# Mysql Live
20-
# DB_HOST=127.0.0.1
20+
# DB_HOST=mysql
2121
# DB_DRIVER=mysql
22-
# API_SECRET=98hbun98h #This is used when creating a JWT. It can be anything you want
22+
# API_SECRET=98hbun98h #Used for creating a JWT. Can be anything
2323
# DB_USER=steven
2424
# DB_PASSWORD=here
25-
# DB_NAME=fullstack_api
25+
# DB_NAME=fullstack_mysql
2626
# DB_PORT=3306 #Default mysql port
2727

28-
# Mysql Test
29-
# TestDbHost=127.0.0.1
28+
# # Mysql Test
29+
# TestDbHost=mysql_test
3030
# TestDbDriver=mysql
3131
# TestApiSecret=98hbun98h
3232
# TestDbUser=steven

Dockerfile

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

api/server.go

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ import (
1212

1313
var server = controllers.Server{}
1414

15+
func init() {
16+
// loads values from .env into the system
17+
if err := godotenv.Load(); err != nil {
18+
log.Print("sad .env file found")
19+
}
20+
}
21+
1522
func Run() {
1623

1724
var err error

docker-compose.yml

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
version: '3'
2+
services:
3+
app:
4+
container_name: full_app
5+
build: .
6+
ports:
7+
- 8080:8080 # Forward the exposed port 8080 on the container to port 8080 on the host machine
8+
restart: on-failure
9+
volumes:
10+
- api:/usr/src/app/
11+
depends_on:
12+
- postgres # This service depends on postgres. Start that first.
13+
# - mysql # This service depends on mysql. Start that first.
14+
networks:
15+
- fullstack
16+
17+
postgres:
18+
image: postgres:latest
19+
container_name: full_db
20+
environment:
21+
- POSTGRES_USER=${DB_USER}
22+
- POSTGRES_PASSWORD=${DB_PASSWORD}
23+
- POSTGRES_DB=${DB_NAME}
24+
- DATABASE_HOST=${DB_HOST}
25+
ports:
26+
- '5432:5432'
27+
volumes:
28+
- database_postgres:/var/lib/postgresql/data
29+
networks:
30+
- fullstack
31+
32+
# mysql:
33+
# image: mysql:5.7
34+
# ports:
35+
# - 3306:3306
36+
# environment:
37+
# - MYSQL_DATABASE=${DB_NAME}
38+
# - MYSQL_USER=${DB_USER}
39+
# - MYSQL_PASSWORD=${DB_PASSWORD}
40+
# - MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
41+
# - DATABASE_HOST=${DB_HOST}
42+
# volumes:
43+
# - database_mysql:/var/lib/postgresql/data
44+
# networks:
45+
# - fullstack
46+
47+
volumes:
48+
api:
49+
database_postgres:
50+
51+
# Networks to be created to facilitate communication between containers
52+
networks:
53+
fullstack:
54+

fullstack

11.2 MB
Binary file not shown.

main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package main
22

3-
import "github.com/victorsteven/fullstack/api"
3+
import (
4+
"github.com/victorsteven/fullstack/api"
5+
)
46

57
func main() {
8+
69
api.Run()
10+
711
}

0 commit comments

Comments
 (0)