How do I set a env var and run a gunicorn command in a single docker command?

Viewed 284

I'm having difficulty setting an env var prior to running a gunicorn command in docker (1.29). My docker-compose file, which contains my Django/Python startup looks like the below ...

  web:
    restart: always
    build: ./web
    ports:           # to access the container from outside
      - "8000:8000"
    env_file: .env
    environment:
      DEBUG: 'true'
      SERVICE_CREDS_JSON_FILE: '/my-app/credentials.json'
    command: PYTHONPATH=`pwd`/.. 
 /usr/local/bin/gunicorn directory.wsgi:application --reload -w 2 -b :8000

My web/Dockerfile looks like this

FROM python:3.9-slim

RUN apt-get update && apt-get install

RUN apt-get install -y dos2unix
RUN apt-get update \
    && apt-get install -y --no-install-recommends gcc g++ libgdal-dev libffi-dev \
    && rm -rf /var/lib/apt/lists/*

RUN python -m pip install --upgrade pip

WORKDIR /my-app/

COPY . /my-app
RUN python -m pip install -r requirements.txt

RUN dos2unix /my-app/entrypoint.sh

ENTRYPOINT ["bash", "/my-app/entrypoint.sh"]

However, when I run

docker-compose up

I get this error

/my-app/entrypoint.sh: line 9: /app/PYTHONPATH=`pwd`/..: No such file or directory
maps_web_1 exited with code 127

How do I execute setting the env var appropriately as well as running my gunicorn command?

1 Answers

Keeping in mind that command gets appended to the entrypoint, can you please add what you expect final command to be? (https://docs.docker.com/engine/reference/builder/#understand-how-cmd-and-entrypoint-interact)

My guess is that it's

bash /my-app/entrypoint.sh PYTHONPATH=`pwd`/..

When what you are trying to run is:

PYTHONPATH=`pwd` bash /my-app/entrypoint.sh

If this is the case, why not set $PYTHONPATH in the ENV's?

FROM python:3.9-slim

RUN apt-get update && apt-get install

RUN apt-get install -y dos2unix
RUN apt-get update \
    && apt-get install -y --no-install-recommends gcc g++ libgdal-dev libffi-dev \
    && rm -rf /var/lib/apt/lists/*

RUN python -m pip install --upgrade pip
ENV WORKDIR="/my-app/"
ENV PYTHONPATH=$WORKDIR
WORKDIR $WORKDIR

COPY . /my-app
RUN python -m pip install -r requirements.txt

RUN dos2unix /my-app/entrypoint.sh

ENTRYPOINT ["bash -c", "./entrypoint.sh"]

Then you can get rid of the command altogether from your compose file

Related