How to set environmental variables properly Gitlab CI/CD and Docker

In my experience the best way to pass environment variables to a container docker is creating an environment file, which works to development environment and production environment:

GitLab CI/CD variables

You must create an environment file on GitLab CI/CD, following the next steps, on your project GitLab you must go to:

settings > CI/CD > Variables

on that you must create a ENV_FILE

Demo image

enter image description here

Next on your build stage in .gitlab-ci.yml copy the ENV_FILE to local process

.gitlab-ci.yml

build:
  stage: build
  script:
    - cp $ENV_FILE .env
    - echo "Building the app"
    - docker-compose build

your Dockerfile should be stay normally so it doesn't have to change

Dockerfile

FROM python:3.8.6-slim

# Rest of setup goes here...

COPY .env .env

The SECRET_KEY variable will be available to all your CI jobs, as configured. However, I don't see any references to it in your Docker Compose file to pass it to one or more of your services. For the web service to use it, you'd map it in like the other variables you already have.

  web:
    build: .
    command: /usr/local/bin/gunicorn writer.wsgi:application -w 2 -b :8000
    environment:
      SECRET_KEY: ${SECRET_KEY}
      DEBUG: ${DEBUG}
      …

As for creating the database, you should wrap up whatever you currently run interactively in the postgres container in a SQL file or shell script, and then bind-mount it into the container's initialization scripts directory under /docker-entrypoint-initdb.d. See the Initialization scripts section of the postgres image's description for more details.