Skip to content

Commit

Permalink
Add Dockerfile and docker-compose.yml for Docker setup
Browse files Browse the repository at this point in the history
  • Loading branch information
heedaeshin committed Aug 21, 2024
1 parent 482599a commit 33b32ea
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
UID=1000
GID=1000
USER_NAME=ubuntu
GROUP_NAME=ubuntu
41 changes: 41 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
##############################################################
## Stage 1 - Go Build
##############################################################

FROM golang:latest AS builder
WORKDIR /opt
COPY . .
RUN go build -o app .

#############################################################
## Stage 2 - Application Setup
##############################################################
FROM ubuntu:22.04
ARG UID=0
ARG GID=0
ARG USER=root
ARG GROUP=root
#-------------------------------------------------------------
RUN apt-get update
RUN apt-get install ca-certificates -y
#-------------------------------------------------------------
# User Set
RUN if [ "${USER}" != "root" ]; then \
groupadd -g ${GID} ${GROUP} && \
useradd -m -u ${UID} -g ${GID} -s /bin/bash ${USER}; \
fi
#-------------------------------------------------------------
# Copy App and Web
RUN mkdir -p /app/log
RUN chown -R ${USER}:${GROUP} /app
USER ${USER}
COPY --from=builder --chown=${USER}:${GROUP} /opt/app /app/app
COPY --from=builder --chown=${USER}:${GROUP} /opt/web /app/web
#-------------------------------------------------------------
# Add entrypoint script
COPY --chown=${USER}:${GROUP} entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh
#-------------------------------------------------------------
WORKDIR /app
EXPOSE 3300
ENTRYPOINT ["/app/entrypoint.sh"]
41 changes: 41 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
services:
app:
image: m-cmp/data-manager:latest
container_name: mcmp-dm-web
build:
context: .
dockerfile: Dockerfile
args:
UID: ${UID:-0}
GID: ${GID:-0}
USER: ${USER_NAME:-root}
GROUP: ${GROUP_NAME:-root}
WEB_DIR : ${WEB_DIR:-/web}
APP_HOME: /app
tty: true
ports:
- "3300:3300"
restart : always
volumes:
- ./log:/app/log/
env_file:
- .env
##################
## OPTIONAL ##
##################
## OPTIONAL LOGGING SETTINGS
# logging:
# driver: "json-file"
# options:
# max-size: "5m" # 로그 파일 최대 사이즈
# max-file: "10" # 로그 파일 최대 수

## OPTIONAL RESOURCE LIMITS
# deploy:
# resources:
# limits:
# cpus: "0.50" # 최대 CPU 사용량: 0.5 (50% of a single CPU core)
# memory: "512M" # 최대 메모리 사용량: 512MB
# reservations:
# cpus: "0.25" # 예약된 최소 CPU 사용량: 0.25 (25% of a single CPU core)
# memory: "256M" # 예약된 최소 메모리 사용량: 256MB
3 changes: 3 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
# Execute the main application
exec /app/app server --allow-ip 0.0.0.0/0 --port 3300
25 changes: 23 additions & 2 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,38 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/sirupsen/logrus"
)

func LogFile() {
logFile, err := os.OpenFile("./datamold.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, os.FileMode(0644))
execPath, err := os.Executable()
fmt.Println(execPath)
if err != nil {
logrus.WithError(err).Fatal("Failed to create log file")
logrus.WithError(err).Fatal("Failed to get executable path")
}

// 바이너리 파일의 디렉토리 경로 가져오기
execDir := filepath.Dir(execPath)

// 로그 디렉토리 경로 설정
logDir := filepath.Join(execDir, "log")

// 로그 디렉토리 생성
if err := os.MkdirAll(logDir, os.ModePerm); err != nil {
logrus.WithError(err).Fatal("Failed to create log directory")
}

// 로그 파일 경로 설정
logFilePath := filepath.Join(logDir, "data-manager.log")

// 로그 파일 열기 또는 생성
logFile, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_APPEND|os.O_RDWR, os.FileMode(0644))
if err != nil {
logrus.WithError(err).Fatal("Failed to create log file")
}
logrus.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&CustomTextFormatter{})
logrus.SetOutput(io.MultiWriter(os.Stdout, logFile))
Expand Down

0 comments on commit 33b32ea

Please sign in to comment.