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

Run nginx and gunicorn in docker via supervisor #1313

Merged
merged 7 commits into from Jul 13, 2018

Conversation

Projects
None yet
2 participants
@c-w
Copy link
Contributor

commented Jul 10, 2018

Currently the nginx process is run in the background of the docker container and the gunicorn process is run in the foreground. This means that if the nginx process crashes, it won't get restarted and the nginx
logs don't show up in the docker logs since only the foreground gunicorn logs are captured. More generally, it is also not recommended to run multiple processes in a single docker container unless a process manager is used (more information).

This change introduces the supervisor process manager to run as the single foreground process in docker. The supervisor process in turn takes care of running both nginx and gunicorn.

Note that supervisor has its own environment variable isolation and doesn't have an easy way of passing all the environment variables to the child process. As a work-around to avoid having to declare all environment variables multiple times, we first dump the environment into files and then reload them in the child process from the files via the envdir utility.

Also:

  • Always dereference envs via os.getenv to make it easier to find all the environment variables used in the project via grep.
  • Modify the gunicorn custom logger to fix line length and unused import warnings and fix runtime crash when not in GCP or Azure.
  • Fix startup crash in docker-compose due to missing bash executable.

@c-w c-w requested a review from colinschoen Jul 10, 2018

c-w added some commits Jul 10, 2018

Fix startup crash in docker-compose
The ok-server image doesn't contain bash.
Run nginx and gunicorn in docker via supervisor
Currently the nginx process is run in the background of the docker
container and the gunicorn process is run in the foreground. This means
that if the nginx process crashes, it won't get restarted and the nginx
logs don't show up in the docker logs since only the foreground gunicorn
logs are captured. More generally, it is also not recommended to run
multiple processes in a single docker container unless a process manager
is used (more information can be found in [1]).

This change introduces the supervisor process manager to run as the
single foreground process in docker. The supervisor process in turn
takes care of running both nginx and gunicorn.

Note that supervisor has its own environment variable isolation and
doesn't have an easy way of passing all the environment variables to the
child process. As a work-around to avoid having to declare all
environment variables multiple times, we first dump the environment into
files and then reload them in the child process from the files via the
envdir utility [2].

[1] https://docs.docker.com/config/containers/multi-service_container/
[2] https://github.com/jezdez/envdir
@colinschoen
Copy link
Member

left a comment

LGTM

Excellent

@colinschoen colinschoen merged commit f00ebc0 into okpy:master Jul 13, 2018

3 checks passed

Travis CI - Pull Request Build Passed
Details
ci/circleci Your tests passed on CircleCI!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.