Skip to content

Commit

Permalink
Upgrade Docker during build
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Oct 29, 2020
1 parent af5b053 commit aafd7b7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
@@ -1,13 +1,20 @@
sudo: required
language: go

dist: bionic

go:
- "1.13"

services:
- docker

before_script:
- ./hack/install-docker.sh
- ./hack/install-buildx.sh

script:
- docker buildx version
- TAG=${TRAVIS_TAG:=latest} make docker-build

before_deploy:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Expand Up @@ -24,7 +24,7 @@ RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} GO111MODULE=on go build

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM --platform=${TARGETPLATFORM:-linux/amd64} gcr.io/distroless/static:nonroot
FROM --platform=${BUILDPLATFORM:-linux/amd64} gcr.io/distroless/static:nonroot
WORKDIR /
COPY --from=builder /workspace/manager .
USER nonroot:nonroot
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Expand Up @@ -37,11 +37,11 @@ uninstall: manifests

# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
deploy: manifests
cd config/manager && kustomize edit set image controller=alexellis2/registry-creds-controller${TAG}
cd config/manager && kustomize edit set image controller=alexellis2/registry-creds-controller:$(TAG)
kustomize build config/default | kubectl apply -f -

shrinkwrap:
cd config/manager && kustomize edit set image controller=alexellis2/registry-creds-controller${TAG}
cd config/manager && kustomize edit set image controller=alexellis2/registry-creds-controller:$(TAG)
kustomize build config/default > mainfest.yaml

# Generate manifests e.g. CRD, RBAC etc.
Expand All @@ -65,7 +65,7 @@ docker-build:
@docker buildx create --use --name=multiarch --node=multiarch && \
docker buildx build \
--output "type=docker,push=false" \
--tag alexellis2/registry-creds-controller$(TAG) \
--tag alexellis2/registry-creds-controller:$(TAG) \
.

.PHONY: docker-publish # Push the docker image to the remote registry
Expand All @@ -74,7 +74,7 @@ docker-publish:
docker buildx build \
--platform linux/amd64,linux/arm/v7,linux/arm64 \
--output "type=image,push=true" \
--tag alexellis2/registry-creds-controller$(TAG) .
--tag alexellis2/registry-creds-controller:$(TAG) .

# find or download controller-gen
# download controller-gen if necessary
Expand Down
39 changes: 39 additions & 0 deletions hack/install-buildx.sh
@@ -0,0 +1,39 @@
#!/usr/bin/env bash

# Source: https://www.docker.com/blog/multi-arch-build-what-about-travis/
if [ -n "$DEBUG" ]; then
set -x
fi

set -o errexit
set -o nounset
set -o pipefail

export DOCKER_CLI_EXPERIMENTAL=enabled

if ! docker buildx 2>&1 >/dev/null; then
echo "buildx not available. Docker 19.03 or higher is required with experimental features enabled"
exit 1
fi

# We can skip setup if the current builder already has multi-arch
# AND if it isn't the docker driver, which doesn't work
current_builder="$(docker buildx inspect)"
# linux/amd64, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6
if ! grep -q "^Driver: docker$" <<<"${current_builder}" && \
grep -q "linux/amd64" <<<"${current_builder}" && \
grep -q "linux/arm" <<<"${current_builder}" && \
grep -q "linux/arm64" <<<"${current_builder}"; then
exit 0
fi

# Ensure qemu is in binfmt_misc
# Docker desktop already has these in versions recent enough to have buildx
# We only need to do this setup on linux hosts
if [ "$(uname)" == 'Linux' ]; then
# NOTE: this is pinned to a digest for a reason!
docker run --rm --privileged multiarch/qemu-user-static@sha256:28ebe2e48220ae8fd5d04bb2c847293b24d7fbfad84f0b970246e0a4efd48ad6 --reset -p yes
fi

# Ensure we use a builder that can leverage it (the default on linux will not)
docker buildx create --use --name=multiarch --node multiarch
14 changes: 14 additions & 0 deletions hack/install-docker.sh
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

# Source: https://www.docker.com/blog/multi-arch-build-what-about-travis/

sudo rm -rf /var/lib/apt/lists/*
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) edge"
sudo apt-get update -qy
sudo apt-get -y -o Dpkg::Options::="--force-confnew" install docker-ce
mkdir -vp ~/.docker/cli-plugins/
curl --silent -L "https://github.com/docker/buildx/releases/download/v0.3.0/buildx-v0.3.0.linux-amd64" > ~/.docker/cli-plugins/docker-buildx
chmod a+x ~/.docker/cli-plugins/docker-buildx
sudo systemctl start docker

0 comments on commit aafd7b7

Please sign in to comment.