Running Golang with local Docker and Kubernetes

qomarullah
3 min readDec 27, 2019

Simple tutorial

  1. Install docker local desktop
https://docs.docker.com/docker-for-mac/install/

2. Add Dockerfile under your go project

# Dockerfile References: https://docs.docker.com/engine/reference/builder/# Start from the latest golang base image
FROM golang:latest as builder
# Add Maintainer Info
LABEL maintainer="qomarullah <qomarullah.mail@gmail.com>"
# Set the Current Working Directory inside the container
WORKDIR /app
# Copy go mod and sum files
COPY go.mod go.sum ./
# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed
RUN go mod download
# Copy the source from the current directory to the Working Directory inside the container
COPY . .
# Build the Go app
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
######## Start a new stage from scratch #######
FROM alpine:latest
RUN apk --no-cache add ca-certificatesWORKDIR /root/# Copy the Pre-built binary file from the previous stage
COPY --from=builder /app/main .
# Expose port 8080 to the outside world
EXPOSE 8080
# Command to run the executable
CMD ["./main"]

3. Build a docker image

docker build -t <docker-image-name> .

verify with

docker image -ls

4. Run as container

docker run -d -p <expose-port>:<internal-port> <docker-image-name>

verify with

docker container ls# stop with 
docker container stop <container-id>
# test with
curl to localhost port exposed

5. Debugging

check log error with

docker logs -f <container-id>

this is error where my app because resource file is not copy yet to container

how to solve is check inside container by login inside container with this command

docker exec -it <container-id> /bin/sh

the solution is adding command copy folder config inside Dockerfile

# mkdir
RUN mkdir -p /root/config
# Copy config
COPY --from=builder /app/config/ config/.

Finally, logs container looks ok

6. Deployment in local Kubernetes with minikube,

you can follow this tutorial :)

Deploy dashboard

minikube dashboard

Thanks for reading..

--

--