Dockerfile
Dockerfile
A script for building Docker images. Composed of instructions such as FROM, RUN, and COPY.
Dockerfile
A script for building Docker images. Composed of instructions such as FROM, RUN, and COPY.
A Dockerfile is a text-based script for building Docker images. It defines the container environment as code, from choosing a base image to installing dependencies, copying the application, and specifying the run command. It can be considered the most basic form of "Infrastructure as Code".
Each instruction in a Dockerfile (FROM, RUN, COPY, etc.) creates a new layer in the image. Because layers are cached, placing frequently changing instructions toward the bottom can greatly reduce build time. For example, if you copy only package.json first and run npm install, the dependency cache is reused when the code changes.
Multi-stage builds are a key technique for creating optimized production images. The build stage uses compilers and development tools, and the final stage copies only the executable binary, which can reduce the image size to a few tens of MB.
For security, it is advisable to run as a non-root user, exclude unnecessary files with .dockerignore, and use a specific version of the base image. The latest tag reduces reproducibility, so avoid it in production.
# ๋ฉํฐ์คํ
์ด์ง ๋น๋ - Node.js ์ ํ๋ฆฌ์ผ์ด์
# Stage 1: Build
FROM node:20-alpine AS builder
WORKDIR /app
# ์์กด์ฑ ๋จผ์ ์ค์น (์บ์ ์ต์ ํ)
COPY package*.json ./
RUN npm ci --only=production
# ์์ค ์ฝ๋ ๋ณต์ฌ ๋ฐ ๋น๋
COPY . .
RUN npm run build
# Stage 2: Production
FROM node:20-alpine AS production
WORKDIR /app
# ๋ณด์: non-root ์ฌ์ฉ์ ์์ฑ
RUN addgroup -g 1001 -S nodejs && \
adduser -S nextjs -u 1001
# ๋น๋ ๊ฒฐ๊ณผ๋ฌผ๋ง ๋ณต์ฌ
COPY --from=builder --chown=nextjs:nodejs /app/dist ./dist
COPY --from=builder --chown=nextjs:nodejs /app/node_modules ./node_modules
COPY --from=builder --chown=nextjs:nodejs /app/package.json ./
# non-root ์ฌ์ฉ์๋ก ์คํ
USER nextjs
# ํฌ์ค์ฒดํฌ
HEALTHCHECK --interval=30s --timeout=3s \
CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1
EXPOSE 3000
CMD ["node", "dist/main.js"]
Senior: "The image is 1.2GB, which is too big. Let's shrink it with a multi-stage build."
Junior: "Would it work if I use an alpine base image and remove devDependencies?"
Senior: "That's good too, and consider trying a distroless image as well. It's good for security, too."
Interviewer: "Please explain how to optimize Dockerfile build speed."
Candidate: "I use the layer cache. I install dependencies, which change infrequently, first, and copy the source code afterward. I also exclude unnecessary files with .dockerignore and take advantage of BuildKit's parallel build feature."
Take a mock interview on this topic โ WIA SOOM AI Interview ยท Start for free
Reviewer: "Please specify a particular version instead of FROM node:latest. Also, combining multiple RUN instructions with && reduces the number of layers."
Developer: "Ah, it's running as root. I'll add a non-root user with the USER instruction."