<p>After reading <a href="http://docs.gunicorn.org/en/stable/run.html#commands" rel="nofollow noreferrer">the commands chapter</a> and finding <a href="https://github.com/benoitc/gunicorn/issues/1533" rel="nofollow noreferrer">this sample</a>, it looks like that the custom <code>GUNICORN_CMD_ARGS</code> could be omitted by just passing the arguments directly:</p>

<pre><code>command: gunicorn thymedata:app --bind=127.0.0.1:8001 --workers=3
</code></pre>

<p>If one really would like to use variables then it could be done as follows:</p>

<pre><code>command: gunicorn thymedata:app --bind=${GUNICORN_BIND_ADDRESS} --workers=${GUNICORN_WORKERS}
</code></pre>

<p>.env</p>

<pre><code>GUNICORN_BIND_ADDRESS=127.0.0.1:8001
GUNICORN_WORKERS=3
</code></pre>

CC BY-SA 3.0
2017-12-31T09:25:07.353Z
${GUNICORN_BIND_ADDRESS} will only evaluate variables already in the terminal, none that are loaded via `env_file`. Since I do not set any secrets, it's an acceptable solution
Moritz
CC BY-SA 3.0
2017-12-30T16:26:49.033Z
2017-12-30T16:26:49.033Z
030
<p>I had a similar problem and this worked for me without it feeling like a hack...</p>

<p><strong>docker-compose.yml</strong>:</p>

<pre><code>version: '3'
services:
   web:
      build .
      environment:
      - GUNICORN_CMD_ARGS=--workers=0 --bind=0.0.0.0:8000 --timeout=10
</code></pre>

<p><em>Note: add env GUNICORN_CMD_ARGS without quotes</em></p>

<p><strong>Dockerfile</strong>:</p>

<pre><code>FROM python:3.6-slim
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
ENV GUNICORN_CMD_ARGS="--bind=0.0.0.0:8000 --workers=3"
CMD gunicorn app:app
</code></pre>

<p><em>Note: run CMD gunicorn app:app without passing options via array or quotes</em></p>

CC BY-SA 4.0
2018-05-16T23:44:22.320Z
2018-05-16T23:44:22.320Z
Andres
<p>In ran into this problem with my environment file with docker-compose, and I found the problem to be with the quotes.</p>
<p>If I put <code>GUNICORN_CMD_ARGS=--bind=127.0.0.1:8001 --workers=3</code> on the .env file the parameters are accepted without any issue, but if I put quotes around them the variable gets mangled by docker-compose.</p>

CC BY-SA 4.0
2020-09-22T20:43:40.223Z
2020-09-22T20:43:40.223Z
Andrés Javier López
<p>Context: docker-compose to start several containers, including Gunicorn that calls a Flask app. I'm using an environment file <code>web/env.gunicorn</code> to store my Gunicorn startup configuration. This file contains</p>

<pre><code>GUNICORN_CMD_ARGS="--bind=127.0.0.1:8001 --workers=3"
</code></pre>

<p>The problem is that <code>GUNICORN_CMD_ARGS</code> is not processed correctly somewhere in the pipeline. The error that I get when running <code>docker logs gunicorn</code> is</p>

<pre><code>Error: '8001 --workers=3' is not a valid port number.
</code></pre>

<p>The question is, where in my setup are my assumptions wrong, thereby causing the environment variable to be mangled? The environment variable is accepted when run manually in a terminal. The docker compose file looks as follows</p>

<pre><code>version: "3"
services:
#  nginx:
#    image: nginx:latest
#    ports:
#      - "80:80"
#    volumes:
#      - ./nginx:/etc/nginx/conf.d
#    depends_on:
#      - web

  web:
    build: ./web
    container_name: gunicorn
    ports:
      - "8001:8001"
    environment:
      - APP_CONFIG_FILE=../config/development.py
    env_file:
      - 'web/env.gunicorn'
    networks:
      - backend
    command: gunicorn thymedata:app
    depends_on:
      - influxdb
      - grafana

  influxdb:
    image: influxdb:latest
    container_name: influxdb
    ports:
      - "8086:8086"
    env_file:
      - 'influxdb/env.influxdb'
      - 'influxdb/secrets.influxdb'
    networks:
      - backend
    volumes:
      - influxdb-data:/var/lib/influxdb

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    ports:
      - "3000:3000"
    env_file:
      - 'grafana/env.grafana'
      - 'grafana/secrets.grafana'
    networks:
      - backend
    volumes:
      - grafana-data:/var/lib/grafana
    depends_on:
      - influxdb

networks:
  backend:

volumes:
  influxdb-data:
  grafana-data:
</code></pre>

