Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dockerfile to make an official image #89

Closed
wants to merge 1 commit into from

Conversation

sevki
Copy link

@sevki sevki commented Apr 16, 2019

Closes #62

Dockerfile Outdated
FROM golang:1.12 as builder
WORKDIR /go/src/github.com/cloudflare/cloudflared/
ADD . .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o cloudflared ./cmd/cloudflared

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you build with cgo disabled, compression will no longer work. Compression relies on a cgo brotli implementation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks Matt. I'll update it. I guess that also means we can't use alpine 😄 since last I checked musl didn't work well with cgo

Dockerfile Outdated
ADD . .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o cloudflared ./cmd/cloudflared

FROM alpine:latest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably set this to the same version that golang:1.12 leans on.

Edit: never mind, 1.12 is debian.

@jbergstroem
Copy link
Contributor

jbergstroem commented Apr 29, 2019

Spent some time on this. Here's an alpine image that attempts to reproduce the build settings found in Makefile. I also shave off some megabytes (about half) with upx. In short, changes from PR:

  1. Use alpine consistently
  2. Build in-vitro (avoid inheriting different os artifacts)
  3. Minor change to datestring to validate with ISO
  4. copy ca-certificates from builder
  5. switch to entrypoint/cmd for a more flexible usage (docker run -it foo:1.0 service)

Edit: fix building with specific refspec, show image size and .dockerignore.

FROM golang:1.12-alpine as builder

ARG REPO_URL="https://github.com/cloudflare/cloudflared.git"
ARG REFERENCE=""

WORKDIR /go/src/github.com/cloudflare/cloudflared/
RUN apk add --no-cache git gcc musl-dev upx ca-certificates
RUN [ -n "$REFERENCE" ] && REF="-b $REFERENCE" || REF=""; \
	git clone --depth=1 ${REF} ${REPO_URL} .
RUN VERSION=$(git describe --tags --always --dirty="-dev") && \
	DATE=$(date -u '+%Y-%m-%dT%H:%MZ') && \
	go build -ldflags "-X main.Version=\"${VERSION}\" -X main.BuildTime=\"${DATE}\"" \
			 -installsuffix cgo -o cloudflared ./cmd/cloudflared
RUN upx --no-progress cloudflared

FROM alpine:3.9
COPY --from=builder /go/src/github.com/cloudflare/cloudflared/cloudflared /usr/local/bin/
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
ENTRYPOINT ["cloudflared"]
CMD ["version"]

Build output (passing a reference tag):

$ docker build --build-arg REFERENCE=2019.4.0 -t foo:1.0 .
Sending build context to Docker daemon   2.56kB
Step 1/13 : FROM golang:1.12-alpine as builder
 ---> b97a72b8e97d
Step 2/13 : ARG REPO_URL="https://github.com/cloudflare/cloudflared.git"
 ---> Using cache
 ---> 26df77ebce2a
Step 3/13 : ARG REFERENCE=""
 ---> Using cache
 ---> 559ae0a45c8b
Step 4/13 : WORKDIR /go/src/github.com/cloudflare/cloudflared/
 ---> Using cache
 ---> 7765d138af8c
Step 5/13 : RUN apk add --no-cache git gcc musl-dev upx ca-certificates
 ---> Using cache
 ---> 0e7ce0b477e0
Step 6/13 : RUN	[ -n "$REFERENCE" ] && REF="-b $REFERENCE" || REF=""; 	git clone --depth=1 ${REF} ${REPO_URL} .
 ---> Running in 0df0fae35715
Cloning into '.'...
Note: checking out '980bee22a50c1f8291201a96f33b96a5ee74492a'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

Removing intermediate container 0df0fae35715
 ---> 208f7e24c3f2
Step 7/13 : RUN VERSION=$(git describe --tags --always --dirty="-dev") && 	DATE=$(date -u '+%Y-%m-%dT%H:%MZ') && 	go build -ldflags "-X main.Version=\"${VERSION}\" -X main.BuildTime=\"${DATE}\"" 			 -installsuffix cgo -o cloudflared ./cmd/cloudflared
 ---> Running in f5f2f090fcc8
Removing intermediate container f5f2f090fcc8
 ---> 1c11179ed242
Step 8/13 : RUN upx --no-progress cloudflared
 ---> Running in 9191d595a1c1
                       Ultimate Packer for eXecutables
                          Copyright (C) 1996 - 2018
UPX 3.95        Markus Oberhumer, Laszlo Molnar & John Reiser   Aug 26th 2018

        File size         Ratio      Format      Name
   --------------------   ------   -----------   -----------
  28675360 ->  15037104   52.44%   linux/amd64   cloudflared

Packed 1 file.
Removing intermediate container 9191d595a1c1
 ---> a35b1a051b5f
Step 9/13 : FROM alpine:3.9
 ---> cdf98d1859c1
Step 10/13 : COPY --from=builder /go/src/github.com/cloudflare/cloudflared/cloudflared /usr/local/bin/
 ---> 03fd84f7fde8
Step 11/13 : COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
 ---> 3e4c3e0c92f7
Step 12/13 : ENTRYPOINT ["cloudflared"]
 ---> Running in f19f1fb10c71
Removing intermediate container f19f1fb10c71
 ---> 04c55db24bab
Step 13/13 : CMD ["version"]
 ---> Running in 568fff95e842
Removing intermediate container 568fff95e842
 ---> 6d9e4fa85f46
Successfully built 6d9e4fa85f46
Successfully tagged foo:1.0
$ docker run -it foo:1.0                     
cloudflared version "2019.4.0" (built "2019-04-29T17:55Z")
$ docker images --filter reference=foo   
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
foo                 1.0                 6d9e4fa85f46        4 minutes ago       20.8MB
$ cat .dockerignore
*

jgmize added a commit to mozmeao/cloudflared that referenced this pull request Apr 29, 2019
@hazcod
Copy link
Contributor

hazcod commented Jun 9, 2019

Linking to my scratch image: https://github.com/hazcod/argo/blob/master/Dockerfile

@sssilver sssilver closed this Jun 12, 2019
@sssilver
Copy link
Contributor

Closing this in favor of #90

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Publish official docker image
5 participants