Day 03 Applied Skills

Docker Builds in CI — CI/CD in 5 Days

~1 hour Hands-on Precision AI Academy

Today's Objective

By the end of this lesson, you will understand the core concepts of Docker Builds in CI — CI/CD in 5 Days and be able to apply them in a real project.

dockerfile
dockerfile
# Dockerfile
FROM node:20-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
RUN npm run build

FROM node:20-alpine AS runtime
WORKDIR /app
COPY --from=build /app/dist ./dist
COPY --from=build /app/node_modules ./node_modules
EXPOSE 3000
CMD ["node", "dist/index.js"]
yaml
yaml
# .github/workflows/docker.yml
name: Build and Push Docker Image

on:
  push:
    branches: [main]
    tags: ['v*']

jobs:
  docker:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      - uses: actions/checkout@v4

      - name: Log in to GHCR
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ghcr.io/${{ github.repository }}
          tags: |
            type=sha
            type=semver,pattern={{version}}
            type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }}

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
01

Exercise: Build and Push an Image

  1. Write a Dockerfile for a Node.js or Python app
  2. Create the docker.yml workflow
  3. Push to main and verify the image appears in GHCR
  4. Pull the image: docker pull ghcr.io/yourname/repo:latest
  5. Run it locally: docker run -p 3000:3000 ...
02

Day 3 Summary