Env Vars - Support for container env vars.
CI / Docker Lint (push) Successful in 29s

Now you can configure your container using Environmet vars.
This commit is contained in:
2024-05-12 13:21:28 +02:00
parent 0039b09d98
commit f8365e05af
5 changed files with 48 additions and 2 deletions
+3 -1
View File
@@ -1,4 +1,6 @@
node_modules
build
.dockerignore
Dockerfile
Dockerfile
.env
.env.local
+3
View File
@@ -0,0 +1,3 @@
# App config Variables (Will be visible by client):
VITE_BACKEND_HOST=SOMNIAROOMS_BACKEND_HOST
VITE_BACKEND_PORT=SOMNIAROOMS_BACKEND_PORT
+2
View File
@@ -20,4 +20,6 @@ RUN npm ci && npm run build
FROM nginx:stable-alpine as final-prod
COPY --from=builder /app/dist /usr/share/nginx/html
EXPOSE 80
COPY scripts/env.sh /docker-entrypoint.d/env.sh
RUN chmod +x /docker-entrypoint.d/env.sh
CMD ["nginx", "-g", "daemon off;"]
+28 -1
View File
@@ -30,4 +30,31 @@ docker build -t somniarooms-app:prod --target final-prod .
**Docker Run**
```
docker run -d -it -p 8080:80 --name somniarooms-app somniarooms-app:prod
```
```
**Docker run with env vars:**
```
docker run -d -it -p 8080:80 --name somniarooms-app -e SOMNIAROOMS_BACKEND_HOST=https://somniaapi.example.org/ -e SOMNIAROOMS_BACKEND_PORT=443 somniarooms-app:prod
```
## Env Vars.
As this is distributed as a docker image ready to use, you must set some env variables to configure the app:
| Var | Default Value | Description |
| :------------------------: | :----------------------------: | :------------------------------------------------------------------------------------------: |
| `SOMNIAROOMS_BACKEND_HOST` | https://somniaapi.example.org/ | Host where uses can connect to the SomniaRoom API. API endpoint should be reachble for uses. |
| `SOMNIAROOMS_BACKEND_PORT` | 443 | Port to SomniarRooms API. |
## .env files
This project uses .env files to configure the app. As Vite only supports compile-time vars, we use some tricks to allow setting variables from container env variables.
### .env
Contains default values. It is just an example configuration.
For development is recomended to create a `.env.local` file based on `.env`.
### .env.production
Set varibles to certains placeholder, that are replace on container startup.
(We are using this solution: https://stackoverflow.com/a/77454537)
+12
View File
@@ -0,0 +1,12 @@
#!/bin/sh
for i in $(env | grep SOMNIAROOMS_)
do
key=$(echo $i | cut -d '=' -f 1)
value=$(echo $i | cut -d '=' -f 2-)
echo $key=$value
# sed All files
# find /usr/share/nginx/html -type f -exec sed -i "s|${key}|${value}|g" '{}' +
# sed JS and CSS only
find /usr/share/nginx/html -type f \( -name '*.js' -o -name '*.css' \) -exec sed -i "s|${key}|${value}|g" '{}' +
done