Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ jobs:
run: make test -C python38-slim/
- name: Test python 3.9 image
run: make test -C python39-slim/
- name: Test python 3.12 image
run: make test -C python312-slim/
3 changes: 2 additions & 1 deletion .github/workflows/docker_image_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ jobs:

- name: Docker build and push
run: |
docker build -t daeploy/s2i-python:3.8-slim-${{ github.event.release.tag_name }} -t daeploy/s2i-python:latest ./python38-slim
docker build -t daeploy/s2i-python:3.12-slim-${{ github.event.release.tag_name }} -t daeploy/s2i-python:latest ./python312-slim
docker build -t daeploy/s2i-python:3.8-slim-${{ github.event.release.tag_name }} ./python38-slim
docker build -t daeploy/s2i-python:3.6-slim-${{ github.event.release.tag_name }} ./python36-slim
docker build -t daeploy/s2i-python:3.7-slim-${{ github.event.release.tag_name }} ./python37-slim
docker build -t daeploy/s2i-python:3.9-slim-${{ github.event.release.tag_name }} ./python39-slim
Expand Down
35 changes: 35 additions & 0 deletions python312-slim/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# daeploy/s2i-python
FROM python:3.12-slim

LABEL maintainer="Viking Analytics AB"

ENV BUILDER_VERSION=1.0 \
APP_ROOT="/opt/app-root" \
HOME="/opt/app-root/src" \
STI_SCRIPTS_PATH="/usr/libexec/s2i"

# Set labels used in OpenShift to describe the builder image
LABEL io.k8s.description="Platform for building lightweight Daeploy images" \
io.k8s.display-name="Daeploy python builder" \
io.openshift.expose-services="8080:http" \
io.openshift.tags="builder,daeploy,python." \
io.openshift.s2i.scripts-url="image://${STI_SCRIPTS_PATH}"

# Setup virtualenv
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

# Copy the S2I scripts to the image
COPY ./s2i/bin/ $STI_SCRIPTS_PATH

# Set the default port for applications built using this image
EXPOSE 8080

# Setup file system
RUN mkdir -p ${APP_ROOT} && \
mkdir -p ${HOME}

WORKDIR $HOME

# Set the default CMD for the image
CMD ["$STI_SCRIPTS_PATH/usage"]
10 changes: 10 additions & 0 deletions python312-slim/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
IMAGE_NAME = daeploy/s2i-python

.PHONY: build
build:
docker build -t $(IMAGE_NAME) .

.PHONY: test
test:
docker build -t $(IMAGE_NAME)-candidate .
IMAGE_NAME=$(IMAGE_NAME)-candidate test/run
105 changes: 105 additions & 0 deletions python312-slim/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@

# Creating a basic S2I builder image

## Getting started

### Files and Directories

| File | Required? | Description |
|------------------------|-----------|--------------------------------------------------------------|
| Dockerfile | Yes | Defines the base builder image |
| s2i/bin/assemble | Yes | Script that builds the application |
| s2i/bin/usage | No | Script that prints the usage of the builder |
| s2i/bin/run | Yes | Script that runs the application |
| s2i/bin/save-artifacts | No | Script for incremental builds that saves the built artifacts |
| test/run | No | Test script for the builder image |
| test/test-app | Yes | Test application source code |

#### Dockerfile

Create a *Dockerfile* that installs all of the necessary tools and libraries that are needed to build and run our application. This file will also handle copying the s2i scripts into the created image.

#### S2I scripts

##### assemble

Create an *assemble* script that will build our application, e.g.:

- build python modules
- bundle install ruby gems
- setup application specific configuration

The script can also specify a way to restore any saved artifacts from the previous image.

##### run

Create a *run* script that will start the application.

##### save-artifacts (optional)

Create a *save-artifacts* script which allows a new build to reuse content from a previous version of the application image.

##### usage (optional)

Create a *usage* script that will print out instructions on how to use the image.

##### Make the scripts executable

