I've got a set up using this tutorial: https://codinglatte.com/posts/angular/using-os-environment-variables-in-angular-with-docker/ to pass docker environment variables into a docker container.
I am using an angular custom webpack for this.
However, when I open my docker container and open the app, it seems my docker variables are not passed into the angular container.
How can I correctly pass docker env variables into my Angular App?
Here is my Dockerfile for the angular application:
FROM node:12 AS compile-image
WORKDIR /opt/ng
COPY package.json ./
RUN npm install
COPY . ./
RUN node_modules/.bin/ng build --prod
FROM nginx
VOLUME [ "/usr/share/nginx/html/data" ]
WORKDIR /
COPY --from=compile-image /opt/ng/dist/YomiApp /usr/share/nginx/html
and my docker-compose.yml that I use to open up the Angular container and the backend server for the app:
version: "3"
services:
webapp:
image: nlanson/myAngularImage:latest
environment:
TZ: Australia/Sydney
DATABASE_URI: "http://localhost:6969"
volumes:
- "./data:/usr/share/nginx/html/data" #Data is mounted to the directory where the Angular App is running.
ports:
- "8080:80"
container_name: webapp
server:
image: nlanson/backendImage:latest
environment:
TZ: Australia/Sydney
volumes:
- "./data:/data"
ports:
- "6969:6969"
container_name: server
Here is my custom-webpack.config.js file where my environment variables are listed:
const webpack = require('webpack');
module.exports = {
plugins: [
new webpack.DefinePlugin({
$ENV: {
DATABASE_URI: JSON.stringify(process.env.DATABASE_URI),
}
})
]
};
and the service that uses the environment variable:
import { environment } from '../../environments/environment'
@Injectable({
providedIn:root
})
export class DatabaseService {
URI = environment.DATABASE_URI
}
Maybe it is because the build for my Angular Image is multi stage??
Thanks