Docker: Difference between revisions
No edit summary |
|||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== Python Dockerfile == | == Python Dockerfile for Flask == | ||
<pre>FROM python:3.11-slim as build | <pre>FROM python:3.11-slim as build | ||
Line 20: | Line 20: | ||
</pre> | </pre> | ||
== Ignoring Files (.dockerfile) == | |||
<pre> | |||
.idea/ | |||
.git/ | |||
venv/ | |||
__pycache__/ | |||
database/ | |||
</pre> | |||
== Maintaining Files Between Builds == | == Maintaining Files Between Builds == | ||
Line 30: | Line 35: | ||
<pre>docker run --mount type=bind,src=/home/docker,target=/app/database -dp 5000:5000 fnv</pre> | <pre>docker run --mount type=bind,src=/home/docker,target=/app/database -dp 5000:5000 fnv</pre> | ||
== Pycharm Example == | |||
[[File:Docker deploy example.png]] | |||
== Run == | == Run == |
Latest revision as of 04:50, 21 February 2024
Python Dockerfile for Flask
FROM python:3.11-slim as build EXPOSE 5000/tcp WORKDIR /app ## build python environment from requirements.txt COPY requirements.txt . RUN pip install -r requirements.txt ## copy everything except the database directory (will be mapped) \ ## note that for this implementation, the app needs to initialize its database copy . . RUN rm -Rf app/database ## launch the flask app ENTRYPOINT FLASK_APP=/app/app.py flask run --host=0.0.0.0 --port=5000
Ignoring Files (.dockerfile)
.idea/ .git/ venv/ __pycache__/ database/
Maintaining Files Between Builds
Docker will overwrite files inside the container between builds and resources like databases and cache files may be overwritten. To work around this, it can be useful to create mount points that will reference volumes on the host server (or another server).
docker run --mount type=bind,src=/home/docker,target=/app/database -dp 5000:5000 fnv
Pycharm Example
Run
Not ideal because it resets whenever you start the container. Need a better way:
sudo docker run -it -p 8888:8888 jupyter/pyspark-notebook jupyter notebook --no-browser --allow-root --ip=0.0.0.0
Then access it from a browser:
You'll need the token from the docker terminal the first time accessing the notebook.
Get Shell to Container
Get a list of running containers and find the id for your specific container:
[bpopp@p1 pubs]$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e0fd0ef0e5c0 jupyter/all-spark-notebook "tini -g -- jupyter …" 22 hours ago Up 22 hours 0.0.0.0:8888->8888/tcp kind_sanderson
Launch a shell
sudo docker exec -it e0fd0ef0e5c0 /bin/bash
Or as root
sudo docker exec -u root -it e0fd0ef0e5c0 /bin/bash
To install something
pip install plotnine --proxy http://myproxy.somewhere.com:3128