From d87ea71c2df81ab060d2dec8437aebd47c1d62e2 Mon Sep 17 00:00:00 2001 From: Jon Edvald Date: Thu, 23 Jan 2020 00:09:20 +0100 Subject: [PATCH] fix(build): error in alpine container image builds Also making the build more efficient. --- .circleci/config.yml | 86 ++++++++++++++++++++++++-- garden-service/Dockerfile | 60 +++++++++++------- garden-service/bin/build-containers.sh | 12 ++-- garden-service/bin/push-containers.sh | 1 + 4 files changed, 126 insertions(+), 33 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 4bc29615c1..e6e644cdba 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -97,10 +97,9 @@ commands: chmod +x kubectl echo 'export PATH=$HOME/bin:$PATH' >> $BASH_ENV - configure_kubectl_context: - description: Configure the kubectl context so that we can access our remote cluster. Used for e2e testing. + install_gcloud: + description: Install the gcloud SDK steps: - - install_kubectl - run: name: Install gcloud command: | @@ -108,6 +107,10 @@ commands: curl https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz | tar xz -C $HOME/gcloud $HOME/gcloud/google-cloud-sdk/install.sh --quiet echo 'export PATH=$HOME/gcloud/google-cloud-sdk/bin:$PATH' >> $BASH_ENV + + configure_kubectl_context: + description: Configure the kubectl context via gcloud so that we can access our remote cluster. Used for e2e testing. + steps: - run: name: Configure kubectl context via gcloud and authenticate to Google Container Registry command: | @@ -215,6 +218,8 @@ jobs: - checkout - run: sudo apt-get update && sudo apt-get install rsync - npm_install + - install_kubectl + - install_gcloud - configure_kubectl_context - *attach-workspace - run: @@ -239,6 +244,8 @@ jobs: - checkout - run: sudo apt-get update && sudo apt-get install rsync - npm_install + - install_kubectl + - install_gcloud - configure_kubectl_context - *attach-workspace - run: @@ -270,12 +277,63 @@ jobs: - setup_remote_docker: docker_layer_caching: true - checkout - # This is to copy the pre-build build output from test-service (this should be a different step) - *attach-workspace - run: - name: Build docker images + name: Build image and push to registry + command: | + docker login -u $DOCKER_USER -p $DOCKER_PASS + TAG=gardendev/garden:${CIRCLE_SHA1} + cd garden-service + docker build -t ${TAG} -f Dockerfile . + docker push ${TAG} + build-docker-gcloud: + <<: *node-config + steps: + - setup_remote_docker: + docker_layer_caching: true + - checkout + - *attach-workspace + - run: + name: Build image and push to registry + command: | + docker login -u $DOCKER_USER -p $DOCKER_PASS + TAG=gardendev/garden-gcloud:${CIRCLE_SHA1} + cd garden-service + docker build -t ${TAG} --build-arg TAG=${CIRCLE_SHA1} -f gcloud.Dockerfile . + docker push ${TAG} + build-docker-buster: + <<: *node-config + steps: + - setup_remote_docker: + docker_layer_caching: true + - checkout + - *attach-workspace + - run: + name: Build image and push to registry command: | - ./garden-service/bin/build-containers.sh + docker login -u $DOCKER_USER -p $DOCKER_PASS + TAG=gardendev/garden:${CIRCLE_SHA1}-buster + cd garden-service + docker build -t ${TAG} -f buster.Dockerfile dist/linux-amd64 + test-docker-gcloud: + docker: + - image: gardendev/garden-gcloud:${CIRCLE_SHA1} + environment: + GARDEN_DISABLE_VERSION_CHECK: "true" + GARDEN_TASK_CONCURRENCY_LIMIT: "10" + steps: + # Need to checkout to run example project + - checkout + - configure_kubectl_context + - *attach-workspace + - run: + name: Deploy demo-project with container + # overriding CIRCLE_BUILD_NUM to avoid conflict with other tests + command: CIRCLE_BUILD_NUM=$CIRCLE_BUILD_NUM-docker /garden/garden build --root examples/demo-project --env remote --logger-type basic + - run: + name: Cleanup + command: CIRCLE_BUILD_NUM=$CIRCLE_BUILD_NUM-docker kubectl delete --wait=false $(kubectl get ns -o name | grep testing-$CIRCLE_BUILD_NUM) || true + when: always release-service-docker: <<: *node-config steps: @@ -344,6 +402,8 @@ jobs: steps: # Need to checkout to run example project - checkout + - install_kubectl + - install_gcloud - configure_kubectl_context - *attach-workspace - run: sudo apt-get update && sudo apt-get -y install rsync @@ -486,7 +546,21 @@ workflows: - lint: requires: [build] - build-docker: + <<: *only-internal-prs + context: docker + requires: [build] + - build-docker-gcloud: + <<: *only-internal-prs + context: docker + requires: [build-docker] + - build-docker-buster: + <<: *only-internal-prs + context: docker requires: [build-dist] + - test-docker-gcloud: + <<: *only-internal-prs + context: docker + requires: [build-docker-gcloud] - test-service: requires: [build] - test-dist: diff --git a/garden-service/Dockerfile b/garden-service/Dockerfile index a68de9e349..07ae2ebe70 100644 --- a/garden-service/Dockerfile +++ b/garden-service/Dockerfile @@ -1,6 +1,39 @@ -FROM node:12.13.1-alpine +ARG NODE_VERSION=12.13.1-alpine +FROM node:${NODE_VERSION} as builder + +RUN apk add --no-cache \ + ca-certificates \ + git \ + gzip \ + libstdc++ \ + openssl \ + tar + +WORKDIR /tmp + +RUN npm install pkg@4.4.2 && node_modules/.bin/pkg-fetch node12 alpine x64 + +ADD package.json /tmp/ +ADD package-lock.json /tmp/ + +RUN npm install \ + && rm -rf /root/.npm/* \ + /usr/lib/node_modules/npm/man/* \ + /usr/lib/node_modules/npm/doc/* \ + /usr/lib/node_modules/npm/html/* \ + /usr/lib/node_modules/npm/scripts/* + +ADD bin /tmp/bin +ADD build /tmp/build + +RUN node_modules/.bin/pkg --target node12-alpine-x64 . \ + && mkdir -p /garden \ + && mv garden-service /garden/garden \ + && cp node_modules/sqlite3/lib/binding/node-v72-linux-x64/node_sqlite3.node /garden + +#### Main container #### +FROM node:${NODE_VERSION} -# system dependencies RUN apk add --no-cache \ bash \ curl \ @@ -13,30 +46,15 @@ RUN apk add --no-cache \ gzip \ libstdc++ -VOLUME ["~/.pkg-cache"] - -ADD bin /tmp/bin -ADD build /tmp/build -ADD package.json /tmp/ - -# build binary (need to do this in-container for alpine due to a bug in zeit/pkg) -RUN cd /tmp \ - && npm install \ - && node_modules/.bin/pkg --target node12-alpine-x64 . \ - && mkdir -p /garden \ - && mv garden-service /garden/garden \ - && rm -rf node_modules \ - /root/.npm/* \ - /usr/lib/node_modules/npm/man/* \ - /usr/lib/node_modules/npm/doc/* \ - /usr/lib/node_modules/npm/html/* \ - /usr/lib/node_modules/npm/scripts/* +COPY --from=builder /garden /garden ADD static /garden/static +RUN cd /garden/static && git init WORKDIR /project -RUN ln -s /garden/garden /bin/garden \ +RUN chmod +x /garden/garden \ + && ln -s /garden/garden /bin/garden \ && chmod +x /bin/garden ENTRYPOINT ["/garden/garden"] diff --git a/garden-service/bin/build-containers.sh b/garden-service/bin/build-containers.sh index 090a9c5801..b4e6997da4 100755 --- a/garden-service/bin/build-containers.sh +++ b/garden-service/bin/build-containers.sh @@ -14,17 +14,17 @@ buster_tag=gardendev/garden:${version}-buster echo "Building version ${version}" echo "-> Build ${base_tag}" -docker build -t gardendev/garden:${version} -f Dockerfile . +docker build -t ${base_tag} -f Dockerfile . echo "-> Check ${base_tag}" -docker run --rm -it gardendev/garden:${version} version +docker run --rm -it ${base_tag} version echo "-> Build ${gcloud_tag}" -docker build -t gardendev/garden-gcloud:${version} --build-arg TAG=${version} -f gcloud.Dockerfile . +docker build -t ${gcloud_tag} --build-arg TAG=${version} -f gcloud.Dockerfile . echo "-> Check ${gcloud_tag}" -docker run --rm -it gardendev/garden-gcloud:${version} version +docker run --rm -it ${gcloud_tag} version echo "-> Build ${buster_tag}" -docker build -t gardendev/garden:${version}-buster -f buster.Dockerfile dist/linux-amd64 +docker build -t ${buster_tag} -f buster.Dockerfile dist/linux-amd64 echo "-> Check ${buster_tag}" -docker run --rm -it gardendev/garden:${version}-buster version +docker run --rm -it ${buster_tag} version diff --git a/garden-service/bin/push-containers.sh b/garden-service/bin/push-containers.sh index d747e487ff..4912845c35 100755 --- a/garden-service/bin/push-containers.sh +++ b/garden-service/bin/push-containers.sh @@ -13,3 +13,4 @@ echo "Pushing images" docker push gardendev/garden:${version} docker push gardendev/garden-gcloud:${version} +docker push gardendev/garden:${version}-buster