kha7iq/pingme

Make Dockerfile to build from source

pataquets opened this issue · 6 comments

The current Dockerfile just copies the binary built outside of Docker to the image. It's not a source build (which is the most common use).

An official, built-in Dockerfile which allows to build a Docker image directly from source would be more useful.
Case in point: I wanted to test @ahdekkers' PR #65 and since building is quite an involved process for me and my Golang host OS' packages are outdated, I looked for a Dockerfile to build an image and attempted to build directly from the PR:

docker build --force-rm -t pingme github.com/kha7iq/pingme#pull/65/head

Unfortunately, since it's not a source build and it expects the binary to be built outside of the Docker workflow, it didn't work for me.

Potential use-cases for a source build Dockerfile:

  • Running: With a proper Dockerfile, you could just issue a docker build -t pingme github.com/kha7iq/pingme#master to locally build a Docker image ready to run with a simple docker run. No build tools needed, no packaging, not even cloning the repo! And it would be straight from source (any desired branch or tag).
  • Development: You can start hacking right away with just Docker installed without needing any further toolchain on your system, since the only thing needed to test your code is docker build ....
  • Distribution: You can use the same workflow which works locally to build the images and push them to Docker Hub (even for other architectures) with a simple Github Action. Thus, you'll have a public Docker image with the latest code on HEAD (aka. daily builds).
  • Documentation: since the Dockerfile should install all required build tools, libraries and other packages and would require also to run all the required build commands, it would serve as reference to the build process.

I'm happy to give this a go

Great! Let's see if @kha7iq is OK with adding this before investing time, since I can also help with it.
Once this is going, don't hesitate to ping me for review, if you feel like it or you want help with the Docker part.

Hello @pataquets
Its certainly possible to add a Dockerfile which can build from sources, at the moment the docker build is a part of release workflow with goreleaser once the binary is built from sources its then copied to image.
You are right it will be easier to build from sources to do any kind of testing, let me see what will be the best way to do this. Having 2 Dockerfiles in the repo will be confusing.

Thanks for the prompt feedback, @kha7iq .
I would add that, other than testing, a Dockerfile would enable easier contribution to development by ease code/build/run workflow.

I'm not too familiar with goreleaser, but at first guess I see two approaches (surely, might be some more I haven't thought of):

  • Run goreleaser inside the Docker container. Not the leanest if you want multi-arch Docker images, AFAICT.
  • Drop goreleaser and build the code without it, just on the Docker build. Yup, at first sight, that would mean two different CI actions, unless we figure out how to do it from Docker, which doesn't sound too far-fetched, thou.

Since I'm not sure what goreleaser brings to the table (other than a cursory read at your link), the Docker build can match what's being done now:

  • Can build and push multi-arch Docker images
  • Can produce very small images as done currently, no matter how big the build image is, by using multi-stage builds.

I'm sure there are other advantages on using goreleaser which I can't grasp, so let me know if you see unsolved issues, and I'll try to help as best as I can.

goreleaser has a workflow setup right now, it builds the binaries for linux,mac,windows and deb and rpms with multi arch. you can have a look at the release page
It also generate change log and update scoop for windows & brew for macos along with a docker image.

Here is a Dockerfile if you would like to build from sources, the release will use the existing one as its part of goreleaser

FROM golang:1.18.4-alpine AS builder

WORKDIR /

RUN apk add --no-cache git && \
    git clone https://github.com/kha7iq/pingme.git && \
    cd /pingme && \
    go build -o pingme .


FROM alpine:3.7 as final
COPY --from=builder /pingme/pingme /usr/bin/pingme

ENTRYPOINT ["/usr/bin/pingme"]