Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
135 lines (107 loc) · 2.81 KB
/
Copy pathDockerfile
File metadata and controls
135 lines (107 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Use the official Jenkins LTS image as the base
FROM jenkins/jenkins:lts
# We need to be root to install software
USER root
# Install prerequisites and the Docker CLI
RUN apt-get update && apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
# Add Docker's official GPG key
RUN curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# Set up the Docker repository
RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install the Docker CLI
RUN apt-get update && apt-get install -y docker-ce-cli
# Switch back to the default jenkins user
USER jenkins
# Multi-stage build for optimal image size
# Stage 1: Build stage
FROM node:18-alpine AS builder
# Set working directory
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install dependencies
RUN npm ci --only=production && \
npm cache clean --force
# Copy application code
COPY . .
# Build application (if needed)
# RUN npm run build
# Stage 2: Runtime stage
FROM node:18-alpine
# Install dumb-init for proper signal handling
RUN apk add --no-cache dumb-init
# Create non-root user
RUN addgroup appuser && \
adduser -D -G appuser appuser
# Set working directory
WORKDIR /app
# Copy dependencies from builder
COPY --from=builder /app/node_modules ./node_modules
# Copy application code
COPY . .
# Change ownership to appuser
RUN chown -R appuser:appuser /app
# Set environment variables
ENV NODE_ENV=production \
PORT=8080
# Expose port
EXPOSE 8080
# Switch to non-root user
USER appuser
# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD node healthcheck.js || exit 1
# Use dumb-init to handle signals properly
ENTRYPOINT ["dumb-init", "--"]
# Start application
CMD ["node", "server.js"]
# Alternative Dockerfile for Python applications
# FROM python:3.11-slim
#
# WORKDIR /app
#
# RUN apt-get update && \
# apt-get install -y --no-install-recommends gcc && \
# rm -rf /var/lib/apt/lists/*
#
# COPY requirements.txt .
# RUN pip install --no-cache-dir -r requirements.txt
#
# COPY . .
#
# RUN useradd -m -u 1000 appuser && \
# chown -R appuser:appuser /app
#
# USER appuser
#
# EXPOSE 8080
#
# CMD ["python", "app.py"]
# Alternative Dockerfile for Go applications
# FROM golang:1.21-alpine AS builder
#
# WORKDIR /app
#
# COPY go.mod go.sum ./
# RUN go mod download
#
# COPY . .
#
# RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
#
# FROM alpine:latest
#
# RUN apk --no-cache add ca-certificates
#
# WORKDIR /root/
#
# COPY --from=builder /app/main .
#
# EXPOSE 8080
#
# CMD ["./main"]