Skip to content

Commit b943c49

Browse files
Poetry + Docker
Docker FTW <3
1 parent d6907b3 commit b943c49

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+8816
-2476
lines changed

.dockerignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@
1010
**/.project
1111
**/.settings
1212
**/.toolstarget
13+
**/.venv
1314
**/.vs
1415
**/.vscode
1516
**/*.*proj.user
1617
**/*.dbmdl
18+
**/*.example
1719
**/*.jfm
1820
**/*.py#
1921
**/*.py~
2022
**/*.pyc
23+
**/*test.*
2124
**/azds.yaml
2225
**/bin
2326
**/charts
@@ -26,11 +29,15 @@
2629
**/django
2730
**/docker-compose*
2831
**/Dockerfile*
32+
**/heroku.yml
2933
**/img
3034
**/node_modules
3135
**/npm-debug.log
3236
**/obj
37+
**/pre-commit-config.yaml
38+
**/Procfile
3339
**/README.md
40+
**/runtime.txt
3441
**/secrets.dev.yaml
3542
**/terraform
3643
**/values.dev.yaml

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
JUPYTER_TOKEN=docker
2+
PORT=8888

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ django/composeexample/
33
django/data/
44
django/manage.py
55
*.sqlite
6-
redis*
7-
!redis.conf.example
86
*.pem
97
*.key
10-
raw/
118
gitleaks_report*.json
129

1310
# Byte-compiled / optimized / DLL files

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ verbose: true
44
repos:
55
# checks for secrets via rules (gitleaks.toml)
66
- repo: https://github.com/zricethezav/gitleaks
7-
rev: v8.9.0
7+
rev: v8.13.0
88
hooks:
99
- id: gitleaks
1010
- repo: https://github.com/ambv/black
11-
rev: 22.6.0
11+
rev: 22.8.0
1212
hooks:
1313
- id: black
1414
- repo: https://gitlab.com/pycqa/flake8

Dockerfile

