This documentation provides a detailed step-by-step guide for deploying the benhall/golang-demo project in a production environment. The application leverages a multi-stage Docker build process to facilitate efficient deployment.

Step 1: Code Verification

Before deployment, verify that the code compiles and runs as expected in a local environment. Use the following commands to ensure that all dependencies are correctly set up:

go mod download
go mod tidy
go build -o myapp

Confirm that the application is functional by running it locally.

Step 2: Dockerfile Overview

The Dockerfile from the project defines a clear separation between the build and runtime environments. This results in a smaller final image size, which is optimal for production deployment. Below is the content of the Dockerfile:

# First stage: build the application
FROM golang:1.21-alpine AS builder

# Set the working directory
WORKDIR /app

# Copy go.mod
COPY go.mod go.sum ./

# Download dependencies and generate go.sum
RUN go mod download && go mod tidy

# Copy the rest of the application code
COPY . .

# Build the application
RUN go build -o myapp

# Second stage: create the runtime image
FROM alpine:latest

# Set the working directory
WORKDIR /root/

# Copy the built application from the builder stage
COPY --from=builder /app/myapp .

# Expose port 8080 to the outside world
EXPOSE 8080

# Command to run the executable
CMD ["./myapp"]

Breakdown of Dockerfile Stages

  1. Builder Stage:

    • The base image used is golang:1.21-alpine, which provides a lightweight environment conducive to building Go applications.
    • The working directory is set to /app.
    • Dependencies are managed using the go.mod and go.sum files. The command go mod download && go mod tidy ensures that all dependencies are fetched and cleaned up.
    • The application code is copied to the working directory, and then the application is built using go build -o myapp.
  2. Runtime Stage:

    • The second stage utilizes alpine:latest, creating a minimal runtime environment.
    • The output binary (myapp) from the builder stage is copied to the root directory.
    • Port 8080 is exposed for external access.
    • The command to run the application is defined using CMD ["./myapp"], allowing the application to start when the container is executed.

Step 3: Building the Docker Image

To create the Docker image for the application, execute the following command in the terminal:

docker build -t myapp:latest .

This command builds the Docker image and tags it as myapp:latest.

Step 4: Running the Docker Container

Once the Docker image has been built, the application can be run in a container. Use the following command to start the container with port mapping:

docker run -d -p 8080:8080 myapp:latest

This command runs the application in detached mode (-d) and maps port 8080 of the container to port 8080 of the host. Ensure that your firewall settings permit traffic on this port.

Step 5: Monitoring and Logging

In a production environment, it is essential to monitor the application and its logs for performance and error tracking. You can view logs using:

docker logs <container_id>

Replace <container_id> with the actual ID of the running container to retrieve log output.

Step 6: Scaling and Load Balancing

For applications requiring high availability, scaling may be necessary. You can leverage container orchestration tools such as Kubernetes or Docker Swarm to manage multiple container instances, ensuring that traffic is distributed evenly and uptime is maximized.

Conclusion

This documentation provides a comprehensive step-by-step guide to deploying the benhall/golang-demo project into a production environment. The use of a multi-stage Docker build process optimizes application size and security, delivering a robust deployment architecture.

The source of the information provided above is derived from the specifications available in the project repository.