|
| 1 | +# 🐳 Docker + GitHub Actions — Complete CI/CD Guide |
| 2 | + |
| 3 | +This guide extends your full-stack project to use **Docker** and **GitHub Actions** for containerized CI/CD. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 🧱 Step 1. What You’ll Learn |
| 8 | +By the end of this guide, you’ll be able to: |
| 9 | + |
| 10 | +✅ Build Docker images for backend & frontend |
| 11 | +✅ Push them to Docker Hub (or GHCR) |
| 12 | +✅ Run tests inside containers for consistency |
| 13 | +✅ Deploy using Docker containers |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## ⚙️ Step 2. Prepare Project for Docker |
| 18 | + |
| 19 | +Your existing project: |
| 20 | +- `frontend/` → React (Vite) |
| 21 | +- `backend/` → Express (Node.js) |
| 22 | + |
| 23 | +Add Docker support for both. |
| 24 | + |
| 25 | +### 🐳 2.1 Backend `Dockerfile` |
| 26 | + |
| 27 | +```dockerfile |
| 28 | +FROM node:20-alpine |
| 29 | +WORKDIR /app |
| 30 | +COPY package*.json ./ |
| 31 | +RUN npm ci --only=production |
| 32 | +COPY . . |
| 33 | +EXPOSE 4000 |
| 34 | +CMD ["npm", "start"] |
| 35 | +``` |
| 36 | + |
| 37 | +### 🐳 2.2 Frontend `Dockerfile` |
| 38 | + |
| 39 | +```dockerfile |
| 40 | +FROM node:20-alpine AS build |
| 41 | +WORKDIR /app |
| 42 | +COPY package*.json ./ |
| 43 | +RUN npm ci |
| 44 | +COPY . . |
| 45 | +RUN npm run build |
| 46 | + |
| 47 | +FROM nginx:stable-alpine |
| 48 | +COPY --from=build /app/dist /usr/share/nginx/html |
| 49 | +EXPOSE 80 |
| 50 | +CMD ["nginx", "-g", "daemon off;"] |
| 51 | +``` |
| 52 | + |
| 53 | +### 🧩 2.3 Add `.dockerignore` |
| 54 | + |
| 55 | +```bash |
| 56 | +node_modules |
| 57 | +npm-debug.log |
| 58 | +.git |
| 59 | +.gitignore |
| 60 | +Dockerfile |
| 61 | +.env* |
| 62 | +test-results |
| 63 | +``` |
| 64 | + |
| 65 | +--- |
| 66 | + |
| 67 | +## 🧪 Step 3. Run Locally |
| 68 | + |
| 69 | +```bash |
| 70 | +# Backend |
| 71 | +cd backend |
| 72 | +docker build -t task-backend . |
| 73 | +docker run -p 4000:4000 task-backend |
| 74 | + |
| 75 | +# Frontend |
| 76 | +cd frontend |
| 77 | +docker build -t task-frontend . |
| 78 | +docker run -p 5173:80 task-frontend |
| 79 | +``` |
| 80 | + |
| 81 | +Verify: |
| 82 | +- Frontend → http://localhost:5173 |
| 83 | +- Backend → http://localhost:4000/tasks |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +## 🤖 Step 4. Automate with GitHub Actions |
| 88 | + |
| 89 | +Create: `.github/workflows/docker-ci.yml` |
| 90 | + |
| 91 | +```yaml |
| 92 | +name: Docker Build & Push |
| 93 | + |
| 94 | +on: |
| 95 | + push: |
| 96 | + branches: [ main ] |
| 97 | + |
| 98 | +env: |
| 99 | + REGISTRY: docker.io |
| 100 | + IMAGE_BACKEND: your-dockerhub-username/task-backend |
| 101 | + IMAGE_FRONTEND: your-dockerhub-username/task-frontend |
| 102 | + |
| 103 | +jobs: |
| 104 | + build-and-push: |
| 105 | + runs-on: ubuntu-latest |
| 106 | + |
| 107 | + steps: |
| 108 | + - name: Checkout repo |
| 109 | + uses: actions/checkout@v4 |
| 110 | + |
| 111 | + - name: Set up Docker Buildx |
| 112 | + uses: docker/setup-buildx-action@v3 |
| 113 | + |
| 114 | + - name: Log in to DockerHub |
| 115 | + uses: docker/login-action@v3 |
| 116 | + with: |
| 117 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 118 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 119 | + |
| 120 | + - name: Build and push backend image |
| 121 | + uses: docker/build-push-action@v6 |
| 122 | + with: |
| 123 | + context: ./backend |
| 124 | + push: true |
| 125 | + tags: ${{ env.IMAGE_BACKEND }}:latest |
| 126 | + |
| 127 | + - name: Build and push frontend image |
| 128 | + uses: docker/build-push-action@v6 |
| 129 | + with: |
| 130 | + context: ./frontend |
| 131 | + push: true |
| 132 | + tags: ${{ env.IMAGE_FRONTEND }}:latest |
| 133 | +``` |
| 134 | +
|
| 135 | +--- |
| 136 | +
|
| 137 | +## 🔐 Step 5. Configure Secrets in GitHub |
| 138 | +
|
| 139 | +Go to **Repo → Settings → Secrets → Actions → New repository secret** |
| 140 | +
|
| 141 | +``` |
| 142 | +DOCKERHUB_USERNAME = your_dockerhub_username |
| 143 | +DOCKERHUB_TOKEN = <your_access_token> |
| 144 | +``` |
| 145 | + |
| 146 | +Generate a token at [Docker Hub → Account Settings → Access Tokens](https://hub.docker.com/settings/security). |
| 147 | + |
| 148 | +--- |
| 149 | + |
| 150 | +## 🧠 Step 6. Validate the Pipeline |
| 151 | + |
| 152 | +After pushing to `main`, GitHub Actions will: |
| 153 | +1. Build backend image from `/backend` |
| 154 | +2. Build frontend image from `/frontend` |
| 155 | +3. Push both to Docker Hub |
| 156 | + |
| 157 | +You’ll see in Docker Hub: |
| 158 | +- `task-backend:latest` |
| 159 | +- `task-frontend:latest` |
| 160 | + |
| 161 | +--- |
| 162 | + |
| 163 | +## 🚀 Step 7. (Optional) Run E2E Tests in Docker |
| 164 | + |
| 165 | +```yaml |
| 166 | + e2e-tests: |
| 167 | + runs-on: ubuntu-latest |
| 168 | + needs: build-and-push |
| 169 | + steps: |
| 170 | + - name: Pull and run containers |
| 171 | + run: | |
| 172 | + docker run -d -p 4000:4000 --name backend your-dockerhub-username/task-backend:latest |
| 173 | + docker run -d -p 5173:80 --name frontend your-dockerhub-username/task-frontend:latest |
| 174 | +
|
| 175 | + - name: Wait for services |
| 176 | + run: | |
| 177 | + npx wait-on http://localhost:4000/tasks |
| 178 | + npx wait-on http://localhost:5173 |
| 179 | +
|
| 180 | + - name: Run E2E Tests |
| 181 | + working-directory: e2e |
| 182 | + run: npx playwright test |
| 183 | +``` |
| 184 | +
|
| 185 | +--- |
| 186 | +
|
| 187 | +## 🎓 Step 8. What You’ve Achieved |
| 188 | +
|
| 189 | +✅ Full CI/CD with Docker and GitHub Actions |
| 190 | +✅ Automated build + push to Docker Hub |
| 191 | +✅ Test execution in consistent containers |
| 192 | +✅ Ready for production deployment |
| 193 | +
|
| 194 | +--- |
| 195 | +
|
| 196 | +## 🧭 Next Steps |
| 197 | +
|
| 198 | +- Deploy containers to **Render**, **AWS ECS**, or **Fly.io** |
| 199 | +- Use **multi-stage Docker builds** for optimized images |
| 200 | +- Integrate **GitHub Environments** (staging → production) |
| 201 | +- Explore **Docker Compose in CI** for multi-container tests |
| 202 | +
|
| 203 | +--- |
| 204 | +
|
| 205 | +**You’ve officially completed a full DevOps-ready GitHub Actions journey — from CI → CD → Docker orchestration. 🎉** |
0 commit comments