How to pass environment variables to a frontend web application?

后端 未结 5 1303
攒了一身酷
攒了一身酷 2020-12-07 16:15

I am trying to containerize a frontend web application and I am having troubles to figure out how to pass environment variables. The application is a Angular application, so

5条回答
  •  悲&欢浪女
    2020-12-07 16:38

    My solution: at run time use docker volumes to mount a specific js config file as the env.js.

    I have a docker compose file for dev and prod.

    I have dev.env.js and prod.env.js.

    My html file references env.js.

    In docker-compose.yml I volume mount either env file as env.js.

    E.g. my dev compose:

    web:
        image: nginx
        ports:
          - 80:80
        volumes:
          - ../frontend:/usr/share/nginx/html
          - ../frontend/dev.env.js:/usr/share/nginx/html/env.js
    

    And my prod compose:

    web:
        image: nginx
        ports:
          - 80:80
        volumes:
          - ../frontend:/usr/share/nginx/html
          - ../frontend/prod.env.js:/usr/share/nginx/html/env.js
    

提交回复
热议问题