Menu
  • Home
  • About Me
  • Blog
  • Github
  • LinkedIn

Building Containerized Microservices in Golang Chapter 3 – Docker

May 15, 2022

I’m going to create two types of docker files. One for the local testing, and one for the production. Before creating the docker file, I want to create the docker ignore file because geometry and static folders are not used.

#.dockerignore

geometry/
static/
*.exe

Create dockerfile

FROM golang:latest

WORKDIR /app
COPY go.mod go.sum ./

RUN go mod download
COPY . .
RUN go build -o main .

EXPOSE 80
ENTRYPOINT [ "./main" ]

build and run

docker build -t go-app:latest .
docker run -d -p 80:80 --name web go-app:latest

The problem of this image has 974MB which is way too big for a simple application. I will use multi stage builds.

FROM golang:latest as builder

WORKDIR /app
COPY go.mod go.sum ./

RUN go mod download
COPY . .
RUN go build -o main .
# EXPOSE 80
# ENTRYPOINT [ "./main" ]

# Second stage
FROM gcr.io/distroless/base-debian11
COPY --from=builder /app/main .
EXPOSE 80
CMD ["/main"]

Much better! 👏

Docker-compose file

// docker-compose.yml
version: "3.9"
services:
  web:
    # build from here
    build:
      # the location where your dockerfile is existing
      context: .
      dockerfile: Dockerfile
    #name of image
    image: go-app-ms:latest
    ports:
      - "80:80"
    # always restart 
    restart: always
    networks:
      - web

networks:
  web:
docker-compose build
docker-compose up -d

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts
  • ChinguTalkBot v0.1.0: Setting up AWS Cognito with CDK for User Authentication
  • Phoenix & Elixir: Fix PostgreSQL connection refused
  • Demo: Git Log with Shell script to create a release notes
  • Metasploit
  • CyberSecurity Lab – Online Password Attack

Archives
  • March 2024
  • May 2023
  • April 2023
  • February 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
Categories
  • Amazon Interview (3)
  • Ansible (3)
  • AWS (9)
  • Azure (9)
  • Certification (2)
  • ChinguTalkBot Project (1)
  • cybersecurity (3)
  • Data analytics (6)
  • Demo Videos (6)
  • Docker (5)
  • Git (1)
  • GitLab (1)
  • Golang (3)
  • JavaScript (2)
  • Jenkins (4)
  • PowerShell (1)
  • Python (10)
  • Terraform (11)
  • Uncategorized (9)

©2025 | Powered by WordPress and Superb Themes!