Step-by-Step Guide
1. Build the Docker Image
The first step in deploying the project in production is to build the Docker image. The Dockerfile provided specifies multiple build stages: for the backend and the frontend.
Run the following command in your terminal from the project root:
docker build -t helixml/helix:latest .
This command triggers the building process as defined in the Dockerfile. It will create a multi-stage build environment where the Go backend and the Node.js frontend are built separately and then combined into a final lightweight image.
2. Understand the Dockerfile Structure
The Dockerfile contains several distinct phases:
Backend Build Phase:
FROM golang:1.22 AS go-build-env
WORKDIR /app
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY api ./api
COPY .git /.git
WORKDIR /app/api
RUN CGO_ENABLED=0 go build -ldflags "-s -w" -o /helix
- The
FROM golang:1.22 AS go-build-env
statement specifies the use of the Go language Docker image. - The
WORKDIR /app
command sets the working directory. COPY go.mod .
andCOPY go.sum .
bring over the module files needed for dependency management.- The
RUN go mod download
command resolves and downloads dependencies. - Finally, the source code is copied, and the Go application is built using
go build
.
Frontend Build Phase:
FROM node:21-alpine AS ui-build-env
WORKDIR /app
RUN apk update && apk upgrade && \
apk add bash git curl openssh
COPY ./frontend/*.json /app/
COPY ./frontend/yarn.lock /app/yarn.lock
RUN yarn install
COPY ./frontend /app
RUN yarn build
- The
FROM node:21-alpine AS ui-build-env
statement pulls in a lightweight Node.js environment. - Necessary packages are installed using
apk
, which is essential for building the frontend. - Module dependencies are installed with
yarn install
, followed by the execution ofyarn build
to compile the frontend assets.
Final Base Image:
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"]
- The final image is built from a minimal Alpine base image.
- The backend binary and frontend assets are copied from their respective build stages.
- Environment variables can be set, such as
FRONTEND_URL
, to route frontend requests correctly. - The app starts on entry with the command
ENTRYPOINT ["/helix", "serve"]
.
3. Run the Docker Container
After building the Docker image, you can run the container using the following command:
docker run -d -p 8080:8080 helixml/helix:latest
This command will deploy the application in a detached mode and map your host’s port 8080
to the container’s port 8080
.
4. Verify Application
Access the application by navigating to http://localhost:8080
in your web browser. Ensure that the application is serving the expected frontend and functioning as intended.
5. Production Considerations
- Regularly pull changes and rebuild the Docker image to keep your production environment up-to-date.
- Consider using a CI/CD pipeline to automate the build and deployment processes for efficiency and reliability.
- Monitor for logs and errors during the execution of the application to troubleshoot potential issues.
Following these steps allows for a smooth deployment of the helixml/helix
project into production, utilizing Docker for environment consistency and ease of scaling.