Logo

dev-resources.site

for different kinds of informations.

Deploy Go Application using Docker Compose Replicas and Nginx

Published at
6/3/2024
Categories
docker
go
nginx
deploy
Author
almatins
Categories
4 categories in total
docker
open
go
open
nginx
open
deploy
open
Author
8 person written this
almatins
open
Deploy Go Application using Docker Compose Replicas and Nginx

Deploying the Go application using docker and docker-compose with Nginx load balancer can be achieved using the below strategy. This is not the only one on how to do the task, but hopefully, you can find this useful.

Project Structure

My Go application project structure looks like this:

/go-app
|-- cmd
|-- internal <-- the app source code
|-- nginx
   |-- nginx.conf
|-- config.json <-- my config file for Viper
|-- docker-compose.yaml
|-- Dockerfile
|-- go.mod
|-- go.sum
|-- main.go
Enter fullscreen mode Exit fullscreen mode

Nginx Configuration

user nginx;
# can handle 1000 concurrent connections
events {
    worker_connections   1000;
}
# forwards http requests
http {
        # http server
        server {
              # listens the requests coming on port 8080
              listen 80;
              access_log  off;
              proxy_request_buffering off;
              proxy_buffering off;
              # / means all the requests have to be forwarded to api service
              location / {
                # resolves the IP of api using Docker internal DNS
                proxy_pass http://rest-api:3000;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
              }
        }
}
Enter fullscreen mode Exit fullscreen mode

Dockerfile

FROM golang:1.22-alpine AS builder

# working directory (/build).
WORKDIR /build

# dependency using go mod.
COPY go.mod go.sum ./
RUN go mod download

# Copy the code
COPY . .

# environment variables for docker image
# and build the server.
ENV CGO_ENABLED=0 GOOS=linux GOARCH=amd64
RUN apk add --no-cache dumb-init
RUN go build -ldflags="-s -w" -o apiserver ./main.go

FROM alpine:latest

# working directory (/build).
WORKDIR /

# Copy the Pre-built binary file from the previous stage.
COPY --from=builder ["/usr/bin/dumb-init", "/usr/bin/dumb-init"]
COPY --from=builder ["/build/apiserver", "/"]
COPY --from=builder ["/build/config.json", "/config.json"]

# Export necessary port.
EXPOSE 3000

ENTRYPOINT ["/usr/bin/dumb-init", "--"]
CMD ["/apiserver"]
Enter fullscreen mode Exit fullscreen mode

Docker Compose

services:
  # service name
  rest-api:
    # Dockerfile location
    build: "."
    # Exposes the port 3000 for internal
    ports:
      - "3000"
    # always restart when the service went down
    restart: always
    # number of replicas
    deploy:
      replicas: 2
  # nginx load balancer
  nginx:
    # latest stable alpine nginx image
    image: nginx:stable-alpine
    # nginx configuration
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
    # start nginx after the service up successfully
    depends_on:
      - rest-api
    # map the nginx port 80 to docker port 3000
    ports:
      - "3000:80"
Enter fullscreen mode Exit fullscreen mode

That’s it, now we can test the docker using docker compose build then continue with docker compose up -d .

Hope this can help someone out there. happy coding!

deploy Article's
30 articles in total
Favicon
How to Deploy a Static Website to AWS S3 with Razorops CI/CD
Favicon
Pipeline CD en Jenkins para terraform AWS EKS segunda parte (plugin AWS Credentials)
Favicon
Kamal 2 Quick Start - the missing tutorial
Favicon
Evento de Mobile, Frontend, Backend, Banco de Dados e Deploy Gratuito
Favicon
Deploy a Static Astro Site on Railway
Favicon
Deploy a PHP site to Railway
Favicon
Added advanced debugging features to my machine learning library like pytorch.
Favicon
When Companies Adopt Feature Flags
Favicon
Why should you have a Staging environment?
Favicon
How to Deploy Flutter Apps to the Google Play Store and App Store
Favicon
Deploy MongoDB Replica Set on K8s
Favicon
Deploy Go Application using Docker Compose Replicas and Nginx
Favicon
Despliegue de aplicación de Django con Github Actions para un servidor propio
Favicon
Common and Useful Deployment Patterns
Favicon
From Frustration to Fix: Conquering Vercel Errors Like a Pro
Favicon
Efficient Data Management with Prisma, Fly.io, and LiteFS Configuration
Favicon
Deploying Forem on Render.com PromptZone.com
Favicon
My Docker stack to deploy a Django + Celery web app
Favicon
Firebase Hosting Setup Complete Issue
Favicon
Deploy an Azure Functions app from a monorepo with a GitHub Action for Node.js
Favicon
DevOps, como começar? ...e por que?
Favicon
Kotlin and Azure Functions - Automating the deployment
Favicon
Private Deployment Gantt chart Project management tools
Favicon
Laravel Deployer Free package for laravel and nodejs apps Deployment
Favicon
Don't Couple Your Deployments
Favicon
Six niche tips for shipping Flutter MacOS builds
Favicon
Deploying a Static Site (feat.Vite, gh-pages)
Favicon
Deploy Express App to Render with MySQL
Favicon
Deploying on Netlify via GitHub Actions: A Seamless Guide
Favicon
AWS CodeDeploy Best Practices for Reliable and Efficient Deployments

Featured ones: