Containerization is a key technology in modern software development and deployment, enabling consistent environments across different stages of development and production. Docker is the most popular containerization platform, providing tools to create, deploy, and run applications inside containers. This section will guide you through containerizing a Go application using Docker and best practices for working with Docker in a DevOps environment.
Dockerfile:
dockerfile# Use an official Go runtime as a parent image FROM golang:1.16-alpine # Set the Current Working Directory inside the container WORKDIR /app # Copy the local package files to the container's workspace COPY . . # Build the Go app RUN go build -o main . # Command to run the executable CMD ["./main"]
Building an Image:
docker build
command to create a Docker image from the Dockerfile.shdocker build -t go-app .
Running a Container:
docker run
command to create and start a container from the image.shdocker run -p 8080:8080 go-app
yamlversion: '3'
services:
web:
image: go-app
build: .
ports:
- "8080:8080"
depends_on:
- db
db:
image: postgres:13
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: goappdb
shdocker-compose up --build
shdocker-compose down
Multi-Stage Builds:
dockerfile# Build stage FROM golang:1.16-alpine AS build WORKDIR /app COPY . . RUN go build -o main . # Run stage FROM alpine:latest WORKDIR /root/ COPY --from=build /app/main . CMD ["./main"]
Minimize Image Size:
alpine
to reduce the final image size.Environment Variables:
dockerfileENV PORT=8080 ENV DB_HOST=db
Health Checks:
dockerfileHEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ CMD curl -f http://localhost:8080/health || exit 1
yamlname: CI/CD Pipeline
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Build Docker image
run: docker build -t go-app .
- name: Log in to Docker Hub
run: echo ${{ secrets.DOCKER_HUB_PASSWORD }} | docker login -u ${{ secrets.DOCKER_HUB_USERNAME }} --password-stdin
- name: Push Docker image
run: docker push go-app:latest
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Deploy to Kubernetes
run: |
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
yaml# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: go-app
spec:
replicas: 2
selector:
matchLabels:
app: go-app
template:
metadata:
labels:
app: go-app
spec:
containers:
- name: go-app
image: go-app:latest
ports:
- containerPort: 8080
yaml# service.yaml
apiVersion: v1
kind: Service
metadata:
name: go-app
spec:
type: LoadBalancer
selector:
app: go-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
Minimize Vulnerabilities:
Clair
or Trivy
to scan images for known vulnerabilities.Limit Container Privileges:
yaml# Kubernetes Pod Security Context
apiVersion: v1
kind: Pod
metadata:
name: go-app
spec:
containers:
- name: go-app
image: go-app:latest
securityContext:
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000
Use Secrets Management:
By following these guidelines and best practices, you can effectively containerize your Go applications, ensuring they are portable, scalable, and secure. Docker and containerization are fundamental to modern DevOps practices, enabling consistent and efficient deployment workflows.