
FROM python:3.12

# Architecture: amd64 or arm64
#ARCH=arm64
#ARCH=amd64
ARG ARCH

ARG firefox_ver=137.0
ARG geckodriver_ver=0.36.0

RUN echo "Architecture build: $ARCH"

RUN if [ "${ARCH}" = "amd64" ] ; then \
   ARCH_G="linux64"; ARCH_F="linux-x86_64"; \
   else \
   ARCH_G="linux-aarch64"; ARCH_F="linux-aarch64"; \
   fi \
 && apt-get update \
 && apt-get upgrade -y \
 && apt-get install -y --no-install-recommends --no-install-suggests \
            ca-certificates \
 && update-ca-certificates \
    \
 # Install tools for building
 && toolDeps=" \
        curl xz-utils \
    " \
 && apt-get install -y --no-install-recommends --no-install-suggests $toolDeps \
    \
 # Install dependencies for Firefox
 && apt-get install -y --no-install-recommends --no-install-suggests libgl1 libpci3 \
            `apt-cache depends firefox-esr | awk '/Depends:/{print$2}'` \
    \
 # Download and install Firefox
 && curl -fL -o /tmp/firefox.tar.xz \
         https://ftp.mozilla.org/pub/firefox/releases/${firefox_ver}/${ARCH_F}/en-GB/firefox-${firefox_ver}.tar.xz \
 && tar -xJf /tmp/firefox.tar.xz -C /tmp/ \
 && mv /tmp/firefox /opt/firefox \
    \
 # Download and install geckodriver
 && curl -fL -o /tmp/geckodriver.tar.gz \
         https://github.com/mozilla/geckodriver/releases/download/v${geckodriver_ver}/geckodriver-v${geckodriver_ver}-${ARCH_G}.tar.gz \
 && tar -xzf /tmp/geckodriver.tar.gz -C /tmp/ \
 && chmod +x /tmp/geckodriver \
 && mv /tmp/geckodriver /usr/local/bin/ \
    \
 # Cleanup unnecessary stuff
 && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false $toolDeps \
 && rm -rf /var/lib/apt/lists/* /tmp/*

RUN pip install --no-cache-dir selenium fastapi "uvicorn[standard]"

WORKDIR /opt/app
COPY . /opt/app/

# As this image cannot run in non-headless mode anyway, it's better to forcibly enable it, regardless whether WebDriver client requests it in capabilities or not.
ENV MOZ_HEADLESS=1

CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "80"]

# docker build -f Dockerfile -t selenium_app .
# docker run --rm -it --shm-size=512m --name selenium_app selenium_app

# docker exec -it selenium_app bash -c "curl localhost:80/get_missing_kids/?pages=5"
# docker exec -it selenium_app bash -c "curl localhost:80/get_missing_kids/?pages=-1"
