I have some environment variables which I want to use in both docker-compose configurations and Dockerfile. I have an environment file named .env in order to make use of them in docker-compose file (a solution that I used for this issue); but these environment variables are not reachable in node's Dockerfile. Consider JMX_PORT, WS_PORT and IP as variables which are defined in my .env file; I put an echo for each of them in my Dockerfile to check if they are passed to it or not.
I tried to put .env in the env_file section of the node:
version: "2"
services:
mynode:
build:
context: .
dockerfile: Dockerfile-mynode
env_file:
- .env
ports:
- "${JMX_PORT}:${JMX_PORT}"
- "${WS_PORT}:${WS_PORT}"
I also tried:
version: "2"
services:
mynode:
build:
context: .
dockerfile: Dockerfile-mynode
environment:
- IP=${IP}
- JMX_PORT=${JMX_PORT}
- WS_PORT=${WS_PORT}
ports:
- "${JMX_PORT}:${JMX_PORT}"
- "${WS_PORT}:${WS_PORT}"
And with a little change:
version: "2"
services:
mynode:
build:
context: .
dockerfile: Dockerfile-mynode
environment:
- IP
- JMX_PORT
- WS_PORT
ports:
- "${JMX_PORT}:${JMX_PORT}"
- "${WS_PORT}:${WS_PORT}"
None of the above approaches worked.
I don't want to redefine them in another file. Is there any solution for that?
EDIT 1:
This is my Dockerfile:
FROM mybaseimage
MAINTAINER Zeinab Abbasimazar
RUN echo ${IP}
RUN echo ${JMX_PORT}
RUN echo ${WS_PORT}
EDIT 2:
This is the output of docker-compose config command which implies the docker-compose has access to the environment variables as expected:
services:
mynode:
build:
context: /home/zeinab/Workspace/ZiZi-Docker/Test/test-1
dockerfile: Dockerfile-WRCore
environment:
IP: 171.80.80.80
JMX_PORT: '1998'
WS_PORT: '9000'
ports:
- 1998:1998
- 9000:9000
version: '2.0'
volumes: {}
And this is the output of docker-compose build --no-cache command which implies the Dockerfile does not have access to the environment variables as expected:
docker-compose build --no-cache
Building mynode
Step 1 : FROM mybaseimage
---> 875978d91e2e
Step 2 : MAINTAINER Zeinab Abbasimazar
---> Running in 250eac87d078
---> 0e7bd6a18c80
Removing intermediate container 250eac87d078
Step 3 : RUN echo ${IP}
---> Running in 4ee6dbf74b2a
---> fd2bff1de6b6
Removing intermediate container 4ee6dbf74b2a
Step 4 : RUN echo ${JMX_PORT}
---> Running in 01245df43926
---> 68b7e90b1c01
Removing intermediate container 01245df43926
Step 5 : RUN echo ${WS_PORT}
---> Running in d27db926ecac
---> a8c3d0cbade8
Removing intermediate container d27db926ecac
Successfully built a8c3d0cbade8