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
I had a similar problem for a static HTML file and here is what I wanted to solve:
I tried other answers but it seems like they didn't fit the above. So this is what I ended up with using envsubst
Dockerfile
FROM nginx:alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY . /usr/share/nginx/html
EXPOSE 80
# awkwardly replace env variables
COPY ./replaceEnvVars.sh /
RUN chmod +x replaceEnvVars.sh
ENTRYPOINT ["./replaceEnvVars.sh"]
CMD ["nginx", "-g", "daemon off;"]
replaceEnvVars.sh
#!/bin/sh
envsubst < /usr/share/nginx/html/index.tmpl.html > /usr/share/nginx/html/index.html && nginx -g 'daemon off;' || cat /usr/share/nginx/html/index.html
index.tmpl.html
...
...
Login
docker-compose.yml
version: '3'
services:
landing:
build: .
...
environment:
- BASE_URL=https://dev.example.com
- GA_CODE=UA-12345678-9
...