📝 Custom Images = Dockerfile
Custom images are powerful. Dockerfile builds custom images for your applications.
📝 Dockerfile Basics
# .NET Core Application FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build WORKDIR /src # Copy and restore COPY *.csproj . RUN dotnet restore # Build COPY . . RUN dotnet publish -c Release -o /app # Runtime FROM mcr.microsoft.com/dotnet/aspnet:8.0 WORKDIR /app COPY --from=build /app . ENV ASPNETCORE_ENVIRONMENT=Production EXPOSE 80 ENTRYPOINT ["dotnet", "MyApp.dll"] # Node.js Application FROM node:18-alpine AS builder WORKDIR /app COPY package*.json ./ RUN npm install COPY . . RUN npm run build FROM nginx:alpine COPY --from=builder /app/dist /usr/share/nginx/html EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]
🎯 Advanced Dockerfile
# Multi-stage Build
FROM golang:1.21 AS build
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -o app .
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=build /app/app .
CMD ["/root/app"]
# Build Arguments
ARG BUILD_ENV=production
FROM node:18-alpine AS builder-${BUILD_ENV}
WORKDIR /app
COPY package*.json ./
RUN npm install --production=${BUILD_ENV}
# Healthcheck
HEALTHCHECK --interval=30s --timeout=3s \
CMD curl -f http://localhost/ || exit 1
# Labels
LABEL maintainer="team@example.com"
LABEL version="1.0.0"
LABEL description="My application"
# Environment Variables
ENV NODE_ENV=production
ENV PORT=8080
💡 Dockerfile Tips
- Use Alpine base images
- Minimize layers
- Use multi-stage builds
- Set environment variables
- Add healthchecks
Dockerfile gives you full control over image creation. It’s essential for production containerization.
