skip to Main Content

I’m deploying my Django app using AWS code pipeline which is dockerized and I was storing my env variables inside an env file for local development but for the code pipeline I set them all inside environment variables but the variables are still getting None.

docker-compose.yml

version: "3.8"

services:
  db:
    container_name: db
    image: "postgres"
    restart: always
    volumes:
      - postgres-data:/var/lib/postgresql/data/

  app:
    container_name: app
    build:
      context: .
    restart: always
    volumes:
      - static-data:/vol/web
    depends_on:
      - db

  proxy:
    container_name: proxy
    build:
      context: ./proxy
    restart: always
    depends_on:
      - app
    ports:
      - 80:8000
    volumes:
      - static-data:/vol/static

volumes:
  postgres-data:
  static-data:

getting env variable in django like:

os.environ.get('FRONTEND_URL')

2

Answers


  1. Chosen as BEST ANSWER

    in my case, I put all env variables inside my AWS CodeBuild environment variables and call them in my docker environment as below.

    version: "3.8"
    
    services:
      db:
        container_name: db
        image: "postgres"
        restart: always
        volumes:
          - postgres-data:/var/lib/postgresql/data/
        environment:
           - VARIABLE_NAME: ${VARIABLE_NAME}
    

  2. You can specify the env file in the docker compose itself along with relative path.

    ....
    ....
    app:
        container_name: app
        build:
          context: .
        restart: always
        env_file:
        - <web-variables1.env>
        volumes:
          - static-data:/vol/web
        depends_on:
          - db
    ....
    ....
    

    Refer more details at documentation

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search