Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Use additional build contexts to build frontend Docker image (#2048)

Dhruv Bhanushali4a42c93102b9
diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 000000000..eade7d756
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1,6 @@
+# Exclude everything and only include certain files.
+# This Docker context is used to build the frontend Docker image.
+*
+!.npmrc
+!.pnpmfile.cjs
+!pnpm-lock.yaml
diff --git a/.github/workflows/ci_cd.yml b/.github/workflows/ci_cd.yml
index cb564555f..da453a634 100644
--- a/.github/workflows/ci_cd.yml
+++ b/.github/workflows/ci_cd.yml
@@ -185,13 +185,10 @@ jobs:
# This step
# - downloads translation strings from GlotPress so that they can be
# bundled inside the Docker image
- # - copies pnpm config files from the root to the `frontend/` directory
- # so that they can be used to mock a workspace inside the Docker image
- name: Prepare frontend for building
if: matrix.image == 'frontend'
run: |
just frontend/run i18n
- cp .npmrc .pnpmfile.cjs pnpm-lock.yaml frontend/
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v2
@@ -201,13 +198,14 @@ jobs:
- name: Build image `${{ matrix.image }}`
uses: docker/build-push-action@v4
with:
- context: ${{ matrix.context }}
+ context: ${{ matrix.context || matrix.image }}
target: ${{ matrix.target }}
push: false
tags: openverse-${{ matrix.image }}
cache-from: type=gha,scope=${{ matrix.image }}
cache-to: type=gha,scope=${{ matrix.image }}
outputs: type=docker,dest=/tmp/${{ matrix.image }}.tar
+ build-contexts: ${{ matrix.build-contexts || '' }}
build-args: |
SEMANTIC_VERSION=${{ needs.get-image-tag.outputs.image_tag }}
CATALOG_PY_VERSION=${{ steps.prepare-build-args.outputs.catalog_py_version }}
diff --git a/api/Dockerfile b/api/Dockerfile
index 12bbd8b03..f3eace765 100644
--- a/api/Dockerfile
+++ b/api/Dockerfile
@@ -1,3 +1,5 @@
+# syntax=docker/dockerfile:1
+
# Automatically build image using Python version specified in the `Pipfile`.
ARG API_PY_VERSION
diff --git a/automations/python/workflows/set_matrix_images.py b/automations/python/workflows/set_matrix_images.py
index 0bc32a98d..a975798d5 100644
--- a/automations/python/workflows/set_matrix_images.py
+++ b/automations/python/workflows/set_matrix_images.py
@@ -27,15 +27,11 @@ includes = {
"context": "docker/upstream_db",
"target": "db",
},
- "catalog": {"image": "catalog", "context": "catalog", "target": "cat"},
- "ingestion_server": {
- "image": "ingestion_server",
- "context": "ingestion_server",
- "target": "ing",
- },
- "api": {"image": "api", "context": "api", "target": "api"},
+ "catalog": {"image": "catalog", "target": "cat"},
+ "ingestion_server": {"image": "ingestion_server", "target": "ing"},
+ "api": {"image": "api", "target": "api"},
"api_nginx": {"image": "api_nginx", "context": "api", "target": "nginx"},
- "frontend": {"image": "frontend", "context": "frontend", "target": "app"},
+ "frontend": {"image": "frontend", "target": "app", "build-contexts": "repo_root=."},
}
if "ci_cd" in changes:
diff --git a/catalog/Dockerfile b/catalog/Dockerfile
index 9d65fc98a..37efc0906 100644
--- a/catalog/Dockerfile
+++ b/catalog/Dockerfile
@@ -1,3 +1,5 @@
+# syntax=docker/dockerfile:1
+
# Cannot use $PYTHON_VERSION because that is overwritten by the base image
# with the patch version (we only care about major and minor version)
ARG CATALOG_PY_VERSION
diff --git a/docker/upstream_db/Dockerfile b/docker/upstream_db/Dockerfile
index 8dc1b349b..96fa212db 100644
--- a/docker/upstream_db/Dockerfile
+++ b/docker/upstream_db/Dockerfile
@@ -1,3 +1,5 @@
+# syntax=docker/dockerfile:1
+
FROM postgres:13.6 as db
ARG PGCLI_VERSION=3.2.0
diff --git a/documentation/general/general_setup.md b/documentation/general/general_setup.md
index 5652021a7..52ada6dd8 100644
--- a/documentation/general/general_setup.md
+++ b/documentation/general/general_setup.md
@@ -184,6 +184,22 @@ If not, you can use one of the many
[installation methods](https://docs.docker.com/engine/install/) officially
supported by Docker.
+Note that the building images for the frontend needs an advanced build feature,
+namely additional build contexts, that is provided by the
+[Buildx plugin](https://docs.docker.com/build/architecture/#buildx). The Buildx
+plugin is included in Docker Desktop and all
+[installation methods](https://docs.docker.com/engine/install/) for Docker
+Engine include steps to also install the Buildx plugin.
+
+To know if your Docker setup is using Buildx, run the following command.
+
+```console
+$ docker build --help
+```
+
+If you see `docker buildx build` under 'Aliases', you have Buildx set up as the
+build client.
+
#### Docker Compose
Docker Compose makes it easy to orchestrate multiple services which we put to
diff --git a/frontend/Dockerfile b/frontend/Dockerfile
index 40f909bd9..2173d48b8 100644
--- a/frontend/Dockerfile
+++ b/frontend/Dockerfile
@@ -1,3 +1,5 @@
+# syntax=docker/dockerfile:1
+
# Automatically build image using Node.js version specified in `package.json`.
ARG FRONTEND_NODE_VERSION
@@ -19,13 +21,11 @@ USER node
WORKDIR /home/node/
# Copy monorepo mocking files into `/home/node`, which pretends to be the monorepo root.
-COPY --chown=node:node .npmrc .pnpmfile.cjs pnpm-lock.yaml ./
+COPY --from=repo_root --chown=node:node .npmrc .pnpmfile.cjs pnpm-lock.yaml ./
RUN echo '{"packages":["frontend/"]}' > pnpm-workspace.yaml
# Copy the `frontend/` directory into `/home/node/frontend`, as a package in the monorepo.
COPY --chown=node:node . ./frontend/
-# Remove the files copied into `frontend/` by `.github/workflows/ci_cd.yml`.
-RUN rm ./frontend/.npmrc ./frontend/.pnpmfile.cjs ./frontend/pnpm-lock.yaml
# Fetch dependencies, including local development tools, to the virtual store.
RUN pnpm fetch
diff --git a/frontend/Dockerfile.playwright b/frontend/Dockerfile.playwright
index a9e123107..615097955 100644
--- a/frontend/Dockerfile.playwright
+++ b/frontend/Dockerfile.playwright
@@ -1,3 +1,5 @@
+# syntax=docker/dockerfile:1
+
ARG PLAYWRIGHT_VERSION
FROM mcr.microsoft.com/playwright:v${PLAYWRIGHT_VERSION}-jammy
diff --git a/frontend/justfile b/frontend/justfile
index 5dcb9faad..e78135def 100644
--- a/frontend/justfile
+++ b/frontend/justfile
@@ -25,6 +25,30 @@ export FRONTEND_PNPM_VERSION := `grep 'packageManager' ../package.json | awk -F'
@pnpm-version:
echo $FRONTEND_PNPM_VERSION
+##########
+# Docker #
+##########
+
+# This section exists because the frontend is not a part of the Docker-based setup
+# and cannot be built easily like `just dc build <service>`.
+
+# Build the frontend Docker image
+build-img tag="openverse-frontend:local" target="app":
+ docker build \
+ --target {{ target }} \
+ --build-context 'repo_root=..' \
+ --build-arg FRONTEND_NODE_VERSION=$(just node-version) \
+ --build-arg FRONTEND_PNPM_VERSION=$(just pnpm-version) \
+ --tag {{ tag }} \
+ .
+
+# Run the frontend Docker image
+run-img tag="openverse-frontend:local":
+ docker run \
+ --rm \
+ --publish 8443:8443 \
+ {{ tag }}
+
######
# Up #
######
diff --git a/ingestion_server/Dockerfile b/ingestion_server/Dockerfile
index b0ec96d91..2b0e95b90 100644
--- a/ingestion_server/Dockerfile
+++ b/ingestion_server/Dockerfile
@@ -1,3 +1,5 @@
+# syntax=docker/dockerfile:1
+
# Automatically build image using Python version specified in the `Pipfile`.
ARG INGESTION_PY_VERSION
diff --git a/utilities/load_testing/Dockerfile b/utilities/load_testing/Dockerfile
index 9afac237b..b5529ac7e 100644
--- a/utilities/load_testing/Dockerfile
+++ b/utilities/load_testing/Dockerfile
@@ -1,3 +1,5 @@
+# syntax=docker/dockerfile:1
+
FROM python:3.10-slim
# See https://github.com/grafana/k6#linux