Improve Docker build

This commit is contained in:
Tomasz Sęk 2019-12-16 16:54:54 +01:00
parent e9f5df2eb3
commit 667d263f7d
No known key found for this signature in database
GPG Key ID: DC356D23F6A644D0
4 changed files with 21 additions and 99 deletions

View File

@ -1,53 +0,0 @@
FROM docker:18.09
ARG GO_VERSION
ARG OPERATOR_SDK_VERSION
ARG MINIKUBE_VERSION
ARG GOPATH="/go"
RUN mkdir -p /go
# Stage 1 - Install dependencies
RUN apk update && \
apk add --no-cache \
curl \
python \
py-crcmod \
bash \
libc6-compat \
openssh-client \
git \
make \
gcc \
libc-dev \
git \
mercurial
RUN curl -O https://storage.googleapis.com/golang/go$GO_VERSION.linux-amd64.tar.gz && tar -xvf go$GO_VERSION.linux-amd64.tar.gz
# Stage 2 - Install operator-sdk
RUN echo $GOPATH/bin/operator-sdk
RUN curl -L https://github.com/operator-framework/operator-sdk/releases/download/v$OPERATOR_SDK_VERSION/operator-sdk-v$OPERATOR_SDK_VERSION-x86_64-linux-gnu -o $GOPATH/bin/operator-sdk \
&& chmod +x $GOPATH/bin/operator-sdk
RUN curl -Lo minikube https://storage.googleapis.com/minikube/releases/v$MINIKUBE_VERSION/minikube-linux-amd64 \
&& chmod +x minikube \
&& cp minikube /usr/local/bin/ \
&& rm minikube
RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl \
&& chmod +x ./kubectl \
&& mv ./kubectl /usr/local/bin/kubectl
RUN export GO111MODULE=auto
RUN mkdir -p $GOPATH/src/github.com/jenkinsci/kubernetes-operator
WORKDIR $GOPATH/src/github.com/jenkinsci/kubernetes-operator
RUN mkdir -p /home/builder
ENV DOCKER_TLS_VERIFY 1
ENV DOCKER_CERT_PATH /minikube/certs
ENTRYPOINT ["./entrypoint.sh"]

View File

@ -293,7 +293,11 @@ docker-login: ## Log in into the Docker repository
.PHONY: docker-build
docker-build: check-env ## Build the container
@echo "+ $@"
docker build . -t $(DOCKER_REGISTRY):$(GITCOMMIT) --file build/Dockerfile
docker build \
--build-arg GO_VERSION=$(GO_VERSION) \
--build-arg OPERATOR_SDK_VERSION=$(OPERATOR_SDK_VERSION) \
-t $(DOCKER_REGISTRY):$(GITCOMMIT) . \
--file build/Dockerfile
.PHONY: docker-images
docker-images: ## List all local containers
@ -415,30 +419,6 @@ endif
go mod vendor -v
@echo
.PHONY: image
image: ## Create the docker image from the Dockerfile. This image is used to build linux binary regardless of the system on the host
@echo "+ $@"
docker build --rm --force-rm --no-cache \
--build-arg GO_VERSION=$(GO_VERSION) \
--build-arg MINIKUBE_VERSION=$(MINIKUBE_VERSION) \
--build-arg OPERATOR_SDK_VERSION=$(OPERATOR_SDK_VERSION) \
-t jenkins-operator/runner .
.PHONY: indocker
PWD := $(shell pwd)
DOCKER_HOST_IP := $(shell minikube docker-env | grep DOCKER_HOST | cut -d '"' -f 2 2> /dev/null)
MINIKUBE_IP := $(shell minikube ip 2> /dev/null)
indocker: minikube-start image ## Run make in a docker container
@echo "+ $@"
docker run --rm -it $(DOCKER_FLAGS) \
-v /var/run/docker.sock:/var/run/docker.sock \
--mount type=bind,source=$(PWD),target=/go/src/github.com/jenkinsci/kubernetes-operator \
--mount type=bind,source=$(HOME)/.minikube,target=/minikube \
--mount type=bind,source=$(HOME)/.kube,target=/home/builder/.kube \
-e DOCKER_HOST=$(DOCKER_HOST_IP) \
-e MINIKUBE_IP=$(MINIKUBE_IP) \
jenkins-operator/runner
.PHONY: travis-prepare
travis-prepare:
@echo "+ $@"

View File

@ -1,7 +1,21 @@
FROM alpine:3.8
ARG GO_VERSION
# build stage
FROM golang:$GO_VERSION-alpine3.10 AS build-stage
ARG OPERATOR_SDK_VERSION
ENV GO111MODULE=on
RUN apk --no-cache add git curl make \
&& curl -L https://github.com/operator-framework/operator-sdk/releases/download/v$OPERATOR_SDK_VERSION/operator-sdk-v$OPERATOR_SDK_VERSION-x86_64-linux-gnu -o /usr/local/bin/operator-sdk \
&& chmod +x /usr/local/bin/operator-sdk
ADD . /kubernetes-operator
RUN cd /kubernetes-operator && make build
# run stage
FROM alpine:3.10
USER nobody
ADD build/_output/bin/jenkins-operator /usr/local/bin/jenkins-operator
COPY --from=build-stage /kubernetes-operator/build/_output/bin/jenkins-operator /usr/local/bin/jenkins-operator
CMD [ "/usr/local/bin/jenkins-operator" ]

View File

@ -1,19 +0,0 @@
#!/bin/bash
export GOPATH=/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
export GO111MODULE=on
kubectl config set-cluster minikube --server=https://$MINIKUBE_IP:8443 \
--certificate-authority=/minikube/ca.crt && \
kubectl config set-credentials minikube --certificate-authority=/root/.minikube/ca.crt \
--client-key=/minikube/client.key \
--client-certificate=/minikube/client.crt && \
kubectl config set-context minikube --cluster=minikube --user=minikube && \
kubectl config use-context minikube
make go-dependencies
ln -s $GOPATH/src/github.com/jenkinsci/kubernetes-operator/vendor/k8s.io $GOPATH/src/k8s.i
ln -s $GOPATH/src/github.com/jenkinsci/kubernetes-operator/vendor/sigs.k8s.io $GOPATH/src/sigs.k8s.io
bash