Skip to content

Commit

Permalink
add deployment with docker compose
Browse files Browse the repository at this point in the history
  • Loading branch information
rizquuula committed Nov 4, 2024
1 parent b316468 commit 3bddd77
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 5 deletions.
20 changes: 20 additions & 0 deletions Dockerfile.core
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# docker build -f Dockerfile.core -t einote:core-0.1.0 .

# Stage 1: Build the Go application
FROM golang:1.23 AS builder

WORKDIR /app
COPY core/go.mod core/go.sum ./
RUN go mod download
COPY core/ .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o einote cmd/app/main.go

# Stage 2: Run the Go application
FROM alpine:latest

COPY --from=builder /app/einote /einote
RUN touch .env

EXPOSE 8080

CMD ["/einote"]
36 changes: 36 additions & 0 deletions Dockerfile.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# docker build -f Dockerfile.ui -t einote:ui-0.1.0 .

# build fe
FROM node:18-alpine AS build_stage

# Install additional dependencies for Node.js on Alpine
RUN apk add --no-cache libc6-compat

WORKDIR /fe

COPY ./ui/package.json \
./ui/package-lock.json \
./ui/postcss.config.js \
./ui/tailwind.config.js \
./ui/tsconfig.json \
./

COPY ./ui/src ./src
COPY ./ui/public ./public

RUN npm install --loglevel info

# backend api will be proxied in nginx
ENV REACT_APP_BACKEND_API=http://localhost:4848/api

RUN npm run build --loglevel info

# build nginx to serve FE
FROM nginx:alpine3.19-slim

COPY --from=build_stage /fe/build /usr/share/nginx/html

COPY ./nginx.fe.conf /etc/nginx/conf.d/default.conf

EXPOSE 3000
CMD ["nginx", "-g", "daemon off;"]
9 changes: 9 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export VERSION=0.1.0

buildUiCmd="docker build -f Dockerfile.ui -t einote:ui-$VERSION ."
echo "##### RUNNING: $buildUiCmd"
eval $buildUiCmd

buildCoreCmd="docker build -f Dockerfile.core -t einote:core-$VERSION ."
echo "##### RUNNING: $buildCoreCmd"
eval $buildCoreCmd
2 changes: 1 addition & 1 deletion core/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module core

go 1.23.0
go 1.23

require (
github.com/Masterminds/squirrel v1.5.4
Expand Down
3 changes: 3 additions & 0 deletions core/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
github.com/Masterminds/squirrel v1.5.4 h1:uUcX/aBc8O7Fg9kaISIUsHXdKuqehiXAMQTYX8afzqM=
github.com/Masterminds/squirrel v1.5.4/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand All @@ -13,7 +14,9 @@ github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhR
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6FmdpVm2joNMFikkuWg0EoCKLGUMNw=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
4 changes: 4 additions & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Run database migration
# migrate -database postgres://root:root@localhost:7432/einote?sslmode=disable -path ./migrations up

docker compose up -d --force-recreate
48 changes: 47 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,51 @@ services:
interval: 30s
timeout: 10s
retries: 5
# ports:
# - "7432:5432"

einote-be:
image: einote:core-0.1.0
container_name: einote-be
restart: unless-stopped
environment:
PORT: 8080
DB_USER: root
DB_PASSWORD: root
DB_NAME: einote
DB_HOST: einote-db
DB_PORT: 5432
TABLE_NOTE: note
TABLE_NOTEBOOK: notebook
# ports:
# - 8080:8080
networks:
- default
depends_on:
- einote-db

einote-fe:
image: einote:ui-0.1.0
container_name: einote-fe
restart: unless-stopped
# ports:
# - 3000:3000
volumes:
- ./nginx.fe.conf:/etc/nginx/conf.d/default.conf:ro
networks:
- default
depends_on:
- einote-be

einote-proxy:
image: nginx:alpine3.19-slim
container_name: einote-px
restart: unless-stopped
ports:
- "7432:5432"
- 4848:80 # changing port? don't forget adjust REACT_APP_BACKEND_API in Dockerfile.ui
networks:
- default
depends_on:
- einote-fe
volumes:
- ./nginx.main.conf:/etc/nginx/conf.d/default.conf
13 changes: 13 additions & 0 deletions nginx.fe.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
server {
listen 3000;
listen [::]:3000;
server_name localhost;

access_log /var/log/nginx/host.access.log main;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
20 changes: 20 additions & 0 deletions nginx.main.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
server {
listen 80;
listen [::]:80;

# listen 443 ssl;

server_name 0.0.0.0;

access_log /var/log/nginx/host.access.log main;

# ssl_certificate /ssl/certificate_merge.crt;
# ssl_certificate_key /ssl/private.key;

location / {
proxy_pass http://einote-fe:3000;
}
location /api/ {
proxy_pass http://einote-be:8080/;
}
}
1 change: 1 addition & 0 deletions ui/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REACT_APP_BACKEND_API=
2 changes: 1 addition & 1 deletion ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function App() {
const webTheme = localStorage.getItem("theme")

return (
<div className={webTheme || ''}>
<div className={webTheme || 'dark'}>
<Provider />
</div>
);
Expand Down
5 changes: 3 additions & 2 deletions ui/src/core/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import NoteAPI from "./api/note";


function Provider() {
const notebookApi = new NotebookAPI("http://localhost:2001", false)
const noteApi = new NoteAPI("http://localhost:2001", false)
const apiHost = process.env.REACT_APP_BACKEND_API ? process.env.REACT_APP_BACKEND_API : ""
const notebookApi = new NotebookAPI(apiHost, false)
const noteApi = new NoteAPI(apiHost, false)
return (
<BrowserRouter>
<div className='bg-white dark:bg-black text-black dark:text-white'>
Expand Down

0 comments on commit 3bddd77

Please sign in to comment.