CC BY-SA 3.0
2017-12-29T21:33:45.453Z
Not using gunicorn, but reading the doc there's no = sign out of the note at about the env variable. I'd try with a space, but it sounds like it doesn't parse properly multiples arguments and as such is maybe a bug in gunicorn (inverting the arguments order to end by the bind argument may be a workaround or exhibit the split problem with the workers parameter)
Tensibai
CC BY-SA 3.0
2017-12-30T06:13:03.360Z
I tried changing the ordering of the parameters, but that did not help. I also tried starting Gunicorn from the command line with `GUNICORN_CMD_ARGS` set, and it accepted it properly (# of workers and address checked). That's why i suppose the problem being on Docker's side.
Moritz
CC BY-SA 3.0
2017-12-30T16:11:50.227Z
`ERROR: build path /home/ben/devopsStackExchange/web either does not exist, is not accessible, or is not a valid URL.`. Please create a `steps to reproduce paragraph` and add the required files so that other people could help you by at least allowing them to reproduce the issue.
030
CC BY-SA 3.0
2017-12-30T16:33:51.790Z
Please add at least the Gunicorn Dockerfile.
030
CC BY-SA 3.0
2017-12-31T09:23:12.543Z
I don't use a Gunicorn Dockerfile. I'll stick to the `command: gunicorn thymedata:app --bind=127.0.0.1:8001 --workers=3` version, since the variables loaded via `env_file` are not accepted. They have to already be set in the calling terminal.
Moritz
<docker><docker-compose>
Why is environment variable mangled in docker compose environment file
Moritz
# Why is environment variable mangled in docker compose environment file

(Asked by: username_0 on 2017-12-28)

Context: docker-compose to start several containers, including Gunicorn that calls a Flask app. I'm using an environment file web/env.gunicorn to store my Gunicorn startup configuration. This file contains
GUNICORN_CMD_ARGS="--bind=127.0.0.1:8001 --workers=3"

The problem is that GUNICORN_CMD_ARGS is not processed correctly somewhere in the pipeline. The error that I get when running docker logs gunicorn is
Error: '8001 --workers=3' is not a valid port number.

The question is, where in my setup are my assumptions wrong, thereby causing the environment variable to be mangled? The environment variable is accepted when run manually in a terminal. The docker compose file looks as follows
version: "3"
services:
#  nginx:
#    image: nginx:latest
#    ports:
#      - "80:80"
#    volumes:
#      - ./nginx:/etc/nginx/conf.d
#    depends_on:
#      - web

  web:
    build: ./web
    container_name: gunicorn
    ports:
      - "8001:8001"
    environment:
      - APP_CONFIG_FILE=../config/development.py
    env_file:
      - 'web/env.gunicorn'
    networks:
      - backend
    command: gunicorn thymedata:app
    depends_on:
      - influxdb
      - grafana

  influxdb:
    image: influxdb:latest
    container_name: influxdb
    ports:
      - "8086:8086"
    env_file:
      - 'influxdb/env.influxdb'
      - 'influxdb/secrets.influxdb'
    networks:
      - backend
    volumes:
      - influxdb-data:/var/lib/influxdb

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    ports:
      - "3000:3000"
    env_file:
      - 'grafana/env.grafana'
      - 'grafana/secrets.grafana'
    networks:
      - backend
    volumes:
      - grafana-data:/var/lib/grafana
    depends_on:
      - influxdb

networks:
  backend:

volumes:
  influxdb-data:
  grafana-data:

**Tags:** docker, docker-compose

> **Comments:**
> **username_1**: Not using gunicorn, but reading the doc there's no = sign out of the note at about the env variable. I'd try with a space, but it sounds like it doesn't parse properly multiples arguments and as such is maybe a bug in gunicorn (inverting the arguments order to end by the bind argument may be a workaround or exhibit the split problem with the workers parameter)  
> **username_0**: I tried changing the ordering of the parameters, but that did not help. I also tried starting Gunicorn from the command line with `GUNICORN_CMD_ARGS` set, and it accepted it properly (# of workers and address checked). That's why i suppose the problem being on Docker's side.  
> **username_2**: `ERROR: build path /home/ben/devopsStackExchange/web either does not exist, is not accessible, or is not a valid URL.`. Please create a `steps to reproduce paragraph` and add the required files so that other people could help you by at least allowing them to reproduce the issue.  
> **username_2**: Please add at least the Gunicorn Dockerfile.  
> **username_0**: I don't use a Gunicorn Dockerfile. I'll stick to the `command: gunicorn thymedata:app --bind=127.0.0.1:8001 --workers=3` version, since the variables loaded via `env_file` are not accepted. They have to already be set in the calling terminal.  

---

## 3 answers

### Answer by username_2 on 2017-12-30. Score: 2 | Accepted answer

After reading the commands chapter and finding this sample, it looks like that the custom GUNICORN_CMD_ARGS could be omitted by just passing the arguments directly:
command: gunicorn thymedata:app --bind=127.0.0.1:8001 --workers=3

If one really would like to use variables then it could be done as follows:
command: gunicorn thymedata:app --bind=${GUNICORN_BIND_ADDRESS} --workers=${GUNICORN_WORKERS}

.env
GUNICORN_BIND_ADDRESS=127.0.0.1:8001
GUNICORN_WORKERS=3

> **Comments:**
> **username_0**: ${GUNICORN_BIND_ADDRESS} will only evaluate variables already in the terminal, none that are loaded via `env_file`. Since I do not set any secrets, it's an acceptable solution  

---

### Answer by username_3 on 2018-05-16. Score: 3

I had a similar problem and this worked for me without it feeling like a hack...
docker-compose.yml:
version: '3'
services:
   web:
      build .
      environment:
      - GUNICORN_CMD_ARGS=--workers=0 --bind=0.0.0.0:8000 --timeout=10

Note: add env GUNICORN_CMD_ARGS without quotes
Dockerfile:
FROM python:3.6-slim
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
ENV GUNICORN_CMD_ARGS="--bind=0.0.0.0:8000 --workers=3"
CMD gunicorn app:app

Note: run CMD gunicorn app:app without passing options via array or quotes


---

### Answer by username_4 on 2020-09-22. Score: 0

In ran into this problem with my environment file with docker-compose, and I found the problem to be with the quotes.

If I put `GUNICORN_CMD_ARGS=--bind=127.0.0.1:8001 --workers=3` on the .env file the parameters are accepted without any issue, but if I put quotes around them the variable gets mangled by docker-compose.


---

