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