Skip to content

Commit 23cfb9e

Browse files
committed
Docker flask app
1 parent bb8f601 commit 23cfb9e

File tree

6 files changed

+168
-0
lines changed

6 files changed

+168
-0
lines changed

ArgoCDLocal/go-docker/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
hello_server
2+
go-docker
3+
out/
4+
vendor/
5+
_vendor-*

Docker/flask-app/Dockerfile

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# our base image
2+
FROM alpine:3.5
3+
4+
# Install python and pip
5+
RUN apk add --update py2-pip
6+
7+
# upgrade pip
8+
RUN pip install --upgrade pip
9+
10+
# install Python modules needed by the Python app
11+
COPY requirements.txt /usr/src/app/
12+
RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt
13+
14+
# copy files required for the app to run
15+
COPY app.py /usr/src/app/
16+
COPY templates/index.html /usr/src/app/templates/
17+
18+
# tell the port number the container should expose
19+
EXPOSE 5000
20+
21+
# run the application
22+
CMD ["python", "/usr/src/app/app.py"]

Docker/flask-app/cerficates/ssl.crt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PORTUS_CHECK_SSL_USAGE_ENABLED=false

Docker/flask-app/init

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/bin/bash
2+
3+
# This script will ensure Portus' database is ready to be used. It will keep
4+
# waiting for the db to be usable, but the script will exit with an error
5+
# after a certain amount of failed attempts.
6+
#
7+
# The script will automatically import all the SSL certificates from
8+
# `/certificates` into the final system. This is needed to talk with the
9+
# registry API when this one is protected by TLS.
10+
#
11+
# Finally the script will start apache running Portus via mod_rails.
12+
13+
set -e
14+
15+
wait_for_database() {
16+
should_setup=${1:-0}
17+
18+
TIMEOUT=90
19+
COUNT=0
20+
RETRY=1
21+
22+
while [ $RETRY -ne 0 ]; do
23+
case $(portusctl exec --vendor rails r /srv/Portus/bin/check_db.rb | grep DB) in
24+
"DB_DOWN")
25+
if [ "$COUNT" -ge "$TIMEOUT" ]; then
26+
printf " [FAIL]\n"
27+
echo "Timeout reached, exiting with error"
28+
exit 1
29+
fi
30+
echo "Waiting for mariadb to be ready in 5 seconds"
31+
sleep 5
32+
COUNT=$((COUNT+5))
33+
;;
34+
"DB_EMPTY"|"DB_MISSING")
35+
if [ $should_setup -eq 1 ]; then
36+
# create db, apply schema and seed
37+
echo "Initializing database"
38+
portusctl exec --vendor rake db:setup
39+
if [ $? -ne 0 ]; then
40+
echo "Error at setup time"
41+
exit 1
42+
fi
43+
fi
44+
;;
45+
"DB_READY")
46+
echo "Database ready"
47+
break
48+
;;
49+
esac
50+
done
51+
set -e
52+
}
53+
54+
setup_database() {
55+
wait_for_database 1
56+
}
57+
58+
# Usage: file_env 'XYZ_DB_PASSWORD' 'example'. This code is taken from:
59+
# https://github.com/docker-library/postgres/blob/master/docker-entrypoint.sh
60+
file_env() {
61+
local var="$1"
62+
local fileVar="${var}_FILE"
63+
if [ -v "${var}" ] && [ -v "${fileVar}" ]; then
64+
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
65+
exit 1
66+
fi
67+
if [ -v "${fileVar}" ]; then
68+
val="$(< "${!fileVar}")"
69+
export "$var"="$val"
70+
fi
71+
unset "$fileVar"
72+
}
73+
74+
# Setup environment variables from secrets.
75+
secrets=( PORTUS_DB_PASSWORD PORTUS_PASSWORD PORTUS_SECRET_KEY_BASE
76+
PORTUS_EMAIL_SMTP_PASSWORD PORTUS_LDAP_AUTHENTICATION_PASSWORD )
77+
for s in "${secrets[@]}"; do
78+
if [[ -z "${!s}" ]]; then
79+
file_env "$s"
80+
fi
81+
done
82+
83+
# Ensure additional certificates (e.g. docker registry) are known.
84+
update-ca-certificates
85+
86+
# Further settings
87+
export PORTUS_PUMA_HOST="0.0.0.0:3000"
88+
export RACK_ENV="production"
89+
export RAILS_ENV="production"
90+
export CCONFIG_PREFIX="PORTUS"
91+
92+
if [ -z "$PORTUS_GEM_GLOBAL" ]; then
93+
export GEM_PATH="/srv/Portus/vendor/bundle/ruby/2.6.0"
94+
fi
95+
96+
# On debug, print the environment in which we'll call Portus.
97+
if [ "$PORTUS_LOG_LEVEL" == "debug" ]; then
98+
printenv
99+
fi
100+
101+
# Go to the Portus directory and execute the proper command.
102+
cd /srv/Portus
103+
if [ ! -z "$PORTUS_BACKGROUND" ]; then
104+
wait_for_database
105+
portusctl exec --vendor rails r /srv/Portus/bin/background.rb
106+
elif [ -z "$PORTUS_INIT_COMMAND" ]; then
107+
setup_database
108+
portusctl exec --vendor "pumactl -F /srv/Portus/config/puma.rb start"
109+
else
110+
wait_for_database
111+
portusctl exec --vendor "$PORTUS_INIT_COMMAND"
112+
fi

Docker/flask-app/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Flask==1.0

Docker/flask-app/templates/index.html

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<html>
2+
<head>
3+
<style type="text/css">
4+
body {
5+
background: black;
6+
color: white;
7+
}
8+
div.container {
9+
max-width: 500px;
10+
margin: 100px auto;
11+
border: 20px solid white;
12+
padding: 10px;
13+
text-align: center;
14+
}
15+
h4 {
16+
text-transform: uppercase;
17+
}
18+
</style>
19+
</head>
20+
<body>
21+
<div class="container">
22+
<h4>Cat Gif of the day</h4>
23+
<img src="{{url}}" />
24+
<p><small>Courtesy: <a href="http://www.buzzfeed.com/copyranter/the-best-cat-gif-post-in-the-history-of-cat-gifs">Buzzfeed</a></small></p>
25+
</div>
26+
</body>
27+
</html>

0 commit comments

Comments
 (0)