Lines changed: 13 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,26 @@
1-
# using ubuntu LTS version
2-
FROM ubuntu:22.04 AS builder-image
3-
4-
# avoid stuck build due to user prompt
5-
ARG DEBIAN_FRONTEND=noninteractive
6-
7-
RUN apt-get -qq update \
8-
&& apt-get -qq install \
9-
--no-install-recommends -y \
10-
aptitude \
11-
autoconf \
12-
automake \
13-
build-essential \
14-
ca-certificates \
15-
curl \
16-
git \
17-
locales \
18-
libbz2-dev \
19-
libffi-dev \
20-
libncurses-dev \
21-
libreadline-dev \
22-
libsqlite3-dev \
23-
libssl-dev \
24-
libtool \
25-
libxslt-dev \
26-
libyaml-dev \
27-
python3 \
28-
python3-dev \
29-
python3-pip \
30-
sqlite3 \
31-
unixodbc-dev \
32-
unzip \
33-
&& rm -rf /var/lib/apt/lists/*
34-
35-
# Set locale
36-
RUN locale-gen en_US.UTF-8
37-
ENV LANG=en_US.UTF-8
38-
ENV LANGUAGE=en_US:en
39-
ENV LC_ALL=en_US.UTF-8
40-
41-
ARG USERNAME=appuser
42-
ENV HOME="/home/${USERNAME}"
43-
ENV PATH="$HOME/.asdf/bin:$HOME/.asdf/shims:$PATH"
44-
45-
RUN useradd --create-home $USERNAME
46-
47-
# install asdf then python latest
48-
RUN bash -c "git clone --depth 1 https://github.com/asdf-vm/asdf.git $HOME/.asdf \
49-
&& echo '. $HOME/.asdf/asdf.sh' >> $HOME/.bashrc \
50-
&& echo '. $HOME/.asdf/asdf.sh' >> $HOME/.profile"
51-
RUN asdf plugin-add python \
52-
&& asdf install python 3.10.7 \
53-
&& asdf global python 3.10.7
54-
55-
# TODO: test poetry via asdf
56-
ENV POETRY_HOME="$HOME/.poetry"
57-
RUN curl -sSL https://install.python-poetry.org | python3.10 -
58-
ENV PATH "${POETRY_HOME}/bin:$PATH"
59-
60-
WORKDIR $$HOME/app
61-
COPY pyproject.toml poetry.lock ./
62-
RUN python3.10 -m venv /opt/venv
63-
64-
# Install pip requirements
65-
RUN . /opt/venv/bin/activate && poetry install
66-
67-
# TODO: dive + docker-slim
68-
FROM ubuntu:22.04 AS runner-image
69-
70-
ARG USERNAME=appuser
71-
ENV HOME="/home/${USERNAME}"
72-
ENV VIRTUAL_ENV="/opt/venv"
73-
ENV PATH="${VIRTUAL_ENV}/bin:$HOME/.asdf/bin:$HOME/.asdf/shims:$PATH"
74-
75-
RUN useradd --create-home $USERNAME \
76-
&& mkdir -p ${HOME}/app
77-
78-
COPY --chown=${USERNAME}:${USERNAME} . $HOME/app
79-
COPY --from=builder-image --chown=${USERNAME}:${USERNAME} /opt/venv /opt/venv
80-
COPY --from=builder-image --chown=${USERNAME}:${USERNAME} $HOME/.asdf $HOME/.asdf
81-
82-
# avoid stuck build due to user prompt
83-
ARG DEBIAN_FRONTEND=noninteractive
84-
85-
RUN apt-get -qq update \
86-
&& apt-get -qq install \
87-
--no-install-recommends -y \
88-
ca-certificates \
89-
curl \
90-
git \
91-
libsqlite3-dev \
92-
sqlite3 \
93-
&& rm -rf /var/lib/apt/lists/*
1+
FROM jupyter/minimal-notebook:latest
942

953
# Keeps Python from generating .pyc files in the container
964
ENV PYTHONDONTWRITEBYTECODE=1
975

986
# Turns off buffering for easier container logging
997
ENV PYTHONUNBUFFERED=1
1008

101-
# activate virtual environment
102-
RUN python -m venv $VIRTUAL_ENV
9+
USER jovyan
10+
11+
COPY . $HOME/work
10312

104-
USER appuser
13+
WORKDIR $HOME/work
10514

106-
WORKDIR $HOME/app
15+
RUN conda install \
16+
xarray \
17+
netCDF4 \
18+
bottleneck \
19+
numpy \
20+
pandas \
21+
matplotlib \
22+
jupyterlab
10723

10824
# ENTRYPOINT ["python", "main.py"]
109-
# CMD ["gunicorn", "-c", "config/gunicorn.conf.py", "main:app"]
11025
CMD ["/bin/bash", "startup.sh"]
11126
# CMD ["/bin/bash"]

README.md

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,76 @@
1-
# python_template
1+
# wine_quality
22

3-
!["It's dangerous to go alone! Take this."](img/zelda.jpg)
4-
<!-- <img src="https://user-images.githubusercontent.com/4097471/144654508-823c6e31-5e10-404c-9f9f-0d6b9d6ce617.jpg" width="300"> -->
3+
![JupyterLab](img/jupyterlab.png)
54

65
## Summary
7-
Oftentimes the initial setup of a Python repo can take a few minutes to a couple hours.
8-
By laying the foundation to rapidly implement an idea, can focus on the good bits instead of
9-
devops drudgery.
10-
11-
### Caveat Emptor
12-
Very little of this gets tested on Windows hosts. Windows Subsystem for Linux (WSL) is used where necessary with the default Ubuntu LTS install. Moved bulk of document to the [markdown](markdown/) directory to opt-in vs. opt-out of documentation.
13-
14-
Be the change et al if Windows is your main and you wanna raise a PR with broad instructions on getting tooling working under Windows (e.g., docker, poetry, playwright.)
6+
Udacity course on data science.
157

168
**Table of Contents**
17-
* [python_template](#python_template)
9+
* [wine_quality](#wine_quality)
1810
* [Summary](#summary)
19-
* [Caveat Emptor](#caveat-emptor)
2011
* [Setup](#setup)
2112
* [Usage](#usage)
22-
* [Mac and Linux users](#mac-and-linux-users)
13+
* [Poetry](#poetry)
14+
* [Docker](#docker)
15+
* [Both](#both)
2316
* [TODO](#todo)
17+
* [Further Reading](#further-reading)
2418

2519
## Setup
2620
* Install
2721
* [editorconfig](https://editorconfig.org/)
28-
* [wsl](https://docs.microsoft.com/en-us/windows/wsl/setup/environment)
2922
* [asdf](https://asdf-vm.com/guide/getting-started.html#_2-download-asdf)
3023
* [poetry](https://python-poetry.org/docs/)
31-
* [docker-compose](https://docs.docker.com/compose/install/)
32-
* [playwright](https://playwright.dev/python/docs/intro#installation)
33-
* [Kubernetes (k8s)](markdown/kubernetes.md)
24+
* [docker](https://docs.docker.com/compose/install/)
25+
* Download CSVs from [here](https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/)
26+
* Move to `csv` directory
3427

3528
## Usage
36-
### Mac and Linux users
37-
Development environments and tooling are first-class citizens on macOS and *nix. For Windows faithfuls, please setup [WSL](markdown/wsl.md).
29+
### Poetry
30+
* Install requirements via Poetry:
31+
```bash
32+
poetry install
33+
poetry run ipython kernel install --name "python3.10.7" --user
34+
```
35+
* Run Jupyter Lab
36+
```bash
37+
poetry shell
38+
jupyter lab --ip=0.0.0.0 --port=8888 --no-browser
39+
```
40+
* Quit the server via `ctrl-c` in the terminal
41+
* Enter `deactivate` to exit the Poetry virtual environment
42+
43+
### Docker
44+
* Customize the `.env.example` and rename to `.env`
45+
* General commands
46+
```bash
47+
# build image locally
48+
docker-compose build --pull --no-cache
49+
50+
# start container
51+
docker-compose up -d
52+
53+
# stop container
54+
docker-compose stop
55+
56+
# remove container and network
57+
docker-compose down
58+
```
59+
60+
### Both
61+
* Open a browser and navigate to `http://127.0.0.1:888`
62+
* Docker uses the token specified in `.env`
63+
* Select the "python3.10.7" kernel if asked
64+
* Open `refactor-wine-quality.ipynb` from the left-hand column
65+
* Run cells by selecting them and pressing `shift-enter`
3866

3967
## TODO
40-
* Django
41-
* Merge with [docker_python](https://github.com/pythoninthegrass/docker_python) and put the latter on an ice float
42-
* Flask
43-
* Bonus points for [Svelte](https://svelte.dev/blog/the-easiest-way-to-get-started) front-end ❤️
44-
* FastAPI
45-
* MongoDB
46-
* Switch to `docker-compose`
47-
* Fix unique index deleting too many keys
48-
* k8s
49-
* `~/.kubeconfig`
50-
* ansible
51-
* wsl
52-
* VSCode
53-
* Remote WSL install and usage
54-
* Or at least further reading nods
55-
* Debugging
56-
* Dependencies
57-
* script itself via [icecream](https://github.com/gruns/icecream)
68+
* Get Jupyter working in [VSCode](https://github.com/microsoft/vscode-jupyter)
69+
* `Makefile`
70+
71+
## Further Reading
72+
[Starting JupyterLab](https://jupyterlab.readthedocs.io/en/stable/getting_started/starting.html)
73+
74+
[Jupyter Docker Stacks — Docker Stacks documentation](https://jupyter-docker-stacks.readthedocs.io/en/latest/index.html)
75+
76+
[Dockerizing Jupyter Projects](https://towardsdatascience.com/dockerizing-jupyter-projects-39aad547484a)

compose.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: "3.9"
2+
3+
services:
4+
jupyter:
5+
image: jupyter/minimal-notebook:latest
6+
tty: true # false for `entrypoint` in Dockerfile
7+
stdin_open: true # false for `entrypoint` in Dockerfile
8+
env_file:
9+
- ./.env
10+
volumes:
11+
- .:/home/jovyan/work
12+
ports:
13+
- 8888:${PORT:-8888}
14+
build:
15+
context: ./
16+
dockerfile: ./Dockerfile
17+
18+
networks:
19+
default:
20+
driver: bridge

0 commit comments

Comments
 (0)