diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +.env diff --git a/.env-sample b/.env-sample new file mode 100644 index 0000000..d6ff1a3 --- /dev/null +++ b/.env-sample @@ -0,0 +1 @@ +LIGHTHOUSE_MONGO_KEY= \ No newline at end of file diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..c6f2af5 --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,40 @@ +name: Create and publish a Docker image + +on: + release: + types: [published] +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Log in to the Container registry + uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Build and push Docker image + uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..451fb35 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +.idea \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4b896a1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,37 @@ +FROM python:3.10.9-bullseye as base + +# Setup env +ENV LANG C.UTF-8 +ENV LC_ALL C.UTF-8 +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONFAULTHANDLER 1 + + +FROM base AS python-deps + +# Install pipenv and compilation dependencies +RUN pip install pipenv +RUN apt-get update && apt-get install -y --no-install-recommends gcc + +# Install python dependencies in /.venv +COPY Pipfile . +COPY Pipfile.lock . +RUN PIPENV_VENV_IN_PROJECT=1 pipenv install --deploy + + +FROM base AS runtime + +# Copy virtual env from python-deps stage +COPY --from=python-deps /.venv /.venv +ENV PATH="/.venv/bin:$PATH" + +# Create and switch to a new user +RUN useradd --create-home appuser +WORKDIR /home/appuser +USER appuser + +# Install application into container +COPY . . + +# Run the application +CMD ["python", "-m", "streamlit", "run", "spreadsheet/webapp.py"] \ No newline at end of file diff --git a/README.md b/README.md index 99f326e..5adf126 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,9 @@ # lts_stats 1. Add connection string to environment variables - variable name should be LIGHTHOUSE_MONGO_KEY 2. Install relevant packages from Pipfile -3. run python -m streamlit run spreadsheet/webapp.py in the terminal (will use port 8501) \ No newline at end of file +3. run python -m streamlit run spreadsheet/webapp.py in the terminal (will use port 8501) + +# using docker for dev +1. create .env file in root directory, add LIGHTHOUSE_MONGO_KEY= +2. docker build -t lts_stats . +3. docker run -p 8501:8501 --env-file .env lts_stats \ No newline at end of file