Wayan Jimmy's Brain

Assignment: Build Your Own Image

related
Docker Mastery

Assignment

  • Take existing Golang app and Dockerize it
  • Make Dockerfile. Build it. Test it. Push it. (rm it). Run it.
  • Expect this to be iterative. Rarely do I get it right the first time.
  • Use the alpine image.

Answer

In this case I’m using Podman, instead of Docker for the sake of learning.

Clone my personal project yulje

git clone git@github.com:wayanjimmy/yulje.git

Write Containerfile

Write build.sh script for injecting version for variable at build time.

GIT_COMMIT=$(git rev-list -1 main)
go build -ldflags "-X main.GitCommit=$GIT_COMMIT" -o app yulje/cmd/yulje

Integrate build.sh to Containerfile

FROM docker.io/golang:1.16-alpine AS build

RUN apk update && apk add --no-cache git

WORKDIR /build

COPY .git .
COPY . .

RUN sh build.sh

FROM docker.io/alpine:latest AS final

COPY --from=build /build/app /app

ENTRYPOINT ["/app"]

Build the container

podman build -t wayanjimmy/yulje:latest .

Create a pod

Since the web server on the project yulje exposing port 8080, I need to publish the port.

podman pod create -p 8080:8080

Run the container inside the pod

Run with detached mode

podman run -d -e DATABASE_URL=mysql://user:password@host:port/database_name -d --pod pod_name wayanjimmy/yulje:latest