chore:update docker
Some checks failed
CI Pipeline / Security Scan (push) Successful in 5m1s
CI Pipeline / Lint (push) Failing after 6m9s
CI Pipeline / Test (push) Has been skipped
CI Pipeline / Build (push) Has been skipped
CI Pipeline / Notification (push) Successful in 1s

This commit is contained in:
ulflow_phattt2901 2025-06-06 19:11:07 +07:00
parent 02c6a15607
commit f8957e0d95
7 changed files with 106 additions and 135 deletions

View File

@ -1,6 +1,4 @@
# syntax=docker/dockerfile:1.4
# Production-ready multi-stage build for server deployment
# Dockerfile
# Build arguments
ARG GO_VERSION=1.24.3
ARG ALPINE_VERSION=3.19
@ -16,7 +14,7 @@ RUN apk add --no-cache git make gcc libc-dev
# Set working directory
WORKDIR /build
# Enable Go modules
# Enable Go modules and set build environment variables
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
@ -25,37 +23,35 @@ ENV GO111MODULE=on \
# Copy dependency files first for better layer caching
COPY go.mod go.sum ./
# Download dependencies
# Download dependencies using cache mount for efficiency
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download
# Copy source code
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
RUN mkdir -p /build/configs && \
cp -r configs/* /build/configs/ 2>/dev/null || :
# Build the application with optimizations
# Build the application with optimizations and version info
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')'" \
-o /build/bin/app ./cmd/app
-o /build/bin/app ./src/cmd/api # Update path to your main package
# --- Final Stage ---
FROM alpine:${ALPINE_VERSION}
# Re-declare ARG to use in this stage
ARG APP_USER=appuser
ARG APP_GROUP=appgroup
ARG APP_USER
ARG APP_GROUP
ARG APP_HOME=/app
# Set environment variables
# Set runtime environment variables
ENV TZ=Asia/Ho_Chi_Minh \
APP_USER=${APP_USER} \
APP_GROUP=${APP_GROUP} \
APP_HOME=${APP_HOME}
APP_HOME=${APP_HOME} \
APP_ENV=production
# Install runtime dependencies
RUN apk add --no-cache \
@ -68,7 +64,7 @@ RUN apk add --no-cache \
RUN addgroup -S ${APP_GROUP} && \
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
# Switch to app directory

View File

@ -86,7 +86,10 @@ clean:
docker-build:
@echo "Building Docker image..."
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
docker-run:
@echo "Running application in Docker container..."
@ -97,6 +100,13 @@ docker-run:
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
docker-compose-up:
@echo "Starting all services with Docker Compose for local development..."

View File

@ -70,6 +70,7 @@ func (s *HTTPService) Shutdown(ctx context.Context) error {
}
func main() {
fmt.Println("Starting application 222...")
// Initialize config loader
configLoader := config.NewConfigLoader()

59
docker-compose.dev.yml Normal file
View 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

View File

@ -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

View File

@ -1,56 +1,44 @@
# docker-compose.prod.yml (Cho Production / CI/CD)
version: '3.8'
services:
# API Service
api:
build:
context: .
dockerfile: Dockerfile.local
container_name: ulflow-api
restart: unless-stopped
dockerfile: Dockerfile # Sử dụng Dockerfile production
container_name: ulflow-api-prod
ports:
- "3000:3000"
volumes:
- .:/app
- go-modules:/go/pkg/mod
env_file:
- .env
# KHÔNG CÓ VOLUMES: - .:/app ở đây!
command: /app/app # Chạy binary đã build
# KHÔNG CÓ ENV_FILE Ở ĐÂY, BIẾN MÔI TRƯỜNG SẼ ĐƯỢC CUNG CẤP TỪ BÊN NGOÀI
restart: always
deploy:
resources:
limits:
cpus: '0.50'
memory: 256M
depends_on:
- postgres
networks:
- ulflow-network
# PostgreSQL Database
postgres:
image: postgres:15-alpine
container_name: ulflow-postgres
container_name: ulflow-postgres-prod
restart: unless-stopped
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: Minhtuyen0605
POSTGRES_DB: ulflow_zee
POSTGRES_USER: ${DATABASE_USERNAME}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
POSTGRES_DB: ${DATABASE_NAME}
ports:
- "5432:5432"
- "5433:5432"
volumes:
- postgres-data:/var/lib/postgresql/data
- postgres-prod-data:/var/lib/postgresql/data # Volume riêng cho prod DB
networks:
- 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:
postgres-data:
go-modules:
postgres-prod-data:
networks:
ulflow-network: