Accessing environment variables from docker compose in Quasar app - docker

I am wondering how should I serve and deploy Quasar app in docker on different environments. Env is injected to App during the build so if I use docker-compose on differents environment it will fail to change env inside app.
For now I found solution to create function to add my docker-compose env to my index.html area and then parse it by another function to variables in program.
Is there more elegant way to do this? For example to create one env file with variables to all environment and in docker-compose just point which sets of variables should be used on that environment? (Ofc everything should work on docker-compose, I want to build only once and deploy to many environments).

Related

Docker compose ¿caching? env variables

I'm experiencing very strange behaviour with docker-compose. I have a repository configured to work with docker swarm for production and docker-compose for development. Swarm is working OK in production, but docker-compose is having strange behaviour.
Specifically, I'm defining build arguments with parameter substitution, like this
build:
context: .
args:
- APP_DIRECTORY=${APP_DIRECTORY:-/srv/app}
- APP_ENV=${APP_ENV:-dev}
When APP_ENV is not defined or is empty, it should take the value dev. This was working fine, but now it's taking the value prod when the variable is not defined. I rebooted, cleared all envrionment variables, even removed docker-compose and installed it again, and APP_ENV is still being assinged prod. Is there some sort of caching done by compose that I'm not being aware of?
Another strange behaviuor, is that docker-compose is passing proxy-related envionment variables to the container. Those variables are not specified on the compose file, and they are not even present on the host. Again, is there some soer of caching taking place? And why is docker-compose passing env variables that I didn't ask for to the container?
I was making a stupding mistake, I had a .env file in the same directory, and docker-compose was reading the variables from the file.

Dockerized Vue SPA with different API urls depending on where it is running

I got this docker image of my Vue.js app that fetches data from an api running in a java backend. In production the api is running under app.example.com/api, in staging it will run under staging.example.com/api and when running it on my local computer the api will be running at localhost:8081. When running the frontend on my computer I might be using vue cli serve in the project folder, or it might be started as a docker image using docker-compose. The backend is always running as a docker image.
I would like to be able to use the same docker image for local docker-compose, deploy to staging and deploy to production, but using a different url to the backend api. As a bonus it would be nice to be able to use vue-cli serve.
How can this be achieved?
You can use an environment variable containing the API url and then use the environment variable in your Vue app.
The Vue cli supports environment variables and allows you to use environment variables that start with VUE_APP_ in your client-side code. So if you create an environment variable called VUE_APP_API_URL in the environment you're running the Vue CLI in (whether it is Docker or on your host machine) you should be able to use process.env.VUE_APP_API_URL in your Vue code.
If you're running Vue CLI locally, you can just run export VUE_APP_API_URL="localhost:8081" before running vue cli serve.
Docker also supports environment variables. For example, if your SPA Docker service is called "frontend", you can add an environment variable to your Docker Compose file like this:
frontend:
environment:
- VUE_APP_API_URL
If you have the VUE_APP_API_URL environment variable set in the host you're running Docker from it will be passed on to your "frontend" Docker container. So, for example, if you're running it locally your can run export VUE_APP_API_URL="localhost:8081" before running Docker Compose.
You can also pass through environment variables using an .env file. You can read more about environment variables in Docker Compose files here if you're interested.
you can create .env files
env file for development, production ...
check this out for better details:
https://dev.to/ratracegrad/how-to-use-environment-variables-in-vue-js-4ko7

Define environment variable in Dockerfile or docker-compose?

After reading the config point of the 12 factor app I decided to override my config file containing default value with environment variable.
I have 3 Dockerfiles, one for an API, one for a front-end and one for a worker. I have one docker-compose.yml to run those 3 services plus a database.
Now I'm wondering if I should define the environment variables in Dockerfiles or docker-compose.yml ? What's the difference between using one rather than another ?
See this:
You can set environment variables in a service’s containers with the 'environment' key, just like with docker run -e VARIABLE=VALUE ...
Also, you can use ENV in dockerfile to define a environment variable.
The difference is:
Environment variable define in Dockerfile will not only used in docker build, it will also persist into container. This means if you did not set -e when docker run, it will still have environment variable same as defined in Dockerfile.
While environment variable define in docker-compose.yaml just used for docker run.
Maybe next example could make you understand more clear:
Dockerfile:
FROM alpine
ENV http_proxy http://123
docker-compose.yaml:
app:
environment:
- http_proxy=http://123
If you define environment variable in Dockerfile, all containers used this image will also has the http_proxy as http://123. But the real situation maybe when you build the image, you need this proxy. But, the container maybe run by other people maybe not need this proxy or just have another http_proxy, so they had to remove the http_proxy in entrypoint or just change to another value in docker-compose.yaml.
If you define environment variable in docker-compose.yaml, then user could just choose his own http_proxy when do docker-compose up, http_proxy will not be set if user did not configure it docker-compose.yaml.

Docker use variables in conf file?

If I'm using Docker with nginx for hosting a web app, how can I use either
Variables in my docker-compose.yml file
Environment variables such as HOSTNAME=example.com.
So that when I build the container, it will insert the value into my nginx.conf file that I copy over when I build the container.
You can use environment variables is your compose file. According to official docs
Your configuration options can contain environment variables. Compose uses the variable values from the shell environment in which docker-compose is run. For example, suppose the shell contains POSTGRES_VERSION=9.3 and you supply this configuration:
db: image: "postgres:${POSTGRES_VERSION}"
When you run docker-compose up with this configuration, Compose looks for the POSTGRES_VERSION environment variable in the shell and substitutes its value in.
See the docs for more information. You will find various other approaches to supply environment variables in the link like passing them through env_file etc.

Docker Compose: Change env variables

I have writte a docker-compose.yml file to create a multi-container app with nodejs and mongodb (so 2 containers), and I want to make some options configurable, as the choice of server address and port.
To do that, I have written what follows in docker-compose.yml to set them as env variables:
..
web:
environment:
- PORT=3000
- ADDRESS=xxx.xxx.xxx.xxx
..
Within the source code of my application, I use process.env.PORT and process.env.ADDRESS to refer to these variables.
But how can I do if I want to change those values, and for example set PORT=3001?
I have to use docker-compose build and docker-compose up again and build all the application, included mongodb container?
I have to use docker-compose build and docker-compose up again and build all the application, included mongodb container?
Not build, just up. They are runtime options, not build options. Changing them in the docker-compose.yml file and then doing a docker-compose up again should recreate the containers with the new environment variables.
Alternatively, you can specify environment variables outside the docker-compose.yml file to make for easier changes:
One of these methods is to use a .env file in the same folder that you execute docker-compose form (see https://docs.docker.com/compose/env-file/ for information on it).
Another option is to interpolate environment variables from your shell. You could specify them such as - PORT=${APP_PORT} and then export APP_PORT=3000 on your shell before running docker-compose. See https://docs.docker.com/compose/environment-variables/ for more information.

Resources