kubernetes set root environmental variables - ruby-on-rails

I have a rails app that runs apache2 as root with database.yml config values set by environmental variables passed in via a kubernetes configmap.
However, since apache2 is a root process, it doesn't have the passed in environmental values. How do I set the environmental values for root from kubernetes configmap?

since apache2 is a root process, it doesn't have the passed in environmental values.
If Use ConfigMap-defined environment variables is not possible, you could add ConfigMap data to a Volume, which then can be read by a wrapper to the apache2 runner.
That wrapper can:
read the values in the config-map-based volume
set the right environment variables
launch Apache2

Related

Find source of container environment variable manager by portainer

I'm new to portainer. I have only set up a couple of services in the last week, but until now it worked quite well. However, now I'm facing a problem whose origin I just don't understand currently:
I set up a docker compose service which is pulled from a Github repository. However, somehow in the container appears an env var value which is never set. The env vars in question are VIRTUAL_HOST, which seems to be set to portainer, and VIRTUAL_PORT, which is set to 9443.
In the repository, the value portainer does not exist, the env vars however are declared as follows:
environment:
- VIRTUAL_HOST
- VIRTUAL_PORT
In the portainer stack I loaded a .env file with other environment variables and added the VIRTUAL_HOST and VIRTUAL_PORT manually, both to other values than then appear in the container.
When I open the container view in portainer, it actually show the wrong values. If I replace them forcefully in the container edit view, it will update in the container and everything works. If I remove and restart all containers, the wrong values are there again.
So the point is: where else can I expect the environment values to be injected from?

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.

set_by_lua & os.getenv within openresty:alpine docker container

I'm running openresty nginx within official alpine-fat docker image, and openresty process starts with nobody user.
I need to set nginx variable with the next string:
set_by_lua $var 'return os.getenv("ENV_VAR")';
docker-compose.yml contains the next block:
build:
context: .
dockerfile: ./Dockerfile.nginx
environment:
- ENV_VAR=value
But, nginx worker process seems not getting its value, and $var remains empty.
I tried to add export ENV_VAR=value to /etc/profile file, but no use.
I tried to run openresty with nginx user, but it also can't see the value of ENV_VAR variable.
How can I make that thing work, if I can?
Try adding env ENV_VAR; to your nginx config. By default nginx will discard all environment variables, this will allow to save it.
From https://nginx.org/en/docs/ngx_core_module.html#env
Syntax: env variable[=value];
Default:
env TZ;
Context: main
By default, nginx removes all environment variables inherited from its parent process except the TZ variable. This directive allows preserving some of the inherited variables, changing their values, or creating new environment variables.

Environment variable and docker-compose

When running a number of linked services with docker-compose up, how can I add values for environment variables to be passed to specific containers for services? Say I have a service "webapp" which uses the TIMEOUT environment variable, how do I set TIMEOUT=10000 with docker-compose for the container that "webapp" lives in? I have tried the notation -e TIMEOUT=10000 from the docker command, but this does not seem to work.
with docker compose your have to specify environment variables in docker-compose.yml with env_file or environment configuration commands
https://docs.docker.com/compose/compose-file/#env-file
https://docs.docker.com/compose/compose-file/#environment

Resources