# Build an image named "inventory" with:
#   docker build --tag inventory .
#
# Then run your image with:
#   docker run -it --env INVENTORY_DISABLE_AUTH=1 --publish 8081:8081 --volume $(pwd)/inventory.db:/prog/inventory.db inventory
#
# Please note that the file "inventory.db" must exist, and be
# writable for the user in the image (uid 1000, gid 1000, you
# can change that below).
#
# Please also note that setting INVENTORY_DISABLE_AUTH to 1
# (like shown above) disables authentication and thus shall
# NEVER be used in untrusted networks (like the internet)!
#
# For more documentation and development, please see:
#   https://github.com/chiefenne/electronics_inventory
#
FROM alpine:latest AS builder
WORKDIR /prog
ADD https://github.com/chiefenne/electronics_inventory.git /src
RUN <<EOS
    apk add python3 py3-pip
    python -m venv /prog/venv
    . /prog/venv/bin/activate
    cp -R /src/* /prog/
    python -m pip install -r /prog/requirements.txt

    echo '#!/bin/sh' >> /prog/run.sh
    echo 'cd /prog/' >> /prog/run.sh
    echo '. /prog/venv/bin/activate' >> /prog/run.sh
    echo 'exec uvicorn app:app --proxy-headers $@' >> /prog/run.sh
    chmod 755 /prog/run.sh

    echo 'app:x:1000:1000:Daemon User,,,:/prog:/bin/false' > /etc/passwd
    echo 'app:x:1000' > /etc/group
EOS

FROM alpine:latest
RUN apk add --no-cache python3
COPY --from=builder /prog /prog
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group
VOLUME /prog/inventory.db
WORKDIR /
ENTRYPOINT ["/prog/run.sh"]
CMD ["--host", "0.0.0.0", "--port", "8081"]
