chore:update docker
This commit is contained in:
parent
02c6a15607
commit
f8957e0d95
32
Dockerfile
32
Dockerfile
@ -1,6 +1,4 @@
|
|||||||
# syntax=docker/dockerfile:1.4
|
# Dockerfile
|
||||||
# Production-ready multi-stage build for server deployment
|
|
||||||
|
|
||||||
# Build arguments
|
# Build arguments
|
||||||
ARG GO_VERSION=1.24.3
|
ARG GO_VERSION=1.24.3
|
||||||
ARG ALPINE_VERSION=3.19
|
ARG ALPINE_VERSION=3.19
|
||||||
@ -16,7 +14,7 @@ RUN apk add --no-cache git make gcc libc-dev
|
|||||||
# Set working directory
|
# Set working directory
|
||||||
WORKDIR /build
|
WORKDIR /build
|
||||||
|
|
||||||
# Enable Go modules
|
# Enable Go modules and set build environment variables
|
||||||
ENV GO111MODULE=on \
|
ENV GO111MODULE=on \
|
||||||
CGO_ENABLED=0 \
|
CGO_ENABLED=0 \
|
||||||
GOOS=linux \
|
GOOS=linux \
|
||||||
@ -25,37 +23,35 @@ ENV GO111MODULE=on \
|
|||||||
# Copy dependency files first for better layer caching
|
# Copy dependency files first for better layer caching
|
||||||
COPY go.mod go.sum ./
|
COPY go.mod go.sum ./
|
||||||
|
|
||||||
|
# Download dependencies using cache mount for efficiency
|
||||||
# Download dependencies
|
|
||||||
RUN --mount=type=cache,target=/go/pkg/mod \
|
RUN --mount=type=cache,target=/go/pkg/mod \
|
||||||
go mod download
|
go mod download
|
||||||
|
|
||||||
# Copy source code
|
# Copy source code
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
|
# Create the configs directory in the build context (if it exists)
|
||||||
|
RUN if test -d "configs"; then mkdir -p /build/configs && cp -r configs/* /build/configs/; fi
|
||||||
|
|
||||||
# Create the configs directory in the build context
|
# Build the application with optimizations and version info
|
||||||
RUN mkdir -p /build/configs && \
|
|
||||||
cp -r configs/* /build/configs/ 2>/dev/null || :
|
|
||||||
|
|
||||||
# Build the application with optimizations
|
|
||||||
RUN --mount=type=cache,target=/root/.cache/go-build \
|
RUN --mount=type=cache,target=/root/.cache/go-build \
|
||||||
go build -ldflags="-w -s -X 'main.Version=$(git describe --tags --always 2>/dev/null || echo 'dev')'" \
|
go build -ldflags="-w -s -X 'main.Version=$(git describe --tags --always 2>/dev/null || echo 'dev')'" \
|
||||||
-o /build/bin/app ./cmd/app
|
-o /build/bin/app ./src/cmd/api # Update path to your main package
|
||||||
|
|
||||||
# --- Final Stage ---
|
# --- Final Stage ---
|
||||||
FROM alpine:${ALPINE_VERSION}
|
FROM alpine:${ALPINE_VERSION}
|
||||||
|
|
||||||
# Re-declare ARG to use in this stage
|
# Re-declare ARG to use in this stage
|
||||||
ARG APP_USER=appuser
|
ARG APP_USER
|
||||||
ARG APP_GROUP=appgroup
|
ARG APP_GROUP
|
||||||
ARG APP_HOME=/app
|
ARG APP_HOME=/app
|
||||||
|
|
||||||
# Set environment variables
|
# Set runtime environment variables
|
||||||
ENV TZ=Asia/Ho_Chi_Minh \
|
ENV TZ=Asia/Ho_Chi_Minh \
|
||||||
APP_USER=${APP_USER} \
|
APP_USER=${APP_USER} \
|
||||||
APP_GROUP=${APP_GROUP} \
|
APP_GROUP=${APP_GROUP} \
|
||||||
APP_HOME=${APP_HOME}
|
APP_HOME=${APP_HOME} \
|
||||||
|
APP_ENV=production
|
||||||
|
|
||||||
# Install runtime dependencies
|
# Install runtime dependencies
|
||||||
RUN apk add --no-cache \
|
RUN apk add --no-cache \
|
||||||
@ -68,7 +64,7 @@ RUN apk add --no-cache \
|
|||||||
RUN addgroup -S ${APP_GROUP} && \
|
RUN addgroup -S ${APP_GROUP} && \
|
||||||
adduser -S -G ${APP_GROUP} -h ${APP_HOME} -D ${APP_USER}
|
adduser -S -G ${APP_GROUP} -h ${APP_HOME} -D ${APP_USER}
|
||||||
|
|
||||||
# Create necessary directories
|
# Create necessary application directories
|
||||||
RUN mkdir -p ${APP_HOME}/configs ${APP_HOME}/logs ${APP_HOME}/storage
|
RUN mkdir -p ${APP_HOME}/configs ${APP_HOME}/logs ${APP_HOME}/storage
|
||||||
|
|
||||||
# Switch to app directory
|
# Switch to app directory
|
||||||
@ -99,4 +95,4 @@ EXPOSE 3000
|
|||||||
ENV APP_ENV=production
|
ENV APP_ENV=production
|
||||||
|
|
||||||
# Command to run the application
|
# Command to run the application
|
||||||
ENTRYPOINT ["/sbin/tini", "--"]
|
ENTRYPOINT ["/sbin/tini", "--"]
|
||||||
12
Makefile
12
Makefile
@ -86,7 +86,10 @@ clean:
|
|||||||
docker-build:
|
docker-build:
|
||||||
@echo "Building Docker image..."
|
@echo "Building Docker image..."
|
||||||
docker build -t ulflow-zee:latest .
|
docker build -t ulflow-zee:latest .
|
||||||
|
# Build Docker image for development
|
||||||
|
docker-build-dev:
|
||||||
|
@echo "Building Docker image for development..."
|
||||||
|
docker-compose -f docker-compose.dev.yml up --build
|
||||||
# Run application in Docker container
|
# Run application in Docker container
|
||||||
docker-run:
|
docker-run:
|
||||||
@echo "Running application in Docker container..."
|
@echo "Running application in Docker container..."
|
||||||
@ -97,6 +100,13 @@ docker-run:
|
|||||||
docker run -p 3000:3000 --env-file .env ulflow-zee:latest
|
docker run -p 3000:3000 --env-file .env ulflow-zee:latest
|
||||||
)
|
)
|
||||||
|
|
||||||
|
docker-up-dev:
|
||||||
|
@echo "Starting all services with Docker Compose for local development..."
|
||||||
|
docker-compose -f docker-compose.dev.yml up
|
||||||
|
|
||||||
|
docker-down-dev:
|
||||||
|
@echo "Stopping all development services..."
|
||||||
|
docker-compose -f docker-compose.dev.yml down -v
|
||||||
# Run Docker Compose for local development
|
# Run Docker Compose for local development
|
||||||
docker-compose-up:
|
docker-compose-up:
|
||||||
@echo "Starting all services with Docker Compose for local development..."
|
@echo "Starting all services with Docker Compose for local development..."
|
||||||
|
|||||||
@ -70,6 +70,7 @@ func (s *HTTPService) Shutdown(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
fmt.Println("Starting application 222...")
|
||||||
// Initialize config loader
|
// Initialize config loader
|
||||||
configLoader := config.NewConfigLoader()
|
configLoader := config.NewConfigLoader()
|
||||||
|
|
||||||
@ -112,7 +113,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
lifecycleMgr := lifecycle.New(shutdownTimeout)
|
lifecycleMgr := lifecycle.New(shutdownTimeout)
|
||||||
|
|
||||||
var gormDB *gorm.DB // Declare gormDB for database connection
|
var gormDB *gorm.DB // Declare gormDB for database connection
|
||||||
var dbInstance *database.Database // Declare dbInstance for HTTPService and other potential users
|
var dbInstance *database.Database // Declare dbInstance for HTTPService and other potential users
|
||||||
|
|
||||||
if feature.IsEnabled(feature.EnableDatabase) {
|
if feature.IsEnabled(feature.EnableDatabase) {
|
||||||
|
|||||||
59
docker-compose.dev.yml
Normal file
59
docker-compose.dev.yml
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
# docker-compose.dev.yml (Cho Local Development)
|
||||||
|
version: '3.8'
|
||||||
|
|
||||||
|
services:
|
||||||
|
api:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile.dev # Sử dụng Dockerfile.dev
|
||||||
|
container_name: ulflow-api-dev
|
||||||
|
ports:
|
||||||
|
- "3000:3000"
|
||||||
|
volumes:
|
||||||
|
- .:/app # Bind mount code cho hot reload
|
||||||
|
- go-modules:/go/pkg/mod
|
||||||
|
command: air # Chạy công cụ hot reload
|
||||||
|
env_file:
|
||||||
|
- ./.env # Load biến môi trường từ file .env
|
||||||
|
depends_on:
|
||||||
|
- postgres
|
||||||
|
networks:
|
||||||
|
- ulflow-network
|
||||||
|
restart: "no" # Không tự động restart trong dev
|
||||||
|
|
||||||
|
postgres:
|
||||||
|
image: postgres:15-alpine
|
||||||
|
container_name: ulflow-postgres-dev
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
# Lấy biến từ file .env
|
||||||
|
POSTGRES_USER: ${DATABASE_USERNAME}
|
||||||
|
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
|
||||||
|
POSTGRES_DB: ${DATABASE_NAME}
|
||||||
|
ports:
|
||||||
|
- "5432:5432" # Để truy cập từ host nếu muốn
|
||||||
|
volumes:
|
||||||
|
- postgres-dev-data:/var/lib/postgresql/data # Volume riêng cho dev DB
|
||||||
|
networks:
|
||||||
|
- ulflow-network
|
||||||
|
|
||||||
|
adminer: # Adminer cũng nên có trong dev để quản lý DB dễ dàng
|
||||||
|
image: adminer:latest
|
||||||
|
container_name: ulflow-adminer-dev
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
environment:
|
||||||
|
ADMINER_DEFAULT_SERVER: postgres # Tên service của postgres trong compose này
|
||||||
|
depends_on:
|
||||||
|
- postgres
|
||||||
|
networks:
|
||||||
|
- ulflow-network
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
postgres-dev-data: # Volume riêng cho dev DB
|
||||||
|
go-modules: # Volume để cache Go modules cho dev
|
||||||
|
|
||||||
|
networks:
|
||||||
|
ulflow-network:
|
||||||
|
driver: bridge
|
||||||
@ -1,83 +0,0 @@
|
|||||||
version: '3.8'
|
|
||||||
|
|
||||||
services:
|
|
||||||
# API Service - Production Configuration
|
|
||||||
api:
|
|
||||||
build:
|
|
||||||
context: .
|
|
||||||
dockerfile: Dockerfile
|
|
||||||
container_name: ulflow-api
|
|
||||||
restart: always
|
|
||||||
ports:
|
|
||||||
- "3000:3000"
|
|
||||||
env_file:
|
|
||||||
- .env
|
|
||||||
environment:
|
|
||||||
- APP_ENV=production
|
|
||||||
depends_on:
|
|
||||||
- postgres
|
|
||||||
networks:
|
|
||||||
- ulflow-network
|
|
||||||
deploy:
|
|
||||||
resources:
|
|
||||||
limits:
|
|
||||||
cpus: '1'
|
|
||||||
memory: 1G
|
|
||||||
restart_policy:
|
|
||||||
condition: on-failure
|
|
||||||
max_attempts: 3
|
|
||||||
healthcheck:
|
|
||||||
test: ["CMD", "wget", "--spider", "-q", "http://localhost:3000/health"]
|
|
||||||
interval: 30s
|
|
||||||
timeout: 10s
|
|
||||||
retries: 3
|
|
||||||
start_period: 40s
|
|
||||||
|
|
||||||
# PostgreSQL Database - Production Configuration
|
|
||||||
postgres:
|
|
||||||
image: postgres:15-alpine
|
|
||||||
container_name: ulflow-postgres
|
|
||||||
restart: always
|
|
||||||
environment:
|
|
||||||
POSTGRES_USER: ${DB_USER:-user}
|
|
||||||
POSTGRES_PASSWORD: ${DB_PASSWORD:-password}
|
|
||||||
POSTGRES_DB: ${DB_NAME:-ulflow_db}
|
|
||||||
volumes:
|
|
||||||
- postgres-data:/var/lib/postgresql/data
|
|
||||||
networks:
|
|
||||||
- ulflow-network
|
|
||||||
deploy:
|
|
||||||
resources:
|
|
||||||
limits:
|
|
||||||
cpus: '1'
|
|
||||||
memory: 1G
|
|
||||||
healthcheck:
|
|
||||||
test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-user} -d ${DB_NAME:-ulflow_db}"]
|
|
||||||
interval: 30s
|
|
||||||
timeout: 5s
|
|
||||||
retries: 3
|
|
||||||
start_period: 10s
|
|
||||||
|
|
||||||
# Nginx Reverse Proxy
|
|
||||||
nginx:
|
|
||||||
image: nginx:alpine
|
|
||||||
container_name: ulflow-nginx
|
|
||||||
restart: always
|
|
||||||
ports:
|
|
||||||
- "80:80"
|
|
||||||
- "443:443"
|
|
||||||
volumes:
|
|
||||||
- ./nginx/conf.d:/etc/nginx/conf.d
|
|
||||||
- ./nginx/ssl:/etc/nginx/ssl
|
|
||||||
- ./nginx/logs:/var/log/nginx
|
|
||||||
depends_on:
|
|
||||||
- api
|
|
||||||
networks:
|
|
||||||
- ulflow-network
|
|
||||||
|
|
||||||
volumes:
|
|
||||||
postgres-data:
|
|
||||||
|
|
||||||
networks:
|
|
||||||
ulflow-network:
|
|
||||||
driver: bridge
|
|
||||||
@ -1,57 +1,45 @@
|
|||||||
|
# docker-compose.prod.yml (Cho Production / CI/CD)
|
||||||
version: '3.8'
|
version: '3.8'
|
||||||
|
|
||||||
services:
|
services:
|
||||||
# API Service
|
|
||||||
api:
|
api:
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: Dockerfile.local
|
dockerfile: Dockerfile # Sử dụng Dockerfile production
|
||||||
container_name: ulflow-api
|
container_name: ulflow-api-prod
|
||||||
restart: unless-stopped
|
|
||||||
ports:
|
ports:
|
||||||
- "3000:3000"
|
- "3000:3000"
|
||||||
volumes:
|
# KHÔNG CÓ VOLUMES: - .:/app ở đây!
|
||||||
- .:/app
|
command: /app/app # Chạy binary đã build
|
||||||
- go-modules:/go/pkg/mod
|
# KHÔNG CÓ ENV_FILE Ở ĐÂY, BIẾN MÔI TRƯỜNG SẼ ĐƯỢC CUNG CẤP TỪ BÊN NGOÀI
|
||||||
env_file:
|
restart: always
|
||||||
- .env
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '0.50'
|
||||||
|
memory: 256M
|
||||||
depends_on:
|
depends_on:
|
||||||
- postgres
|
- postgres
|
||||||
networks:
|
networks:
|
||||||
- ulflow-network
|
- ulflow-network
|
||||||
|
|
||||||
# PostgreSQL Database
|
|
||||||
postgres:
|
postgres:
|
||||||
image: postgres:15-alpine
|
image: postgres:15-alpine
|
||||||
container_name: ulflow-postgres
|
container_name: ulflow-postgres-prod
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_USER: postgres
|
POSTGRES_USER: ${DATABASE_USERNAME}
|
||||||
POSTGRES_PASSWORD: Minhtuyen0605
|
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
|
||||||
POSTGRES_DB: ulflow_zee
|
POSTGRES_DB: ${DATABASE_NAME}
|
||||||
ports:
|
ports:
|
||||||
- "5432:5432"
|
- "5433:5432"
|
||||||
volumes:
|
volumes:
|
||||||
- postgres-data:/var/lib/postgresql/data
|
- postgres-prod-data:/var/lib/postgresql/data # Volume riêng cho prod DB
|
||||||
networks:
|
networks:
|
||||||
- ulflow-network
|
- ulflow-network
|
||||||
adminer:
|
|
||||||
image: adminer:latest # Hoặc adminer:4.8.1 (chọn phiên bản cụ thể nếu muốn)
|
|
||||||
container_name: ulflow-adminer
|
|
||||||
restart: unless-stopped
|
|
||||||
ports:
|
|
||||||
- "8080:8080" # Ánh xạ cổng 8080 của container ra cổng 8080 của host
|
|
||||||
environment:
|
|
||||||
ADMINER_DEFAULT_SERVER: postgres # Tên service của PostgreSQL trong Docker Compose
|
|
||||||
depends_on:
|
|
||||||
- postgres # Đảm bảo postgres khởi động trước adminer
|
|
||||||
networks:
|
|
||||||
- ulflow-network
|
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
postgres-data:
|
postgres-prod-data:
|
||||||
go-modules:
|
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
ulflow-network:
|
ulflow-network:
|
||||||
driver: bridge
|
driver: bridge
|
||||||
Loading…
x
Reference in New Issue
Block a user