skip to Main Content

There are multiple parts of Compose that deal with environment variables in one sense or another. So how do I pass Environment variables in Compose ( docker-compose )

According to the documentation If you have multiple environment variables, you can substitute them by adding them to a default environment variable file named .env or by providing a path to your environment variables file using the –env-file command line option.

version: '3.9'
services:
  nginx:
    image: nginx:stable-alpine
    container_name: nginx
    ports:
      - 80:80
      - 443:443
    restart: always
    volumes:
        - ./nginx/nginx.conf:/etc/nginx/nginx.conf
  postgres:
    container_name: postgres
    image: postgres:13-alpine
    environment:
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASSWORD}
      - POSTGRES_DB=${DB_DATABASE}
    volumes:
      - ./pgdata:/var/lib/postgresql/data
      - ./database/app.sql:/docker-entrypoint-initdb.d/app.sql
    restart: always
    ports:
      - "35000:5432"
    networks:
      - app_network

  app-api:
    container_name: app-api
    build:
      dockerfile: Dockerfile
      context: ./app-api
      target: production
    environment:
      - DB_TYPE=${DATABASE_TYPE}
      - POSTGRES_HOST=${DB_HOST}
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASS=${DB_PASSWORD}
      - POSTGRES_DB=${DB_DATABASE}
      - POSTGRES_PORT=${DB_PORT}
      - APP_PORT=${SERVER_PORT}
      - NODE_ENV:production

      ## AWS
      - AWS_S3_ACCESS_KEY=${AWS_S3_ACCESS_KEY}
      - AWS_S3_SECRET_ACCESS_KEY=${AWS_S3_SECRET_ACCESS_KEY}
      - AWS_S3_BUCKET=${AWS_S3_BUCKET}
      - AWS_S3_REGION=${AWS_S3_REGION}
    ports:
      - "5050:80"
    volumes: 
      - ./pgadmin-data:/var/lib/pgadmin
    depends_on: 
      - postgres
    links:
      - postgres
    networks:
      - app_network

  pgadmin:
    container_name: pgadmin
    image: dpage/pgadmin4
    restart: always
    environment:
      - PGADMIN_DEFAULT_EMAIL=${PGADMIN_DEFAULT_EMAIL}
      - PGADMIN_DEFAULT_PASSWORD=${PGADMIN_DEFAULT_PASSWORD}
      - PGADMIN_LISTEN_PORT=${PGADMIN_LISTEN_PORT}
    restart: always
    ports:
      - "5400:5400"
    depends_on:
      - postgres
    links:
      - postgres
    networks:
      - app_network

2

Answers


  1. Chosen as BEST ANSWER

    When you run docker-compose up, the web service defined above uses the image from the defined Dockerfile. You can verify this with the convert command, which prints your resolved application config to the terminal:

    You can use this command to verify if you are pathing the proper environment variables

      $ docker compose convert
    

    https://docs.docker.com/compose/environment-variables/


  2. you can use it like this. you have to pass value in each environment variable.

    server:
        environment:
          - AWS_S3_ACCESS_KEY=ABCJQHWEQJHWQ
          - AWS_S3_SECRET_ACCESS_KEY=ASKJHDAKJHNAWKLHEN
          - AWS_S3_BUCKET=abc-text
          - AWS_S3_REGION=eu-west-1
        ports:
          - "5050:80"
        volumes: 
          - ./pgadmin-data:/var/lib/pgadmin
        depends_on: 
          - postgres
        links:
          - postgres
        networks:
          - app_network
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search