From 459f68f5b8413765c114b6eff676d14bfb8cc8c1 Mon Sep 17 00:00:00 2001 From: L1ghtman2k Date: Sat, 17 Dec 2022 20:11:21 -0500 Subject: [PATCH 1/4] add docker --- .env-sample | 1 + .gitignore | 1 + Dockerfile | 37 +++++++++++++++++++++++++++++++++++++ README.md | 7 ++++++- 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 .env-sample create mode 100644 .gitignore create mode 100644 Dockerfile 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/.gitignore b/.gitignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env \ 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 From df4cf4b3835c813b866bbecbef8c83103e90af68 Mon Sep 17 00:00:00 2001 From: Aibek <35672535+L1ghtman2k@users.noreply.github.com> Date: Sat, 17 Dec 2022 20:15:56 -0500 Subject: [PATCH 2/4] Create docker-image.yml --- .github/workflows/docker-image.yml | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 .github/workflows/docker-image.yml diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..acdac45 --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,41 @@ +name: Create and publish a Docker image + +on: + push: + branches: ['release'] + +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 }} From 9d99667704cd495d8b514036de18ae149f3a83c8 Mon Sep 17 00:00:00 2001 From: Aibek <35672535+L1ghtman2k@users.noreply.github.com> Date: Sat, 17 Dec 2022 20:19:20 -0500 Subject: [PATCH 3/4] Update docker-image.yml --- .github/workflows/docker-image.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index acdac45..c6f2af5 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -1,9 +1,8 @@ name: Create and publish a Docker image on: - push: - branches: ['release'] - + release: + types: [published] env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository }} From 4ea424020f92f7be895cb6d46681fac915ed6510 Mon Sep 17 00:00:00 2001 From: L1ghtman2k Date: Sat, 17 Dec 2022 20:39:56 -0500 Subject: [PATCH 4/4] prevent .env from getting into docker --- .dockerignore | 1 + .gitignore | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +.env diff --git a/.gitignore b/.gitignore index 2eea525..451fb35 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.env \ No newline at end of file +.env +.idea \ No newline at end of file