Skip to content

Commit

Permalink
Update Dockerfile for improved workflow with VS Code Dev Containers
Browse files Browse the repository at this point in the history
Additionally, move gunicorn config to a config file and run app under
dedicated user account.
  • Loading branch information
clayoster committed Dec 22, 2024
1 parent e5735e6 commit bf6f719
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"dockerFile": "../Dockerfile",
"build": {
"target": "dev"
},
"context": "..",
"forwardPorts": [8080,8080]
}
45 changes: 38 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
# Use an official Python runtime as a parent image
FROM python:3.12-alpine
################
## Base Stage ##
################
# Use an official Python runtime as the base image
FROM python:3.12-alpine AS base

# Set the working directory in the container
# Allow stdout from python app through to docker logs
ENV PYTHONUNBUFFERED=1

# Define the working directory
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install available updates and python packages from requirements.txt
# Install available updates, python packages from requirements.txt
# and create app user
RUN set -ex \
&& apk upgrade --available --no-cache \
&& rm -rf /var/cache/apk/* \
&& pip install --no-cache-dir -r requirements.txt \
&& pip cache purge
&& pip cache purge \
&& adduser -D app

# Expose port 8080
EXPOSE 8080

# Run the python app with gunicorn and bind to port 8080
CMD ["gunicorn", "-w", "2", "--error-logfile", "-", "--log-level", "debug", "-b", "0.0.0.0:8080", "app:app"]
###############
## Dev Stage ##
###############
FROM base AS dev

# Install development tools
RUN set -ex \
&& apk add git \
&& rm -rf /var/cache/apk/* \
&& pip install --no-cache-dir pylint pytest pytest-flask \
&& pip cache purge

# Add environment variables
ENV PYTHONDONTWRITEBYTECODE=1

################
## Prod Stage ##
################
FROM base AS prod

# Run app as 'app' user
USER app

# Run the python app with gunicorn (settings are in gunicorn.conf.py)
CMD ["gunicorn", "app:app"]
6 changes: 6 additions & 0 deletions gunicorn.conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
workers = 1
accesslog = '-'
errorlog = '-'
#loglevel = 'debug'
access_log_format = '%({x-forwarded-for}i)s %(l)s %(t)s %(r)s %(s)s %(b)s %(f)s %(a)s'
bind = ['0.0.0.0:8080']

0 comments on commit bf6f719

Please sign in to comment.