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
Related
This is quite silly, but I can't successfully pass my environment vars into my Next.js service (run with docker-compose up). Anyone can see the bug?
docker-compose.yml
services:
...
nextjs-client:
image: nextjs-client
ports: "3000:3000"
environment:
- NEXT_PUBLIC_API_HOST=192.168.0.9:8080
At my nextjs-client sourcecode I try to access it with process.env.NEXT_PUBLIC_API_HOST, but it's undefined.
Your syntax looks fine,
Try to exec the container and printenv to see if the variable exists.
No need to say, your code should run in the same container, of course.
If it exists, maybe a spelling issue. Check also the process.env declaration vs the docker environment declaration.
Try also to docker-compose down to remove the container, might be also a caching issue.
It might be easier to maintain the environment variables with .env file and docker compose --env-file .env, but it is not the problem, just a tip.
How to using env variables in Dockerfile?
not args
This is my Dockerfile now:
FROM node:12
I want Dockerfile:
FROM node:$version
How can I do it? If it is not possible, then I will create a feature request for Docker.
You need to add as ARGs under a different place from the ENV variables. You can use them in the compose, just a little differently.
ARGs will be used during the build only, if you need them to available for the build & again while running the container for some reason you will have to set both an ARG as well as an ENV variable. {{{During the Build stage they will be called in the exact same manner}}}. ENV are set to replace defaults when running a container, so the build's ENV TZ=Europe/London will be replaced by the Compose's Environment: TZ=America/New_York
services:
my_node_version:
image: my/node
build:
context: .
dockerfile: ../node.Dockerfile
args:
- VERSION=12
- NEEDED_IN_BUILD_AND_RUN=SomeStrangeUseCase
environment:
- ONLY_USED_IN_IMAGE=MyImageWorks
- NEEDED_IN_BUILD_AND_RUN=SomeStrangeUseCase
- OVERRIDE_BUILD_ENV="Will replace the ENV from the Build"
I'm pretty sure that fulfills all the intent from the question (you asked to not use args I assume because you wanted them set in the docker-compose.yaml rather than the dockerfile). You can do this, you just have to set them in a different way.
ENV cannot be used in the build from the Compose (ENV variables set in the Dockerfile just set the defaults that the container will use if none are declared in the OS, the Compose, or anywhere Docker gets them from), so that simply isn't a possibility, so if you need it to be ENV specifically for some reason then the answer is simply "You can't" end of discussion. But I don't see anything that could possibly accomplish, & no reason using ARGs wouldn't accomplish exactly what you want. You just need to place them in a different section, but they should function as needed
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?
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
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.