Production Scaling

Scaling the Helix project in production involves several systematic steps that leverage Docker for containerization and ensure that both backend and frontend components are efficiently built and ready for deployment. The following is a detailed guide on how to achieve this.

1. Building the Go Backend

The backend is built using a multi-stage build approach in Docker. This method helps in minimizing the final image size by only including necessary components.

Dockerfile Configuration for Backend:

# Backend build
FROM golang:1.22 AS go-build-env
WORKDIR /app

# <- COPY go.mod and go.sum files to the workspace
COPY go.mod .
COPY go.sum .

RUN go mod download

# COPY the source code as the last step
COPY api ./api
COPY .git /.git

WORKDIR /app/api

# Build the Go app
RUN CGO_ENABLED=0 go build -ldflags "-s -w" -o /helix
  • The FROM golang:1.22 AS go-build-env line specifies that the build will use the Go programming language image version 1.22.

  • The project dependencies defined in go.mod and go.sum are downloaded to ensure that the application has access to the necessary libraries.

  • The source code for the API is copied, followed by the compilation of the application into a binary named /helix.

2. Building the Frontend

The frontend uses a Node.js environment for building the client-side application, typically managed through Yarn.

Dockerfile Configuration for Frontend:

# Frontend build
FROM node:21-alpine AS ui-build-env

WORKDIR /app

RUN echo "installing apk packages" && \
  apk update && \
  apk upgrade && \
  apk add \
    bash \
    git \
    curl \
    openssh

# root config
COPY ./frontend/*.json /app/
COPY ./frontend/yarn.lock /app/yarn.lock

# Install modules
RUN yarn install

# Copy the rest of the code
COPY ./frontend /app

# Build the frontend
RUN yarn build
  • The Docker image is built on top of node:21-alpine, which ensures a lightweight build environment.

  • Necessary APK packages are installed for building the frontend, ensuring that all dependencies are available.

  • The configuration files (*.json and yarn.lock) are copied and Yarn is used to install the project dependencies.

  • Finally, the frontend application is built, producing static assets for deployment.

3. Finalizing the Production Image

Creating a production-ready image requires combining both the backend and frontend components into a final Docker image.

Final Dockerfile Configuration:

FROM alpine:3.17
RUN apk --update add ca-certificates

COPY --from=go-build-env /helix /helix
COPY --from=ui-build-env /app/dist /www

ENV FRONTEND_URL=/www

ENTRYPOINT ["/helix", "serve"]
  • An Alpine Linux image is used for the final stage, which provides a minimal base for running the application.

  • The built Go binary is copied from the go-build-env and the static files from the ui-build-env.

  • FRONTEND_URL is an environment variable that points to the location of the built frontend files.

  • The entry point of the container is defined to execute the Go server, launching the application.

4. Deploying and Scaling

Once you have the final Docker image created using the above configurations, scaling can be achieved using container orchestration tools like Kubernetes or Docker Swarm.

Example of Scaling with Docker Compose

You can define a docker-compose.yml file to manage the deployment process:

version: '3.8'

services:
  helix:
    image: your_docker_image_name
    ports:
      - "8080:8080"
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: '0.5'
          memory: 512M
  • This configuration defines a service named helix that uses the custom Docker image.

  • The service maps port 8080 on the host to port 8080 on the container and specifies that five replicas should be maintained.

  • Resource limits are defined to ensure that each replica has limited access to CPU and memory, aiding in effective scaling.

Conclusion

By following this structured process for building and deploying the Helix application, you can ensure that the project scales effectively in production environments.