Skip to content

Commit

Permalink
Added cross platform dockerfile
Browse files Browse the repository at this point in the history
Signed-off-by: Rishabh Gupta <r.g.gupta@outlook.com>
  • Loading branch information
zeerorg committed Jun 10, 2019
1 parent d8f72cc commit 50cd5c2
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 81 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ git:
# install qemu-user before running docker build
before_install:
- sudo docker run --privileged linuxkit/binfmt:v0.6
- sudo docker run -d --privileged -p 1234:1234 --name buildkit moby/buildkit:latest --addr tcp://0.0.0.0:1234 --oci-worker-platform linux/amd64 --oci-worker-platform linux/armhf
- sudo docker run -d --privileged -p 1234:1234 --name buildkit moby/buildkit:latest --addr tcp://0.0.0.0:1234 --oci-worker-platform linux/amd64 --oci-worker-platform linux/armhf --oci-worker-platform linux/arm64
- sudo docker cp buildkit:/usr/bin/buildctl /usr/bin/
- export BUILDKIT_HOST=tcp://0.0.0.0:1234

Expand Down
22 changes: 0 additions & 22 deletions Dockerfile.armhf

This file was deleted.

34 changes: 34 additions & 0 deletions Dockerfile.cross
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
FROM --platform=$BUILDPLATFORM golang:1.10.4-alpine as builder
RUN mkdir -p /go/src/github.com/zeerorg/cron-connector
WORKDIR /go/src/github.com/zeerorg/cron-connector

COPY vendor vendor
COPY types types
COPY main_test.go .
COPY main.go .

# Run a gofmt and exclude all vendored code.
RUN test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*"))"

RUN go test -v ./...

ARG TARGETARCH
ENV GOARCH=$TARGETARCH

# Stripping via -ldflags "-s -w"
RUN CGO_ENABLED=0 GOOS=linux GOARCH=$GOARCH go build -a -ldflags "-s -w" -installsuffix cgo -o ./connector

FROM alpine:3.8

RUN addgroup -S app \
&& adduser -S -g app app

WORKDIR /home/app

COPY --from=builder /go/src/github.com/zeerorg/cron-connector/connector .

RUN chown -R app:app ./

USER app

CMD ["./connector"]
67 changes: 38 additions & 29 deletions travis/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,45 @@
# Login into docker
docker login --username $DOCKER_USER --password $DOCKER_PASSWORD

# Build for amd64 and push
buildctl build --frontend dockerfile.v0 \
--local dockerfile=. \
--local context=. \
--exporter image \
--exporter-opt name=docker.io/zeerorg/cron-connector:test-build-amd64 \
--exporter-opt push=true \
--frontend-opt platform=linux/amd64 \
--frontend-opt filename=./Dockerfile


# Build for armhf and push
buildctl build --frontend dockerfile.v0 \
--local dockerfile=. \
--local context=. \
--exporter image \
--exporter-opt name=docker.io/zeerorg/cron-connector:test-build-armhf \
--exporter-opt push=true \
--frontend-opt platform=linux/armhf \
--frontend-opt filename=./Dockerfile.armhf

architectures="arm arm64 amd64"
images=""
platforms=""

export DOCKER_CLI_EXPERIMENTAL=enabled
for arch in $architectures
do
# Build for all architectures and push manifest
platforms="linux/$arch,$platforms"
done

# Create manifest list and push that
docker manifest create zeerorg/cron-connector:test-build \
zeerorg/cron-connector:test-build-amd64 \
zeerorg/cron-connector:test-build-armhf
platforms=${platforms::-1}

docker manifest annotate zeerorg/cron-connector:test-build zeerorg/cron-connector:test-build-armhf --arch arm
docker manifest annotate zeerorg/cron-connector:test-build zeerorg/cron-connector:test-build-amd64 --arch amd64

