Skip to content

Commit

Permalink
build: make the Docker image smaller (#468)
Browse files Browse the repository at this point in the history
* stop copying everything!

* clean up and improve overall Docker workflow

* stop running Docker tests in CI

* rename CI workflow

* shave an extra 27MB with `pip install --no-cache-dir ...`

* shave an extra 75MB with Alpine image

* chore: add missing variables in sample environment file

* stop running Docker tests in Cloud Build

* clean up and reformat `Makefile`

* clean up `Makefile` and GitHub Worflows
  • Loading branch information
lvaylet authored May 17, 2024
1 parent f956e0a commit 90d2797
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 79 deletions.
40 changes: 40 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Git
.git
.gitignore
.gitattributes
.github

# Docker
Dockerfile
.dockerignore

# Byte-compiled / optimized / DLL files
**/__pycache__/
**/*.py[cod]

build/
dist/

# Testing / Linting
.mypy_cache/
.pytest_cache/
.pytype/
.ruff_cache/
.coverage
tests/

# Documentation
docs/

# Configuration
.env

# Virtual Environments
.venv/
venv/

# VS Code
.vscode/

# Cloud Build
cloudbuild.yaml
6 changes: 3 additions & 3 deletions .github/workflows/build-and-push-to-gcr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
- v*.*.*

jobs:
cloudbuild:
cloud-build:
runs-on: ubuntu-latest
environment: prod
steps:
Expand Down Expand Up @@ -39,14 +39,14 @@ jobs:
project_id: ${{ secrets.PROJECT_ID }}

- name: Build Docker container and publish on GCR
run: make cloudbuild || true
run: make cloud_build || true
env:
GCR_PROJECT_ID: ${{ secrets.GCR_PROJECT_ID }}
CLOUDBUILD_PROJECT_ID: ${{ secrets.CLOUDBUILD_PROJECT_ID }}
VERSION: ${{ steps.check-tag.outputs.match == 'true' && steps.check-tag.outputs.version || github.event.pull_request.head.sha || github.sha }}

- name: Build Docker container and publish on GCR [latest]
run: make cloudbuild || true
run: make cloud_build || true
if: ${{ steps.check-tag.outputs.match == 'true' }}
env:
GCR_PROJECT_ID: ${{ secrets.GCR_PROJECT_ID }}
Expand Down
5 changes: 1 addition & 4 deletions .github/workflows/test.yml → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: test
name: Continous Integration (CI)

on:
pull_request:
Expand Down Expand Up @@ -74,6 +74,3 @@ jobs:

- name: Build Docker image
run: make docker_build

- name: Run Docker tests
run: make docker_test
6 changes: 3 additions & 3 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
- v*.*.*

jobs:
cloudrun:
cloud-run:
runs-on: ubuntu-latest
environment: prod
concurrency: prod
Expand Down Expand Up @@ -40,7 +40,7 @@ jobs:
id: wait-build
with:
token: ${{ secrets.GITHUB_TOKEN }}
checkName: cloudbuild
checkName: cloud-build
ref: ${{ github.event.pull_request.head.sha || github.sha }}
intervalSeconds: 10
timeoutSeconds: 900 # 15m
Expand All @@ -58,7 +58,7 @@ jobs:
run: echo Timeout && false # fail if build time out

- name: Deploy Docker container to Cloud Run
run: make cloudrun
run: make cloud_run
env:
GCR_PROJECT_ID: ${{ secrets.GCR_PROJECT_ID }}
CLOUDBUILD_PROJECT_ID: ${{ secrets.CLOUDBUILD_PROJECT_ID }}
Expand Down
37 changes: 26 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,31 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM python:3.9-slim
RUN apt-get update && \
apt-get install -y \
build-essential \
make \
gcc \
locales
ADD . /app
ARG PYTHON_VERSION

FROM python:${PYTHON_VERSION}-alpine

WORKDIR /app
RUN pip install -U setuptools
RUN pip install ."[api, datadog, dynatrace, prometheus, elasticsearch, opensearch, splunk, pubsub, cloud_monitoring, cloud_service_monitoring, cloud_storage, bigquery, cloudevent, dev]"

COPY . ./

# TODO: Is `dev` required if we decide not to run tests from the Docker image?
RUN pip install --no-cache-dir ."[ \
api, \
bigquery, \
cloud_monitoring, \
cloud_service_monitoring, \
cloud_storage, \
cloudevent, \
datadog, \
dynatrace, \
elasticsearch, \
opensearch, \
prometheus, \
pubsub, \
splunk \
]"

ENTRYPOINT [ "slo-generator" ]
CMD ["-v"]

CMD [ "-v" ]
87 changes: 41 additions & 46 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -130,53 +130,48 @@ int_os:
run_api:
slo-generator api --target=run_compute --signature-type=http -c samples/config.yaml

# Local Docker build / push
# Build Docker image locally
docker_build:
DOCKER_BUILDKIT=1
docker build -t slo-generator:latest .

docker_test: docker_build
docker run --entrypoint "make" \
-e GOOGLE_APPLICATION_CREDENTIALS=tests/unit/fixtures/fake_credentials.json \
slo-generator test

# Cloudbuild
cloudbuild: gcloud_alpha
gcloud alpha builds submit \
--config=cloudbuild.yaml \
--project=${CLOUDBUILD_PROJECT_ID} \
--substitutions=_GCR_PROJECT_ID=${GCR_PROJECT_ID},_VERSION=${VERSION}

# Cloudrun
cloudrun:
docker build \
-t slo-generator:latest \
--build-arg PYTHON_VERSION=3.9 \
.

# Build Docker image with Cloud Build
cloud_build:
gcloud builds submit \
--config=cloudbuild.yaml \
--project=${CLOUDBUILD_PROJECT_ID} \
--substitutions=_GCR_PROJECT_ID=${GCR_PROJECT_ID},_VERSION=${VERSION}

# Cloud Run
cloud_run:
gcloud run deploy slo-generator \
--image gcr.io/${GCR_PROJECT_ID}/slo-generator:${VERSION} \
--region=${REGION} \
--platform managed \
--set-env-vars CONFIG_PATH=${CONFIG_URL} \
--service-account=${SERVICE_ACCOUNT} \
--project=${CLOUDRUN_PROJECT_ID} \
--command="slo-generator" \
--args=api \
--args=--signature-type="${SIGNATURE_TYPE}" \
--min-instances 1 \
--allow-unauthenticated

# Cloudrun - export mode only
cloudrun_export_only:
--image gcr.io/${GCR_PROJECT_ID}/slo-generator:${VERSION} \
--region=${REGION} \
--platform managed \
--set-env-vars CONFIG_PATH=${CONFIG_URL} \
--service-account=${SERVICE_ACCOUNT} \
--project=${CLOUDRUN_PROJECT_ID} \
--command="slo-generator" \
--args=api \
--args=--signature-type="${SIGNATURE_TYPE}" \
--min-instances 1 \
--allow-unauthenticated

# Cloud Run - Export Mode Only
cloud_run_export_only:
gcloud run deploy slo-generator-export \
--image gcr.io/${GCR_PROJECT_ID}/slo-generator:${VERSION} \
--region=${REGION} \
--platform managed \
--set-env-vars CONFIG_PATH=${CONFIG_URL} \
--service-account=${SERVICE_ACCOUNT} \
--project=${CLOUDRUN_PROJECT_ID} \
--command="slo-generator" \
--args=api \
--args=--signature-type="cloudevent" \
--args=--target="run_export" \
--min-instances 1 \
--allow-unauthenticated

gcloud_alpha:
gcloud components install alpha --quiet
--image gcr.io/${GCR_PROJECT_ID}/slo-generator:${VERSION} \
--region=${REGION} \
--platform managed \
--set-env-vars CONFIG_PATH=${CONFIG_URL} \
--service-account=${SERVICE_ACCOUNT} \
--project=${CLOUDRUN_PROJECT_ID} \
--command="slo-generator" \
--args=api \
--args=--signature-type="cloudevent" \
--args=--target="run_export" \
--min-instances 1 \
--allow-unauthenticated
17 changes: 6 additions & 11 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,15 @@
# limitations under the License.
---
steps:

- name: gcr.io/cloud-builders/docker
id: Build SLO generator
args: ['build', '-t', 'gcr.io/$_GCR_PROJECT_ID/slo-generator:${_VERSION}', '.']

- name: gcr.io/$_GCR_PROJECT_ID/slo-generator:${_VERSION}
id: Run all tests
entrypoint: make
env:
- 'MIN_VALID_EVENTS=10'
args: []
id: Build SLO Generator Docker Image
script: |
docker build \
-t gcr.io/$_GCR_PROJECT_ID/slo-generator:${_VERSION} \
.
substitutions:
_VERSION: latest

images:
- 'gcr.io/$_GCR_PROJECT_ID/slo-generator:${_VERSION}'
- gcr.io/$_GCR_PROJECT_ID/slo-generator:${_VERSION}
2 changes: 1 addition & 1 deletion docs/deploy/cloudbuild.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ git clone https://github.com/google/slo-generator
cd slo-generator/
export CLOUDBUILD_PROJECT_ID=<CLOUDBUILD_PROJECT_ID>
export GCR_PROJECT_ID=<GCR_PROJECT_ID>
make cloudbuild
make cloud_build
```

## Run `slo-generator` as a build step
Expand Down

0 comments on commit 90d2797

Please sign in to comment.