Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ WORKDIR /src

COPY requirements.txt .

# Install the required Python packages
# Install python dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy the application code into the container
# Copy the app into the container
COPY src .

# Expose the port the app runs on
# Expose the port
EXPOSE 11000

# Custom monitoring script to check container health
HEALTHCHECK --interval=3m CMD python /src/docker-healthcheck.py

# Command to run the application
# Command to run
CMD ["python", "app.py"]

# A production setup should use a production server, uvicorn or similar e.g.
Expand Down
15 changes: 13 additions & 2 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ docker stop simple-docker-webapp
docker rm simple-docker-webapp
docker build -t simple-docker-webapp .

# This is not production ready, Flask is a dev server
# Should use the -u flag to specify a user other than root
# DISCLAIMERS
# This app is not production fit, flask is only a dev server.
# In production the -u flag should specify an appropriate production user ID
# with scoped privileges.
# A restart policy is not specified here, but could be used. YMMV.
# Further considerations for web security / scalability, not exemplified here:
# - Load balancing (appliance or cloud), auto scaling
# - Reverse proxying / Defense-In-Depth / Application Firewalls
# - Container Limits (cgroups / disk quotas etc)
# - Automated container hygiene, log rotation, SIEM tools (splunk etc)
# - Credential vaults / Certificate provisioning / Auto-renewal
# - Caching / Edge services / Cloudflare / Fastly etc
#
docker run -d -p 11000:11000 --name simple-docker-webapp simple-docker-webapp
5 changes: 4 additions & 1 deletion src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

app = Flask(__name__)
PORT = config("PORT", default="11000")
logging = get_logger()
logging = get_logger(Path(__file__).name)


@app.route("/hello", methods=["GET"])
def hello_world():
"""Returns a hello string in JSON format. Ideally these docstrings
would be compiled with a documentation tool such as readthedocs.io...
"""
logging.info("Hello world called")
return jsonify(
{"message": "hello world - visit https://github.com/bytebraid/simple-docker-webapp"}
Expand Down
8 changes: 8 additions & 0 deletions src/docker-healthcheck.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
""" This class does a simple health check and returns a POSIX
status code, 0 if healthy, 1 if the app is not responding
correctly. Docker runs this healthcheck periodically to
ascertain if the container is behaving correctly. It can stop
the container if the healthcheck fails, and restart if
a policy is defined.
"""

import urllib.request
import sys
from tools.logs import get_logger
Expand Down
6 changes: 5 additions & 1 deletion src/tools/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def get_logger(
LOG_FILE_INFO=str(LOG_INFO),
LOG_FILE_ERROR=str(LOG_ERROR),
):
"""Returns a custom logger tailored for the calling context
providing precise details of line number and file along
with the message or exception and stack trace.
"""
log = logging.getLogger(LOG_NAME)
log_formatter = logging.Formatter(LOG_FORMAT)

Expand All @@ -37,7 +41,7 @@ def get_logger(
except Exception:
print("Logging to console only")
pass
# Dynamic debug switch?

log.setLevel(LOG_LEVEL)

return log
Loading