docker manifest push zeerorg/cron-connector:test-build
# Push multi-arch image
buildctl build --frontend dockerfile.v0 \
--local dockerfile=. \
--local context=. \
--exporter image \
--exporter-opt name=docker.io/zeerorg/cron-connector:test-build \
--exporter-opt push=true \
--frontend-opt platform=$platforms \
--frontend-opt filename=./Dockerfile.cross

# Push image for every arch with arch prefix in tag
for arch in $architectures
do
# Build for all architectures and push manifest
buildctl build --frontend dockerfile.v0 \
--local dockerfile=. \
--local context=. \
--exporter image \
--exporter-opt name=docker.io/zeerorg/cron-connector:test-build-$arch \
--exporter-opt push=true \
--frontend-opt platform=linux/$arch \
--frontend-opt filename=./Dockerfile.cross &
done

wait

docker pull zeerorg/cron-connector:test-build-arm
docker tag zeerorg/cron-connector:test-build-arm zeerorg/cron-connector:test-build-armhf
docker push zeerorg/cron-connector:test-build-armhf
67 changes: 38 additions & 29 deletions travis/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,45 @@
# Login into docker
docker login --username $DOCKER_USER --password $DOCKER_PASSWORD

# Build for amd64 and push
buildctl build --frontend dockerfile.v0 \
--local dockerfile=. \
--local context=. \
--exporter image \
--exporter-opt name=docker.io/zeerorg/cron-connector:${TRAVIS_TAG}-amd64 \
--exporter-opt push=true \
--frontend-opt platform=linux/amd64 \
--frontend-opt filename=./Dockerfile


# Build for armhf and push
buildctl build --frontend dockerfile.v0 \
--local dockerfile=. \
--local context=. \
--exporter image \
--exporter-opt name=docker.io/zeerorg/cron-connector:${TRAVIS_TAG}-arm \
--exporter-opt push=true \
--frontend-opt platform=linux/armhf \
--frontend-opt filename=./Dockerfile.armhf

architectures="arm arm64 amd64"
images=""
platforms=""

export DOCKER_CLI_EXPERIMENTAL=enabled
for arch in $architectures
do
# Build for all architectures and push manifest
platforms="linux/$arch,$platforms"
done

# Create manifest list and push that
docker manifest create zeerorg/cron-connector:${TRAVIS_TAG} \
zeerorg/cron-connector:${TRAVIS_TAG}-amd64 \
zeerorg/cron-connector:${TRAVIS_TAG}-arm
platforms=${platforms::-1}

docker manifest annotate zeerorg/cron-connector:${TRAVIS_TAG} zeerorg/cron-connector:${TRAVIS_TAG}-arm --arch arm
docker manifest annotate zeerorg/cron-connector:${TRAVIS_TAG} zeerorg/cron-connector:${TRAVIS_TAG}-amd64 --arch amd64

docker manifest push zeerorg/cron-connector:${TRAVIS_TAG}
# Push multi-arch image
buildctl build --frontend dockerfile.v0 \
--local dockerfile=. \
--local context=. \
--exporter image \
--exporter-opt name=docker.io/zeerorg/cron-connector:$TRAVIS_TAG \
--exporter-opt push=true \
--frontend-opt platform=$platforms \
--frontend-opt filename=./Dockerfile.cross

# Push image for every arch with arch prefix in tag
for arch in $architectures
do
# Build for all architectures and push manifest
buildctl build --frontend dockerfile.v0 \
--local dockerfile=. \
--local context=. \
--exporter image \
--exporter-opt name=docker.io/zeerorg/cron-connector:$TRAVIS_TAG-$arch \
--exporter-opt push=true \
--frontend-opt platform=linux/$arch \
--frontend-opt filename=./Dockerfile.cross &
done

wait

docker pull zeerorg/cron-connector:$TRAVIS_TAG-arm
docker tag zeerorg/cron-connector:$TRAVIS_TAG-arm zeerorg/cron-connector:$TRAVIS_TAG-armhf
docker push zeerorg/cron-connector:$TRAVIS_TAG-armhf

0 comments on commit 50cd5c2

Please sign in to comment.