Make sure that all of the scripts are executable by running *chmod +x s2i/bin/**

#### Create the builder image

The following command will create a builder image named daeploy/s2i-python based on the Dockerfile that was created previously.

```bash
docker build -t daeploy/s2i-python .
```

The builder image can also be created by using the *make* command since a *Makefile* is included.

Once the image has finished building, the command *s2i usage daeploy/s2i-python* will print out the help info that was defined in the *usage* script.

#### Testing the builder image

The builder image can be tested using the following commands:

```bash
docker build -t daeploy/s2i-python-candidate .
IMAGE_NAME=daeploy/s2i-python-candidate test/run
```

The builder image can also be tested by using the *make test* command since a *Makefile* is included.

#### Creating the application image

The application image combines the builder image with your applications source code, which is served using whatever application is installed via the *Dockerfile*, compiled using the *assemble* script, and run using the *run* script.
The following command will create the application image:

```bash
s2i build test/test-app daeploy/s2i-python daeploy/s2i-python-app
---> Building and installing application from source...
```

Using the logic defined in the *assemble* script, s2i will now create an application image using the builder image as a base and including the source code from the test/test-app directory.

#### Running the application image

Running the application image is as simple as invoking the docker run command:

```bash
docker run -d -p 8080:8080 daeploy/s2i-python-app
```

The application, which consists of a simple static web page, should now be accessible at [http://localhost:8080](http://localhost:8080).

#### Using the saved artifacts script

Rebuilding the application using the saved artifacts can be accomplished using the following command:

```bash
s2i build --incremental=true test/test-app nginx-centos7 nginx-app
---> Restoring build artifacts...
---> Building and installing application from source...
```

This will run the *save-artifacts* script which includes the custom code to backup the currently running application source, rebuild the application image, and then re-deploy the previously saved source using the *assemble* script.
26 changes: 26 additions & 0 deletions python312-slim/s2i/bin/assemble
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
#
# S2I assemble script for the 'daeploy/s2i-python' image.
# The 'assemble' script builds your application source so that it is ready to run.
#
# For more information refer to the documentation:
# https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md
#

# If the 'daeploy/s2i-python' assemble script is executed with the '-h' flag, print the usage.
if [[ "$1" == "-h" ]]; then
exec $STI_SCRIPTS_PATH/usage
fi

echo "---> Installing application source ..."
shopt -s dotglob
mv /tmp/src/* "$HOME"

# Install dependencies
echo "---> Upgrading pip and setuptools"
pip install -U pip

if [[ -f requirements.txt ]]; then
echo "---> Installing dependencies..."
python -m pip install -r requirements.txt
fi
43 changes: 43 additions & 0 deletions python312-slim/s2i/bin/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash
#
# S2I run script for the 'daeploy/s2i-python' image.
# The run script executes the server that runs your application.
#
# For more information see the documentation:
# https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md
#

cd $HOME

if [ -z "$APP_SCRIPT" ] && [ -z "$APP_FILE" ] && [ -z "$APP_MODULE" ]; then
# Set default values for APP_SCRIPT and APP_FILE only when all three APP_
# variables are not defined by user. This prevents a situation when
# APP_MODULE is defined to app:application but the app.py file is found as the
# APP_FILE and then executed by Python instead of gunicorn.
APP_SCRIPT="app.sh"
APP_SCRIPT_DEFAULT=1
APP_FILE="app.py"
APP_FILE_DEFAULT=1
fi

if [ ! -z "$APP_SCRIPT" ]; then
if [[ -f "$APP_SCRIPT" ]]; then
echo "---> Running application from script ($APP_SCRIPT) ..."
if [[ "$APP_SCRIPT" != /* ]]; then
APP_SCRIPT="$APP_ROOT/src/$APP_SCRIPT"
fi
chmod +x "$APP_SCRIPT"
"$APP_SCRIPT"
elif [[ -z "$APP_SCRIPT_DEFAULT" ]]; then
echo "ERROR: file '$APP_SCRIPT' not found." && exit 1
fi
fi

if [ ! -z "$APP_FILE" ]; then
if [[ -f "$APP_FILE" ]]; then
echo "---> Running application from Python script ($APP_FILE) ..."
python "$APP_FILE"
elif [[ -z "$APP_FILE_DEFAULT" ]]; then
echo "ERROR: file '$APP_FILE' not found." && exit 1
fi
fi
12 changes: 12 additions & 0 deletions python312-slim/s2i/bin/usage
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
cat <<EOF
This is the daeploy/s2i-python S2I image:
To use it, install S2I: https://github.com/openshift/source-to-image

Sample invocation:

s2i build <source code path/URL> daeploy/s2i-python <application image>

You can then run the resulting image via:
docker run <application image>
EOF
Loading
Loading