|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Find an available container tool (docker or podman) |
| 4 | +find_container_tool() { |
| 5 | + if command -v docker > /dev/null 2>&1; then |
| 6 | + echo "sudo docker" |
| 7 | + elif command -v podman > /dev/null 2>&1; then |
| 8 | + echo "podman" |
| 9 | + else |
| 10 | + echo "Error: Neither docker nor podman is available." >&2 |
| 11 | + exit 1 |
| 12 | + fi |
| 13 | +} |
| 14 | + |
| 15 | +# Determine which container tool to use |
| 16 | +CONTAINER_TOOL=$(find_container_tool) |
| 17 | + |
| 18 | +# Locate the directory containing dockerfile (root) |
| 19 | +PROJECT_ROOT_DIR="$(cd "$(dirname "$0")" && pwd)/.." |
| 20 | + |
| 21 | +# Check SELinux status and set appropriate mount option |
| 22 | +check_selinux() { |
| 23 | + if command -v getenforce > /dev/null 2>&1; then |
| 24 | + SELINUX_STATUS=$(getenforce) |
| 25 | + if [ "$SELINUX_STATUS" = "Enforcing" ] || [ "$SELINUX_STATUS" = "Permissive" ]; then |
| 26 | + echo ":z" |
| 27 | + else |
| 28 | + echo "" |
| 29 | + fi |
| 30 | + elif [ -f /sys/fs/selinux/enforce ]; then |
| 31 | + if [ "$(cat /sys/fs/selinux/enforce)" = "1" ]; then |
| 32 | + echo ":z" |
| 33 | + else |
| 34 | + echo "" |
| 35 | + fi |
| 36 | + else |
| 37 | + echo "" |
| 38 | + fi |
| 39 | +} |
| 40 | + |
| 41 | +# Get the SELinux option for volume mounts if SELinux is enforcing or permissive |
| 42 | +SELINUX=$(check_selinux) |
| 43 | + |
| 44 | +# Build binary |
| 45 | +$CONTAINER_TOOL image rm build-podman-compose |
| 46 | + |
| 47 | +if expr "$CONTAINER_TOOL" : '.*docker.*' >/dev/null; then |
| 48 | + $CONTAINER_TOOL build -t build-podman-compose "$PROJECT_ROOT_DIR" |
| 49 | + $CONTAINER_TOOL run --name build-podman-compose build-podman-compose |
| 50 | + $CONTAINER_TOOL cp build-podman-compose:/result/podman-compose "$PROJECT_ROOT_DIR/podman-compose" |
| 51 | + $CONTAINER_TOOL container stop build-podman-compose |
| 52 | + $CONTAINER_TOOL container rm -f build-podman-compose |
| 53 | +else |
| 54 | + $CONTAINER_TOOL build -v "$PROJECT_ROOT_DIR:/result$SELINUX" -t build-podman-compose "$PROJECT_ROOT_DIR" |
| 55 | +fi |
| 56 | +$CONTAINER_TOOL image rm python:3.11-slim |
| 57 | +$CONTAINER_TOOL image rm build-podman-compose |
0 commit comments