docker does really confuse me.... I am trying to use the "environments" variable as well as the .env file to pass variables into the container that will be created. Sadly without success.
What my setup is:
docker-compose.yml
# dockerfile version 3.8 -> 18.07.2020
version: "3.8"
# services basicly means scalable amount of containers
services:
testalpine:
image: testenvalpine
build:
context: .
dockerfile: test-dockerfile
args:
- DB_NAME=nextcloud
- DB_USER=nextcloud
# todo only use https and self sign certificate
ports:
- "80:80"
environment:
- env1=hello
- env2=world
networks:
- nextcloudnetwork
# todo include redis and mariadb
networks:
nextcloudnetwork:
# std driver seems to be overlay?
driver: overlay
Dockerfile: test-dockerfile
FROM alpine:latest
LABEL maintainer="xddq <donthavemyownemailyet:(((>"
ARG DB_NAME=default
ARG DB_USER=default
ENV env1=dockerfile env2=$DB_NAME
ENTRYPOINT [ "sh", "-c", \
"echo DB_NAME: $DB_NAME DB_USER: $DB_USER env1: $env1 env2: $env2" ]
My .env file
DB_NAME=nextcloud
DB_USER=nextcloud
The output I did EXPECT:
DB_NAME:nextcloud DB_USER: nextcloud env1: hello env2:nextcloud
The output I got:
DB_NAME: DB_USER: env1: dockerfile env2: nextcloud
Does this mean ".env" and ENV variable in docker-compose are completely useless for the env variables inside the container that will be created? I mean I could only get any result using the ARG variable..? :/
greetings
ARGand assuming that it populates environment at runtime.ARGonly populates environment variables at build time and is used to pass variables to container steps while building the image. The.envfile is only going to overrideENVin the container or theenvironmentmap in the compose file.DB_NAMEandDB_USERwhich are build timeARGand will always be blank at run time.