docker/go alpine 빌드&Hub 배포

Quick Start

go 프로그램 작성 #

예제) main.go
  // 중략
  r := gin.Default()
  r.GET("/", func(c *gin.Context) {
    c.JSON(200, gin.H{
      "message": "Server is running well.",
    })
  })
...
...
func main() {
  r := setupRouter()
  r.Run(":30001") // Listen and Serve
}

Dockerfile 작성 #

Dockerfile
# base image
FROM golang:alpine AS builder

# set environment variables
ENV GO111MODULE=on \
    CGO_ENABLED=0 \
    GOOS=linux \
    GOARCH=amd64 \
    APP_PORT=30001

# create app directory
RUN mkdir /app
WORKDIR /app

# copy go.mod and go.sum files
COPY go.mod go.sum main.go ./

# install dependencies
RUN go mod download

# copy app source code
COPY . .

# build the app
RUN go build -o main .

# app image
FROM scratch
COPY --from=builder /app/main .

# expose port
EXPOSE $APP_PORT

# command to run the app
CMD ["./main"]

login #

$ docker login

build #

$ docker build -t yhyacinth/myapp-go .
※ -t <hub 계정이름>/<hub 리포지토리 이름>:<태그>

tagging #

$ docker tag myapp-go:latest yhyacinth/myapp-go:v0.1

push #

$ docker push yhyacinth/myapp-go:v0.1

이 글에는 0 개의 댓글이 있습니다.