Docker Configuration in ForgeX

Guide for Docker config in ForgeX.

Advanced Flag: Docker Configuration in ForgeX

The Docker advanced flag provides the app's Dockerfile configuration and creates or updates the docker-compose.yml file, which is generated if a DB driver is used.
The Dockerfile includes a two-stage build, resulting in a smaller final image without unnecessary build dependencies. The configuration adjusts dynamically based on the advanced features selected.

Dockerfile

Standard Dockerfile

FROM golang:1.23-alpine AS build

RUN apk add --no-cache curl

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . .

RUN go install github.com/a-h/templ/cmd/templ@latest && \
    templ generate && \
    curl -sL https://github.com/tailwindlabs/tailwindcss/releases/latest/download/tailwindcss-linux-x64 -o tailwindcss && \
    chmod +x tailwindcss && \
    ./tailwindcss -i cmd/web/assets/css/input.css -o cmd/web/assets/css/output.css

RUN go build -o main cmd/api/main.go

FROM alpine:3.20.1 AS prod
WORKDIR /app
COPY --from=build /app/main /app/main
EXPOSE ${PORT}
CMD ["./main"]

Dockerfile with React Flag

FROM golang:1.23-alpine AS build

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . .

RUN go build -o main cmd/api/main.go

FROM alpine:3.20.1 AS prod
WORKDIR /app
COPY --from=build /app/main /app/main
EXPOSE ${PORT}
CMD ["./main"]

FROM node:20 AS frontend_builder
WORKDIR /frontend

COPY frontend/package*.json ./
RUN npm install
COPY frontend/. .
RUN npm run build

FROM node:20-slim AS frontend
RUN npm install -g serve
COPY --from=frontend_builder /frontend/dist /app/dist
EXPOSE 5173
CMD ["serve", "-s", "/app/dist", "-l", "5173"]

Docker Compose

docker-compose.yml pulls environment variables from the .env file. Below is an example configuration if the Docker flag is used with the MySQL DB driver:

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
      target: prod
    restart: unless-stopped
    ports:
      - ${PORT}:${PORT}
    environment:
      APP_ENV: ${APP_ENV}
      PORT: ${PORT}
      FORGEX_DB_HOST: ${FORGEX_DB_HOST}
      FORGEX_DB_PORT: ${FORGEX_DB_PORT}
      FORGEX_DB_DATABASE: ${FORGEX_DB_DATABASE}
      FORGEX_DB_USERNAME: ${FORGEX_DB_USERNAME}
      FORGEX_DB_PASSWORD: ${FORGEX_DB_PASSWORD}
    depends_on:
      mysql_fx:
        condition: service_healthy
    networks:
      - forgex
  mysql_fx:
    image: mysql:latest
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: ${FORGEX_DB_DATABASE}
      MYSQL_USER: ${FORGEX_DB_USERNAME}
      MYSQL_PASSWORD: ${FORGEX_DB_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${FORGEX_DB_ROOT_PASSWORD}
    ports:
      - "${FORGEX_DB_PORT}:3306"
    volumes:
      - mysql_volume_fx:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "${FORGEX_DB_HOST}", "-u", "${FORGEX_DB_USERNAME}", "--password=${FORGEX_DB_PASSWORD}"]
      interval: 5s
      timeout: 5s
      retries: 3
      start_period: 15s
    networks:
      - forgex

volumes:
  mysql_volume_fx:
networks:
  forgex:

Notes

Cleaning Docker Leftovers If you are testing multiple frameworks locally, ensure to clean up Docker leftovers such as volumes. Use the following commands:

docker compose down --volumes
docker compose up --build

or

docker compose build --no-cache && docker compose up