Environment variables for containerized Vue.js applications and how to set them at runtime

An example Vue.js application

<template>
<div>VUE_APP_MY_VARIABLE: {{my_variable}}</div>
</template>
<script>
export default {
name: 'App',
data: () => ({
my_variable: process.env.VUE_APP_MY_VARIABLE
})
}
</script>

Development environment

VUE_APP_MY_VARIABLE=dev
npm run serve
Our environment variable set correctly for our development environment

Build

VUE_APP_MY_VARIABLE=build
npm run build
The content of the dist directory
http-server ./dist
The environment variable successfully taken the build value

Containerization

FROM node:14 as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY ./ .
RUN npm run build
FROM nginx as production-stage
RUN mkdir /app
COPY --from=build-stage /app/dist /app
COPY nginx.conf /etc/nginx/nginx.conf
user  nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /app;
index index.html;
try_files $uri $uri/ /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
**/node_modules
**/dist
docker build -t my-vue-app .
docker run -e VUE_APP_MY_VARIABLE=run -p 8080:80 my-vue-app
The environment variable did not take the value we specified

Substitution of variables after build

VUE_APP_MY_VARIABLE=VUE_APP_MY_VARIABLE_PLACEHOLDER
Using a temporary placeholder for our variables
#!/bin/sh
ROOT_DIR=./dist
# Replace env vars in files served by NGINX
for file in $ROOT_DIR/js/*.js* $ROOT_DIR/index.html $ROOT_DIR/precache-manifest*.js;
do
sed -i 's|VUE_APP_MY_VARIABLE_PLACEHOLDER|'${VUE_APP_MY_VARIABLE}'|g' $file
# Your other variables here...
done
export VUE_APP_MY_VARIABLE=run
bash ./substitute_environment_variables.sh
The environment variable has successfully taken the value of our environment

Substitution of variables at container runtime

# Build the Vue app
FROM node:14 as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY ./ .
RUN npm run build
# Copy the built app in an NGINX contaier
FROM nginx as production-stage
RUN mkdir /app
COPY --from=build-stage /app/dist /app
COPY nginx.conf /etc/nginx/nginx.conf
# Overriding the default NGINX container behavior
COPY ./substitute_environment_variables.sh /substitute_environment_variables.sh
RUN chmod +x /substitute_environment_variables.sh
ENTRYPOINT ["/substitute_environment_variables.sh"]
#!/bin/sh

ROOT_DIR=/app

# Replace env vars in files served by NGINX
for file in $ROOT_DIR/js/*.js* $ROOT_DIR/index.html $ROOT_DIR/precache-manifest*.js;
do
sed -i 's|VUE_APP_MY_VARIABLE_PLACEHOLDER|'${VUE_APP_MY_VARIABLE}'|g' $file
# Your other variables here...
done
# Starting NGINX
nginx -g 'daemon off;'

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store