FROM python:3.9-slim AS base
ARG PYTHON_VER="3.9" # Default value if not provided during build ARG BUILD_INFO="Default build information"
ENV APP_NAME="ExampleApp"
APP_VERSION="1.0.1"
PORT="8080"
LANG="en_US.UTF-8"
LC_ALL="en_US.UTF-8"
PYTHONUNBUFFERED="1"
# Pass ARG to ENV if needed for runtime inspection (optional)
BUILD_PYTHON_VERSION_INFO="${PYTHON_VER}"
BUILD_TIME_INFO_FROM_ARG="${BUILD_INFO}"
LABEL maintainer="Your Name your.email@example.com"
org.opencontainers.image.title="${APP_NAME}"
org.opencontainers.image.description="A Python Flask application demonstrating Dockerfile best practices."
org.opencontainers.image.version="${APP_VERSION}"
org.opencontainers.image.authors="Your Name your.email@example.com"
org.opencontainers.image.vendor="YourCompany"
org.opencontainers.image.licenses="MIT"
org.opencontainers.image.source="https://github.com/your-repo/your-project"
build-arg.python-version="${PYTHON_VER}"
build-arg.build-info="${BUILD_INFO}"
-o pipefail: Causes a pipeline to return the exit status of the last command in the pipe that failed.
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN apt-get update &&
apt-get install -y --no-install-recommends curl locales &&
# Configure locale
echo "$LANG UTF-8" >> /etc/locale.gen &&
locale-gen &&
# Clean up
apt-get clean &&
rm -rf /var/lib/apt/lists/* &&
# Create a non-root user and group
groupadd -r appgroup &&
useradd -r -g appgroup -s /sbin/nologin -d /app appuser &&
# Create app directory and set permissions
mkdir -p /app &&
chown -R appuser:appgroup /app
WORKDIR /app
COPY --chown=appuser:appgroup requirements.txt .
RUN echo "Building with Python version (from ARG): ${PYTHON_VER}" &&
echo "Additional build info (from ARG): ${BUILD_INFO}" &&
pip install --no-cache-dir -r requirements.txt
COPY --chown=appuser:appgroup app.py . COPY --chown=appuser:appgroup onbuild-scripts/ ./onbuild-scripts/ RUN chmod +x ./onbuild-scripts/verify_base.sh # Ensure script is executable
USER appuser
EXPOSE ${PORT}
--start-period: Grace period for the container to start before first check (avoids premature failures).
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3
CMD curl -fsS http://localhost:${PORT}/health || exit 1
STOPSIGNAL SIGTERM
ONBUILD LABEL derived.image.base.version="${APP_VERSION}"
derived.image.build.timestamp="$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
ONBUILD USER root # Switch back to root temporarily if needed for ONBUILD tasks
ONBUILD COPY ./onbuild-scripts/ /app/onbuild-scripts/
ONBUILD RUN echo "ONBUILD: Executing from base image ${APP_NAME} v${APP_VERSION}." &&
if [ -f /app/onbuild-scripts/verify_base.sh ]; then
echo "ONBUILD: Running verification script..." &&
/app/onbuild-scripts/verify_base.sh;
fi
ONBUILD USER appuser # Revert to appuser
CMD ["python", "app.py"]