Skip to content

Commit 2c6ea2f

Browse files
committed
Release 0.2
1 parent fdd2829 commit 2c6ea2f

File tree

5 files changed

+167
-64
lines changed

5 files changed

+167
-64
lines changed

.env.example

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
### Flask settings
33
###
44

5+
# Name of the entrypoint directory
6+
FLASK_APP_DIR=app
7+
58
# Name of the entrypoint file
6-
FLASK_APP=main.py
9+
FLASK_APP_FILE=main.py
710

811
# Name of the project directory which is mounted
912
# to /shared/httpd into the container
@@ -21,8 +24,8 @@ PYTHON_VERSION=3.8
2124
###
2225

2326
# uid/gid of local user for permission syncronization
24-
NEW_GID=1000
2527
NEW_UID=1000
28+
NEW_GID=1000
2629

2730

2831
###

Dockerfile

+6-9
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,15 @@ COPY --from=builder-dev /usr/local/lib/python${PYTHON}/site-packages/ /usr/local
4545
COPY --from=builder-dev /usr/local/bin/flask /usr/local/bin/flask
4646
COPY --from=builder-dev /usr/local/bin/virtualenv /usr/local/bin/virtualenv
4747

48-
# Build args
49-
ARG UID=1000
50-
ARG GID=1000
51-
52-
# User
48+
# User and default dir
5349
RUN set -x \
54-
&& addgroup -g ${GID} devilbox \
55-
&& adduser -h /home/devilbox -G devilbox -D -u ${UID} devilbox
50+
&& addgroup -g 1000 devilbox \
51+
&& adduser -h /home/devilbox -G devilbox -D -u 1000 devilbox \
52+
&& mkdir -p /shared/httpd \
53+
&& chmod 0755 /shared/httpd \
54+
&& chown devilbox:devilbox /shared/httpd
5655

5756
# Start script
58-
ENV NEW_UID=${UID}
59-
ENV NEW_GID=${GID}
6057
COPY data/start.sh /start.sh
6158

6259
# Start

README.md

+62-12
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,61 @@
99

1010
This project provides a Python Flask Docker image for development purposes.
1111

