bash: gunicorn: command not found

OhMyProblems

Hi guys, Have been trying for a whole day. And keep getting similar issues.

Using Procfile, My django deployment keeps failing with message

Preparing to run: `/cnb/process/web` as 1000
2022/01/24 13:32:16 listening on [xxxxxxxxxx]:22 (DNS: [xxxx::3]:53)
bash: gunicorn: command not found
Main child exited normally with code: 127

No idea why gunicorn command not found, It is in requirements.txt file

My Procfile:

# Modify this Procfile to fit your needs
web: gunicorn --pythonpath project myapp.wsgi:application --access-logfile -

fly.toml

fly.toml file generated for loonax on 2022-01-24T18:20:43+08:00

app = "myunknownapp"
kill_signal = "SIGINT"
kill_timeout = 5
processes = []
[build]
  builder = "paketobuildpacks/builder:base"
[env]
  ENVIRONMENT="production"
  DEBUG="False"
[experimental]
  allowed_public_ports = []
  auto_rollback = true
[[services]]
  internal_port = 8000
  # processes = ["app"]
  protocol = "tcp"
  script_checks = []
  [services.concurrency]
    hard_limit = 25
    soft_limit = 20
    type = "connections"
  [[services.ports]]
    handlers = ["http"]
    port = 80
  [[services.ports]]
    handlers = ["tls", "http"]
    port = 443
  [[services.tcp_checks]]
    # grace_period = "1s"
    grace_period = "30s"
    interval = "15s"
    restart_limit = 0
    timeout = "2s"
  [[services.http_checks]]
    interval = 900000  # Every 15 minutes.
    # grace_period = "5s"
    grace_period = "30s"
    method = "get"
    path = "/health"
    protocol = "http"
    timeout = 2000
    tls_skip_verify = false
    [services.http_checks.headers]

Another: Dockerfile problems

This is another problem, but if someone can point out my problems, this is greatly appreciated.

I switch to using Procfile because I had problems with Dockerfile deployment with flyio. The Dockerfile seem like not getting the environment variables set via flyctl secrets set SECRET_KEY="xxxx". Again I have no idea why.

The Errors I Got

It is a django error saying the SECRET_KEY cannot be empty. The deployment failed.
settings.py: SECRET_KEY = os.getenv(“DJANGO_SECRET_KEY”, get_random_secret_key())
Look like there is no such variable in the environment.

My Dockerfile

FROM python:3.8.12-slim-buster
ENV LANG C.UTF-8

ARG ENVIRONMENT
ARG DJANGO_SECRET_KEY
ARG DEBUG
ARG DATABASE_DEFAULT_ENGINE
ARG DATABASE_DEFAULT_NAME
ARG DATABASE_DEFAULT_USER
ARG DATABASE_DEFAULT_PASSWORD
ARG DATABASE_DEFAULT_HOST
ARG DATABASE_DEFAULT_PORT
ARG SENDGRID_API_KEY
ARG JWT_SECRET

ENV ENVIRONMENT=${ENVIRONMENT}
ENV DJANGO_SECRET_KEY=${DJANGO_SECRET_KEY}
ENV DEBUG=${DEBUG}
ENV DATABASE_DEFAULT_ENGINE=${DATABASE_DEFAULT_ENGINE}
ENV DATABASE_DEFAULT_NAME=${DATABASE_DEFAULT_NAME}
ENV DATABASE_DEFAULT_USER=${DATABASE_DEFAULT_USER}
ENV DATABASE_DEFAULT_PASSWORD=${DATABASE_DEFAULT_PASSWORD}
ENV DATABASE_DEFAULT_HOST=${DATABASE_DEFAULT_HOST}
ENV DATABASE_DEFAULT_PORT=${DATABASE_DEFAULT_PORT}
ENV SENDGRID_API_KEY=${SENDGRID_API_KEY}
ENV JWT_SECRET=${JWT_SECRET}

RUN apt-get -y update && apt-get -y autoremove

RUN mkdir /app
WORKDIR /app

RUN apt-get install -y python python-pip python-dev

ADD . /app
RUN chmod +x build.sh
RUN ./build.sh

# WORKDIR /app/loonax

EXPOSE 8000
CMD ["gunicorn", "--pythonpath", "project", "myapp.wsgi:application", "-b", "0.0.0.0:8000", "--access-logfile", "-"]

The build.sh is simply

#!/usr/bin/env bash
# Exit on error.
set -o errexit

pip install -r requirements.txt

python app/manage.py check --deploy --fail-level ERROR
python app/manage.py collectstatic --no-input
python app/manage.py migrate
1 Like

This is probably because the directory containing gunicorn is not present in the $PATH. I’m not sure where it’s installed, but it might be worth either specifying an absolute path for it or adding its containing directory to your $PATH (probably in the Dockerfile).

Secrets are not available at build time. However, you can use:

fly deploy -e SECRET_KEY=xxx -e ANOTHER_SECRET=zzz

to make the env vars available at build time.

It’s worth noting ARGs are different than ENV and you can set them at build time with:

fly deploy --build-arg JWT_SECRET=xxx

This could be caused by failed deployments rolling back secrets that were set. Is SECRET_KEY present if you fly secrets list?

1 Like

A small correction: fly deploy -e will not make env vars available at build time. You need to use build-arg and ARG in the Dockerfile.

Thanks guys. I am newbie to Dockerifle, just learnt that there are build time envs and run time envs. I have solved the problems for Dockerfile deployment and everything is fine now.

fly deploy --build-arg JWT_SECRET=xxx solves the build time probs.

For the Procfile buildpack deployment, the bash: gunicorn: command not found still persists, not sure why. Thanks anyway.

btw, you guys change my life hehe

Thanks Joshua, your comment did make things 200% clearer for me, I believe, for others Dockerfile newbie like me as well. Thanks. Anyway I mark Jerome as solution.