Home > Software design >  How to include Docker environment arguments when running docker compose?
How to include Docker environment arguments when running docker compose?

Time:10-21

I am new to Docker / docker compose and am using it to deploy an API to a server. I found an image that I wish to use, which includes the option to add some environment variables, in particular:

GUNICORN_CMD_ARGS

Any additional command line settings for Gunicorn can be passed in the GUNICORN_CMD_ARGS environment variable.

Read more about it in the Gunicorn docs: Settings.

These settings will have precedence over the other environment variables and any Gunicorn config file.

For example, if you have a custom TLS/SSL certificate that you want to use, you could copy them to the Docker image or mount them in the container, and set --keyfile and --certfile to the location of the files, for example:

docker run -d -p 80:8080 -e GUNICORN_CMD_ARGS="--keyfile=/secrets/key.pem --certfile=/secrets/cert.pem" -e PORT=443 myimage

I would like to add these two options (keyfile and certfile) as arguments to the docker run command, but instead pass docker compose up to create the images and run the container.

How would I go about doing that?

My Docker file is:

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9

COPY ./requirements.txt /app/requirements.txt

RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt

COPY ./app /app

And my docker-compose.yml file is:

version: "3"

services:

  backend:
    build: ./
    restart: always
    network_mode: "host"

I have tried adding a CMD line to the end of my Docker file but to no avail.

CodePudding user response:

You can use the environment key in you docker-compose.yml like this:

environment:
  GUNICORN_CMD_ARGS: --keyfile=/secrets/key.pem --certfile=...
  PORT: 443

https://docs.docker.com/compose/compose-file/compose-file-v3/#environment

  • Related