12+
View **[Dockerfile](https://github.com/devilbox/docker-python-flask/blob/master/Dockerfile)** on GitHub.
13+
14+
15+
| Docker Hub | Upstream Project |
16+
|------------|------------------|
17+
| <a href="https://hub.docker.com/r/devilbox/python-flask"><img height="82px" src="http://dockeri.co/image/devilbox/python-flask" /></a> | <a href="https://github.com/cytopia/devilbox" ><img height="82px" src="https://raw.githubusercontent.com/devilbox/artwork/master/submissions_banner/cytopia/01/png/banner_256_trans.png" /></a> |
18+
19+
20+
#### Documentation
21+
22+
In case you seek help, go and visit the community pages.
23+
24+
<table width="100%" style="width:100%; display:table;">
25+
<thead>
26+
<tr>
27+
<th width="33%" style="width:33%;"><h3><a target="_blank" href="https://devilbox.readthedocs.io">Documentation</a></h3></th>
28+
<th width="33%" style="width:33%;"><h3><a target="_blank" href="https://gitter.im/devilbox/Lobby">Chat</a></h3></th>
29+
<th width="33%" style="width:33%;"><h3><a target="_blank" href="https://devilbox.discourse.group">Forum</a></h3></th>
30+
</tr>
31+
</thead>
32+
<tbody style="vertical-align: middle; text-align: center;">
33+
<tr>
34+
<td>
35+
<a target="_blank" href="https://devilbox.readthedocs.io">
36+
<img title="Documentation" name="Documentation" src="https://raw.githubusercontent.com/cytopia/icons/master/400x400/readthedocs.png" />
37+
</a>
38+
</td>
39+
<td>
40+
<a target="_blank" href="https://gitter.im/devilbox/Lobby">
41+
<img title="Chat on Gitter" name="Chat on Gitter" src="https://raw.githubusercontent.com/cytopia/icons/master/400x400/gitter.png" />
42+
</a>
43+
</td>
44+
<td>
45+
<a target="_blank" href="https://devilbox.discourse.group">
46+
<img title="Devilbox Forums" name="Forum" src="https://raw.githubusercontent.com/cytopia/icons/master/400x400/discourse.png" />
47+
</a>
48+
</td>
49+
</tr>
50+
<tr>
51+
<td><a target="_blank" href="https://devilbox.readthedocs.io">devilbox.readthedocs.io</a></td>
52+
<td><a target="_blank" href="https://gitter.im/devilbox/Lobby">gitter.im/devilbox</a></td>
53+
<td><a target="_blank" href="https://devilbox.discourse.group">devilbox.discourse.group</a></td>
54+
</tr>
55+
</tbody>
56+
</table>
57+
1258

1359
## Docker image tags
1460

1561
* **Image name:** `devilbox/python-flask`
1662

1763
### Rolling tags
1864

65+
Rolling tags are updated and pushed nightly to ensure latest patch-level Python version.
66+
1967
| Image tag | Python version |
2068
|-----------|----------------|
2169
| `3.8-dev` | Latest 3.8.x |
@@ -52,13 +100,14 @@ curl localhost:3000
52100

53101
## Environment Variables
54102

55-
| Variable | Required | Description |
56-
|-----------------|----------|-------------|
57-
| `FLASK_PROJECT` | Yes | The directory name in `/shared/httpd/` to serve [1] |
58-
| `FLASK_APP` | | The main entrypoint file name (default is `main.py`) |
59-
| `FLASK_PORT` | | Docker internal port to serve the application (default is `3000`) |
60-
| `NEW_UID` | | User id of the host system to ensure syncronized permissions between host and container |
61-
| `NEW_GID` | | Group id of the host system to ensure syncronized permissions between host and container |
103+
| Variable | Required | Default | Description |
104+
|------------------|----------|-----------| ------------|
105+
| `FLASK_PROJECT` | Yes | | The sub-directory name under `/shared/httpd/` to serve [1] |
106+
| `FLASK_APP_DIR` | | `app` | The main entrypoint dir name |
107+
| `FLASK_APP_FILE` | | `main.py` | The main entrypoint file name |
108+
| `FLASK_PORT` | | `3000` | Docker container internal http port to serve the application |
109+
| `NEW_UID` | | `1000` | User id of the host system to ensure syncronized permissions between host and container |
110+
| `NEW_GID` | | `1000` | Group id of the host system to ensure syncronized permissions between host and container |
62111

63112
* [1] See [Project directory structure](#project-directory-structure) for usage
64113

@@ -72,21 +121,22 @@ The following shows how to organize your project on the host operating system.
72121
The following is the least required directory structure:
73122
```bash
74123
<project-dir>/
75-
└── app # Must be named 'app'
76-
   └── main.py # Entrypoint name can be changed via env var [1]
124+
└── app # Entrypoint dir name can be changed via env var [1]
125+
   └── main.py # Entrypoint file name can be changed via env var [2]
77126
```
78127

79-
* [1] Use the `FLASK_APP` environment variable to defined the file for the entrypoint in `<project-dir>/app/`. Example: `FLASK_APP=test.py`.
128+
* [1] Use the `FLASK_APP_DIR` environment variable to define the dir for the entrypoint in `<project-dir>/app/`. Example: `FLASK_APP_DIR=src`.
129+
* [2] Use the `FLASK_APP_FILE` environment variable to define the file for the entrypoint in `<project-dir>/app/main.py`. Example: `FLASK_APP_FILE=test.py`.
80130

81131

82132
### Structure with dependencies
83133

84134
The following directory structure allows for auto-installing Python dependencies during startup into a virtual env.
85135
```bash
86136
<project-dir>/
87-
├── app # Must be named 'app'
137+
├── app # Entrypoint dir name can be changed via env var
88138
│   ├── __init__.py
89-
│   └── main.py # Entrypoint name can be changed via env var
139+
│   └── main.py # Entrypoint file name can be changed via env var
90140
└── requirements.txt # Optional: will pip install in virtual env
91141
```
92142

data/start.sh

+91-39
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,31 @@
22

33
set -e
44
set -u
5-
set -x
65

76

87
###
98
### Default Variables
109
###
1110
DEFAULT_FLASK_PORT=3000
12-
DEFAULT_FLASK_APP="main.py"
11+
DEFAULT_FLASK_APP_DIR="app"
12+
DEFAULT_FLASK_APP_FILE="main.py"
13+
DEFAULT_NEW_UID=1000
14+
DEFAULT_NEW_GID=1000
15+
16+
17+
# -------------------------------------------------------------------------------------------------
18+
# Required Environment variables
19+
# -------------------------------------------------------------------------------------------------
20+
21+
###
22+
### Retrieve project directory
23+
###
24+
if ! env | grep '^FLASK_PROJECT=' >/dev/null; then
25+
>&2 echo "[ERR] FLASK_PROJECT variable not set"
26+
exit 1
27+
fi
28+
echo "[INFO] Setting internal project dir to: /shared/httpd/${FLASK_PROJECT}"
29+
PROJECT="/shared/httpd/${FLASK_PROJECT}"
1330

1431

1532
# -------------------------------------------------------------------------------------------------
@@ -19,72 +36,93 @@ DEFAULT_FLASK_APP="main.py"
1936
###
2037
### Custom listening port?
2138
###
22-
if env | grep '^FLASK_PORT=' >/dev/null; then
23-
DEFAULT_FLASK_PORT="$( env | grep '^FLASK_PORT=' | sed 's/^FLASK_PORT=//g' )"
39+
if ! env | grep '^FLASK_PORT=' >/dev/null; then
40+
echo "[INFO] Keeping internal Flask port at default: ${DEFAULT_FLASK_PORT}"
41+
else
42+
echo "[INFO] Setting internal Flask port to: ${FLASK_PORT}"
43+
DEFAULT_FLASK_PORT="${FLASK_PORT}"
44+
fi
45+
46+
47+
###
48+
### Custom default dir?
49+
###
50+
if ! env | grep '^FLASK_APP_DIR=' >/dev/null; then
51+
echo "[INFO] Keeping FLASK_APP_DIR at default: ${DEFAULT_FLASK_APP_DIR}"
52+
else
53+
echo "[INFO] Setting FLASK_APP_DIR to: ${FLASK_APP_DIR}"
54+
DEFAULT_FLASK_APP_DIR="${FLASK_APP_DIR}"
2455
fi
2556

2657

2758
###
2859
### Custom default file?
2960
###
30-
if env | grep '^FLASK_APP=' >/dev/null; then
31-
DEFAULT_FLASK_APP="$( env | grep '^FLASK_APP' | sed 's/^FLASK_APP=//g' )"
61+
if ! env | grep '^FLASK_APP_FILE=' >/dev/null; then
62+
echo "[INFO] Keeping FLASK_APP_FILE at default: ${DEFAULT_FLASK_APP_FILE}"
63+
else
64+
echo "[INFO] Setting FLASK_APP_FILE to: ${FLASK_APP_FILE}"
65+
DEFAULT_FLASK_APP_FILE="${FLASK_APP_FILE}"
3266
fi
3367

3468

3569
###
3670
### Adjust uid/gid
3771
###
38-
grep '^devilbox' /etc/group
39-
sed -i'' "s|^devilbox:.*$|devilbox:x:${NEW_GID}:devilbox|g" /etc/group
40-
grep '^devilbox' /etc/group
72+
if ! env | grep '^NEW_UID=' >/dev/null; then
73+
echo "[INFO] Keeping NEW_UID at default: ${DEFAULT_NEW_UID}"
74+
else
75+
echo "[INFO] Setting NEW_UID to: ${NEW_UID}"
76+
DEFAULT_NEW_UID="${NEW_UID}"
77+
fi
4178

42-
grep '^devilbox' /etc/passwd
43-
sed -i'' "s|^devilbox:.*$|devilbox:x:${NEW_UID}:${NEW_GID}:Linux User,,,:/home/devilbox:/bin/ash|g" /etc/passwd
44-
grep '^devilbox' /etc/passwd
79+
if ! env | grep '^NEW_GID=' >/dev/null; then
80+
echo "[INFO] Keeping NEW_GID at default: ${DEFAULT_NEW_GID}"
81+
else
82+
echo "[INFO] Setting NEW_GID to: ${NEW_GID}"
83+
DEFAULT_NEW_GID="${NEW_GID}"
84+
fi
4585

86+
sed -i'' "s|^devilbox:.*$|devilbox:x:${DEFAULT_NEW_GID}:devilbox|g" /etc/group
87+
ETC_GROUP="$( grep '^devilbox' /etc/group )"
88+
echo "[INFO] /etc/group: ${ETC_GROUP}"
4689

47-
# -------------------------------------------------------------------------------------------------
48-
# Required Environment variables
49-
# -------------------------------------------------------------------------------------------------
90+
sed -i'' "s|^devilbox:.*$|devilbox:x:${DEFAULT_NEW_UID}:${DEFAULT_NEW_GID}:Linux User,,,:/home/devilbox:/bin/ash|g" /etc/passwd
91+
ETC_PASSWD="$( grep '^devilbox' /etc/passwd )"
92+
echo "[INFO] /etc/passwd: ${ETC_PASSWD}"
5093

51-
###
52-
### Retrieve project directory
53-
###
54-
if ! env | grep '^FLASK_PROJECT=' >/dev/null; then
55-
>&2 echo "Error, FLASK_PROJECT variable not set."
56-
exit 1
57-
fi
58-
PROJECT="/shared/httpd/${FLASK_PROJECT}"
94+
chown "${DEFAULT_NEW_UID}:${DEFAULT_NEW_GID}" /home/devilbox
95+
chown "${DEFAULT_NEW_UID}:${DEFAULT_NEW_GID}" /shared/httpd
96+
PERM_HOME="$( ls -ld /home/devilbox )"
97+
PERM_DATA="$( ls -ld /shared/httpd )"
98+
echo "[INFO] ${PERM_HOME}"
99+
echo "[INFO] ${PERM_DATA}"
59100

60101

61102
# -------------------------------------------------------------------------------------------------
62103
# Validation
63104
# -------------------------------------------------------------------------------------------------
64105

65-
###
66-
### Check project directory
67-
###
106+
##
107+
## Check project directory
108+
##
68109
if [ ! -d "${PROJECT}" ]; then
69-
>&2 echo "Error, <project> directory does not exist: ${PROJECT}."
70-
exit 1
110+
>&2 echo "[WARN] <project> directory does not exist: ${PROJECT}"
71111
fi
72112

73113
###
74114
### Check project/app directory
75115
###
76-
if [ ! -d "${PROJECT}/app" ]; then
77-
>&2 echo "Error, <project>/app directory does not exist: ${PROJECT}/app."
78-
exit 1
116+
if [ ! -d "${PROJECT}/${DEFAULT_FLASK_APP_DIR}" ]; then
117+
>&2 echo "[WARN] <project>/${DEFAULT_FLASK_APP_DIR} directory does not exist: ${PROJECT}/${DEFAULT_FLASK_APP_DIR}"
79118
fi
80119

81120

82121
###
83122
### Check Entrypoint file
84123
###
85-
if [ ! -f "${PROJECT}/app/${DEFAULT_FLASK_APP}" ]; then
86-
>&2 echo "Error, <project>/app/${DEFAULT_FLASK_APP} entrypoint file does not exist: ${PROJECT}/app/${DEFAULT_FLASK_APP}."
87-
exit 1
124+
if [ ! -f "${PROJECT}/${DEFAULT_FLASK_APP_DIR}/${DEFAULT_FLASK_APP_FILE}" ]; then
125+
>&2 echo "[WARN] <project>/${DEFAULT_FLASK_APP_DIR}/${DEFAULT_FLASK_APP_FILE} entrypoint file does not exist: ${PROJECT}/${DEFAULT_FLASK_APP_DIR}/${DEFAULT_FLASK_APP_FILE}"
88126
fi
89127

90128

@@ -96,17 +134,31 @@ exec su -c "
96134
set -e
97135
set -u
98136
99-
cd \"${PROJECT}\"
137+
if [ ! -d \"${PROJECT}\" ]; then
138+
echo \"[INFO] Creating project directory: ${PROJECT}\"
139+
mkdir -p \"${PROJECT}\"
140+
fi
100141
142+
cd \"${PROJECT}\"
101143
if [ ! -d \"${PROJECT}/venv\" ]; then
102-
cd \"${PROJECT}\"
144+
echo \"[INFO] Creating Python virtual env: ${PROJECT}/venv\"
103145
virtualenv venv
104146
fi
105147
. venv/bin/activate
106148
107-
if [ -f \"${PROJECT}/requirements.txt\" ]; then
149+
if [ ! -f \"${PROJECT}/requirements.txt\" ]; then
150+
echo \"[INFO] No requirements.txt file found at: ${PROJECT}/requirements.txt\"
151+
else
152+
echo \"[INFO] Installing pip requirements from: ${PROJECT}/requirements.txt\"
108153
pip install -r \"${PROJECT}/requirements.txt\"
109154
fi
110155
111-
cd \"${PROJECT}/app\"
112-
FLASK_ENV=development FLASK_APP=${DEFAULT_FLASK_APP} flask run --host=0.0.0.0 --port=${DEFAULT_FLASK_PORT}" devilbox
156+
if [ ! -d \"${PROJECT}/${DEFAULT_FLASK_APP_DIR}\" ]; then
157+
echo \"[INFO] Creating project app directory: ${PROJECT}/${DEFAULT_FLASK_APP_DIR}\"
158+
mkdir -p \"${PROJECT}/${DEFAULT_FLASK_APP_DIR}\"
159+
fi
160+
161+
flask --version
162+
163+
cd \"${PROJECT}/${DEFAULT_FLASK_APP_DIR}\"
164+
FLASK_ENV=development FLASK_APP=${DEFAULT_FLASK_APP_FILE} flask run --host=0.0.0.0 --port=${DEFAULT_FLASK_PORT}" devilbox

docker-compose.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ services:
1616
- "${HOST_PORT_FLASK:-3000}:${FLASK_PORT:-3000}"
1717
environment:
1818
- FLASK_PORT=${FLASK_PORT:-3000}
19-
- FLASK_APP=${FLASK_APP:-main.py}
19+
- FLASK_APP_DIR=${FLASK_APP_DIR:-app}
20+
- FLASK_APP_FILE=${FLASK_APP_FILE:-main.py}
2021
- FLASK_PROJECT=${FLASK_PROJECT:-test-project}
21-
- NEW_GID=${NEW_UID:-1000}
2222
- NEW_UID=${NEW_UID:-1000}
23+
- NEW_GID=${NEW_GID:-1000}
2324
volumes:
2425
- ./test-project:/shared/httpd/${FLASK_PROJECT:-test-project}

0 commit comments

Comments
 (0)