How to set environment variables in a docker container after it starts - docker

I need to set some environment variables in a docker container after it starts. When the docker starts env X gets value, then I want to set env Y with a value which is the first part of the value X with this command:
Y=$(echo $X | cut -d'#' -f 1)
Is there any way to do this?
I tried ENTRYPOINT and CMD in the Dockerfile, but it doesn't work.
The docker will be deployed on a Kubernetes cluster, and I also tried to set them in the config.yaml file but it doesn't work either.

You are on the right track that you would have to handle this by either CMD or ENTRYPOINT, because you want it to be dynamic and derived from existing data. The specifics would depend on your container and use case though.

You can use the ENV command in your dockerfile like below:
ENV PORT 8080
Source and more info - https://vsupalov.com/docker-build-time-env-values/

Related

disable env variable substitution in docker compose env files

I updated docker and notice a significant change of behavior when using my env files in docker-compose
I have the following:
env_file:
- ./file.env
and, in file.env
property=${something}
Before updating, doing echo $property in my running container would produce the string ${something}. Which was what I wanted.
Now (docker 19.03.12), $property is empty because docker is trying to find $something from my system env variables.
What caused this change of behavior? Is there a way to disable it or work around it?

How to create dynamic env variable in Dockerfile?

I am using Docker composé ver 1.5 or 6 and nginx image. I want to parameterize the nginx.config. To do that I want to create a var from $(basename some path). But the problem is Docker does not accept dynamic vars like that in env part of the Dockerfile. Another problem is that I also cannot map those variables on build run as Docker compose does not accept dynamic, scripted vars. How to overcome that issue?
From nginx
ENV myvar=$(basename /)
This is impossible to build the image.
The other way was
ARG myvar
ENV myvar2=myvar
But my version of Docker compose allows only to set
Environment: myvar=$(basename mypathinthevolume/)
That also does not seem to work

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.

ECS Container Environment Configuration

I have a recently-Dockerized web app that I would like to get running on AWS ECS, and a few fundamental concepts (which I don't see explained in the AWS docs) are throwing me off.
First, when you Edit/configure a new container, it asks you to specify the image to use, but then also has an Environment section:
The Entry point, Command and Working directory fields look suspiciously similar to the commands I already specified when creating my Docker image (here's my Dockerfile):
FROM openjdk:8
RUN mkdir /opt/myapp
ADD build/libs/myapp.jar /opt/myapp
WORKDIR /opt/myapp
EXPOSE 9200
ENTRYPOINT ["java", "-Dspring.config=.", "-jar", "myapp.jar"]
So if ECS is asking me for an image (that's already been built using this Dockerfile), why in tarnation do I need to re-specify the exact same values for WORKDIR, EXPOSE, ENTRYPOINT, CMD, etc.?!?
Also outside of ECS I run my container like so:
docker run -it -p 9200:9200 -d --net="host" --env-file ~/myapp-local.env --name myapp myapp
Notice how I specify the env file? Does ECS support env files, or do I really have to enter each and every env var from my env file into this UI here?
Also I see there is a Docker Labels section near the bottom:
Are these different than env vars, or are they interchangeable?
Yes you need to add environment variable either through UI or through CLI .
For CLI you need to pass it as JSON template .
Also if you have already specified these values in Dockerfile then you dont need to pass these values again.
All the values that will be passed externally will overwrite internal/default values in Dockerfile

Pass local environment variable to docker

This might be a general docker problem but my use-case is docker-compose:
I need to pass my machine's hostname as an environment variable to a container which would be created through docker-compose. That particular container uses a Dockerfile and uses "cmd" flag in it. The command in "cmd" uses this environment variable. Can someone please help?
If you can rewrite the Dockerfile in order to include the hostname in an ENV instruction, as mentioned in issue 1136, that will work:
FROM centos:latest
ENV BLABLA hello
RUN echo $BLABLA
# outputs: "hello"
If this is purely a runtime environment variable value, it should be included in docker 1.9 with issue 14634: Builder - Build-time argument passing (e.g., HTTP_PROXY):
In the meantime, the OP 208rishabh mentions issue 2091 for docker compose, answered by dnephin:
environment or env_file should both work:
You can leave the value blank, and it will take the value from the current environment:
environment:
HOST_HOSTNAME:
That allows:
HOST_HOSTNAME=$(hostname) docker-compose up

Resources