Skip to content

Commit ffe83a7

Browse files
author
gpadres
committed
First commit
1 parent 3678539 commit ffe83a7

File tree

4 files changed

+54
-43
lines changed

4 files changed

+54
-43
lines changed

Dockerfile

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,20 @@
1-
#Start from the base image Ubuntu 18.04i
2-
FROM ubuntu:18.04
1+
# Use an official Python runtime as a parent image
2+
FROM python:2.7-slim
33

4-
#Define a few environement variables
5-
ENV PORT 8888
4+
# Set the working directory to /app
5+
WORKDIR /app
66

7-
# The following (especially the first) is needed to deal with .deb package
8-
# tzdata, which otherwise would ask for input from the terminal
9-
ENV DEBIAN_FRONTEND=noninteractive
10-
ENV TZ="/usr/share/zoneinfo/Europe/Stockholm"
11-
RUN echo $TZ > /etc/timezone
7+
# Copy the current directory contents into the container at /app
8+
COPY . /app
129

13-
### Install .deb packages
14-
# apt_packages.list must contain one package per line with an optional comment. The package name *** must be followed by a TAB *** before the comment
15-
COPY apt_packages.list /
16-
RUN apt-get update 1>/dev/null && apt-get install -y `cat /apt_packages.list | awk -F'\t' '($1!~/^#/) && ($1!~/^$/) {print $1}' ` 1> /dev/null && apt-get clean && rm -rf /var/lib/apt/lists/*
10+
# Install any needed packages specified in requirements.txt
11+
RUN pip install --trusted-host pypi.python.org -r requirements.txt
1712

18-
#Install Python and JupyterLab
19-
RUN python3 -m pip install --upgrade pip setuptools
20-
RUN python3 -m pip install --upgrade pyzmq
21-
RUN python3 -m pip install jupyterlab
13+
# Make port 80 available to the world outside this container
14+
EXPOSE 80
2215

23-
#Install the Python3 kernel
24-
RUN python3 -m pip install ipykernel && python3 -m ipykernel install
16+
# Define environment variable
17+
ENV NAME World
2518

26-
#Install extra python packages defined in the list
27-
# python_packages.list describes one package per line with a structure "name_pkg-TAB-[Python2/Python3/*empty]-TAB-#Comment"
28-
# An empty second columns means the package is installed in both Python2 and Python3.
29-
# Python2 in the second column means that the package should be installed in python2 only.
30-
# Python3 in the second column means that the package should be installed in Python3 only.
31-
COPY python_packages.list /
32-
RUN python3 -m pip install --upgrade `cat python_packages.list | awk -F'\t' '($1!~/^#/) && ($1!~/^$/) && (($2=="") || ($2=="Python3")) {print $1}' `
33-
34-
#Jupyter will be run rooted into that folder - mount whatever the user needs into that folder
35-
RUN mkdir /data
36-
37-
#Fix some folders and permissions so that the container can be run as non-root
38-
RUN mkdir /.jupyter && mkdir /.local && chmod 777 /.jupyter && chmod 777 /.local
39-
40-
#The command to run it all
41-
CMD cd /data && /usr/local/bin/jupyter lab --port=$PORT --ip=0.0.0.0 --no-browser --allow-root --LabApp.token=''
19+
# Run app.py when the container launches
20+
CMD ["python", "app.py"]

app.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from flask import Flask
2+
from redis import Redis, RedisError
3+
import os
4+
import socket
5+
6+
# Connect to Redis
7+
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)
8+
9+
app = Flask(__name__)
10+
11+
@app.route("/")
12+
def hello():
13+
try:
14+
visits = redis.incr("counter")
15+
except RedisError:
16+
visits = "<i>cannot connect to Redis, counter disabled</i>"
17+
18+
html = "<h3>Hello {name}!</h3>" \
19+
"<b>Hostname:</b> {hostname}<br/>" \
20+
"<b>Visits:</b> {visits}"
21+
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)
22+
23+
if __name__ == "__main__":
24+
app.run(host='0.0.0.0', port=80)

readme.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
# Light JupyterLab with Python3 example
1+
# First example of Docker with Python Flask app
22

3-
1. Build image `docker build --tag=jupyter_light . `
3+
1. Build image `docker build --tag=friendlyhello .`
44

5-
1. Run image ``docker run -ti -p 8888:8888 -v "`pwd`":/data jupyter_light``
5+
1. Check for images `docker image ls`
66

7-
Note: you need to run the image in the directory you want JupyterLab to access.
7+
1. Run image ``docker run -p 4000:80 friendlyhello``
88

9-
Alternatively you can pull the image:
9+
Note: the `-p` maps the host's port 4000 to the container port 80.
1010

11-
``docker run -ti -p 8888:8888 -v "`pwd`":/data gpadres/docker_python:jupyter_light``
11+
1. From host computer, via a browser window access Flask App on address `localhost:4000`
1212

13-
Running the image will start JupyterLab in the container.
13+
1. Hit `CTRL+C` to quit in the terminal.
1414

15-
1. From host computer, via a browser window access JupyterLab on address `localhost:8888`
15+
Alternatively:
16+
17+
1. Run image in detached mode: ``docker run -d -p 4000:80 friendlyhello``
18+
19+
1. List containers that are running `docker ps`. Look for the `CONTAINER_ID` that is running.
20+
21+
1. Stop container `docker container stop <CONTAINER_ID>`

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Flask
2+
Redis

0 commit comments

Comments
 (0)