Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure Docker image for production #111

Closed
Tracked by #608
thekaveman opened this issue Aug 3, 2022 · 2 comments · Fixed by #123
Closed
Tracked by #608

Configure Docker image for production #111

thekaveman opened this issue Aug 3, 2022 · 2 comments · Fixed by #123
Assignees
Labels
deployment Related to deployments of the server

Comments

@thekaveman
Copy link
Member

thekaveman commented Aug 3, 2022

The current Docker image (via bin/start.sh) starts the Flask development server.

This has been fine as we've only been using the server for testing. As we move to deploy the server for Courtesy Cards, we'll want to run Flask using production best-practices.

See more at: Flask - Deploying to Production.

In general, we can follow a similar pattern as in benefits:

  • nginx is the reverse proxy that accepts traffic coming into the container, and routes app traffic along
  • gunicorn is the WSGI application server, receiving app traffic from nginx and forwarding to the app (Flask)
  • bin/start.sh starts the production setup
  • From within the devcontainer, the development setup is used
@thekaveman thekaveman moved this to This Sprint (Month) in Digital Services Aug 3, 2022
@thekaveman
Copy link
Member Author

Idea for an approach to this... Thoughts @angela-tran @machikoyasuda @afeld?

Issue

We're going to end up with a Dockerfile here that has a lot of crossover with benefits, at least the commands for installing/configuring nginx and gunicorn:

FROM python:3.10

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    USER=calitp

    # create non-root $USER and home directory
RUN useradd --create-home --shell /bin/bash $USER && \
    # setup $USER permissions for nginx
    mkdir -p /var/cache/nginx && \
    chown -R $USER /var/cache/nginx && \
    mkdir -p /var/lib/nginx && \
    chown -R $USER /var/lib/nginx && \
    mkdir -p /var/log/nginx && \
    chown -R $USER /var/log/nginx && \
    touch /var/log/nginx/error.log && \
    chown $USER /var/log/nginx/error.log && \
    touch /var/run/nginx.pid && \
    chown -R $USER /var/run/nginx.pid && \
    # setup directories and permissions for app and gunicorn
    mkdir -p /home/$USER/app/config && \
    mkdir -p /home/$USER/app/run && \
    chown -R $USER /home/$USER && \
    # install server components
    apt-get update && \
    apt-get install -qq --no-install-recommends build-essential nginx

# enter app directory
WORKDIR /home/$USER/app

# switch to non-root $USER
USER $USER

# update PATH for local pip installs
ENV PATH "$PATH:/home/$USER/.local/bin"

# install python dependencies (gunicorn, etc.)
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

# copy config files
COPY gunicorn.conf.py gunicorn.conf.py

# overwrite default nginx.conf
COPY nginx.conf /etc/nginx/nginx.conf

Proposal

Let's create another repository that builds and publishes a common base image for Python + gunicorn + nginx.

This would provide an image to reference like ghcr.io/cal-itp/<image>:<tag>. Both of eligibility-server and benefits could base their app images on this new image and simplify the common setup and configuration (and build times).

@thekaveman
Copy link
Member Author

I've started working on this.

Repo here: https://github.com/cal-itp/docker-python-web

I have the eligibility_server working locally against that common image! Will push up a branch tonight/tomorrow.

@thekaveman thekaveman self-assigned this Aug 18, 2022
@thekaveman thekaveman moved this from This Sprint (Month) to In Progress in Digital Services Aug 18, 2022
@thekaveman thekaveman added the deployment Related to deployments of the server label Aug 18, 2022
Repository owner moved this from In Progress to Done in Digital Services Aug 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deployment Related to deployments of the server
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant