-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Containerize application and configure GitHub Actions to build and pu…
…sh Docker image to Harbor (#426) * Set python image #354 * Upgrade pip #354 * Install Poetry #354 * Install a different version of setuptools #354 * Install Gunicorn #354 * Install the app dependencies #354 * Serve the app on a Gunicorn server #354 * Pin Poetry version #354 * Define Actions job for building and pushing Docker image to Harbor #355 * Configure job to only run after other jobs succeed #355 * Configure job to login to Harbor #355 * Configure job to extract Docker metadata #355 * Configure job to build image #355 * Configure job to push image to Harbor on pushes to k8s-deployment branch #355 * Add job documentation and TODOs #355 * Configure dependabot to maintain GH Actions dependencies #355 * Add TODO for branch name of push events #355 * Update Dockerfile and add entrypoint script * Use specific python and alpine versions in the base image * Pin python package versions in Dockerfile * Use a cache mount to speed up pip and poetry * Comment the RUN step * Move things out of the datagateway-api-run directory * Remove workaround that is no longer needed * log_location value should not be quoted * Only copy necessary files to build container * Improve readability of RUN instructions * Use a temp file instead of sed -i in entrypoint script * Create a symlink to the installed python module * Address TODOs * Change default value of ICAT_CHECK_CERT ENV * Upgrade and pin actions to commit SHAs * ci(docker): bump actions/checkout to 3.5.3 in docker job * Update README --------- Co-authored-by: Alan Kyffin <[email protected]>
- Loading branch information
Showing
4 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Dockerfile to build and serve datagateway-api | ||
|
||
# Build stage | ||
FROM python:3.11-alpine3.17 as builder | ||
|
||
WORKDIR /datagateway-api-build | ||
|
||
COPY README.md poetry.lock pyproject.toml ./ | ||
COPY datagateway_api/ datagateway_api/ | ||
|
||
RUN --mount=type=cache,target=/root/.cache \ | ||
set -eux; \ | ||
\ | ||
python3 -m pip install 'poetry~=1.3.2'; \ | ||
poetry build; | ||
|
||
|
||
# Install & run stage | ||
FROM python:3.11-alpine3.17 | ||
|
||
WORKDIR /datagateway-api-run | ||
|
||
COPY --from=builder /datagateway-api-build/dist/datagateway_api-*.whl /tmp/ | ||
|
||
RUN --mount=type=cache,target=/root/.cache \ | ||
set -eux; \ | ||
\ | ||
python3 -m pip install \ | ||
'gunicorn~=20.1.0' \ | ||
/tmp/datagateway_api-*.whl; \ | ||
\ | ||
# Create a symlink to the installed python module \ | ||
DATAGATEWAY_API_LOCATION="$(python3 -m pip show datagateway_api | awk '/^Location:/ { print $2 }')"; \ | ||
ln -s "$DATAGATEWAY_API_LOCATION/datagateway_api/" datagateway_api; \ | ||
\ | ||
# Create config.yaml and search_api_mapping.json from their .example files \ | ||
cp datagateway_api/config.yaml.example datagateway_api/config.yaml; \ | ||
cp datagateway_api/search_api_mapping.json.example datagateway_api/search_api_mapping.json; \ | ||
\ | ||
# Create a non-root user to run as \ | ||
addgroup -S datagateway-api; \ | ||
adduser -S -D -G datagateway-api -H -h /datagateway-api-run datagateway-api; \ | ||
\ | ||
# Change ownership of config.yaml - the entrypoint script will need to edit it \ | ||
chown datagateway-api:datagateway-api datagateway_api/config.yaml; | ||
|
||
USER datagateway-api | ||
|
||
ENV ICAT_URL="http://localhost" | ||
ENV ICAT_CHECK_CERT="false" | ||
ENV LOG_LOCATION="/dev/stdout" | ||
|
||
COPY docker/docker-entrypoint.sh /usr/local/bin/ | ||
ENTRYPOINT ["docker-entrypoint.sh"] | ||
|
||
# Serve the application using gunicorn - production ready WSGI server | ||
CMD ["gunicorn", "-b", "0.0.0.0:8000", "datagateway_api.wsgi"] | ||
EXPOSE 8000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/bin/sh -eu | ||
|
||
# Use a tempfile instead of sed -i so that only the file, not the directory needs to be writable | ||
TEMPFILE="$(mktemp)" | ||
|
||
# Set values in config.yaml from environment variables | ||
# No quotes for icat_check_cert because it's boolean | ||
sed -e "s|icat_url: \".*\"|icat_url: \"$ICAT_URL\"|" \ | ||
-e "s|icat_check_cert: .*|icat_check_cert: $ICAT_CHECK_CERT|" \ | ||
-e "s|log_location: \".*\"|log_location: \"$LOG_LOCATION\"|" \ | ||
datagateway_api/config.yaml > "$TEMPFILE" | ||
|
||
cat "$TEMPFILE" > datagateway_api/config.yaml | ||
rm "$TEMPFILE" | ||
|
||
# Run the CMD instruction | ||
exec "$@" |