1

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

3
  • I think your confusion is around ARG and assuming that it populates environment at runtime. ARG only populates environment variables at build time and is used to pass variables to container steps while building the image. The .env file is only going to override ENV in the container or the environment map in the compose file. Commented Aug 29, 2020 at 16:07
  • Mhh it is less about ARG and more about ENV. When I pass values in a .env file I still have to pass the key:ARG value:ENV to the dockerfile to access the env variables later in my container. Which was pretty confusing. I thought ENV variables when set in the docker-compose can be used inside my finished container no matter what I am doing. But ENV variables inside docker-compose only seem to be usable inside docker-compose. Commented Aug 29, 2020 at 23:48
  • They can. But in your example you expected output that depends on DB_NAME and DB_USER which are build time ARG and will always be blank at run time. Commented Aug 30, 2020 at 22:36

1 Answer 1

3

The .env is not automatically passed to the container, you need to declare it in your docker-compose.yml using env_file: Explanation here. The environment inside the dockerfile should be overriden by the ones in your docker-compose file not sure why this is not the case.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh wow. They are really making a difference between .env and env_file ? :D I will check that out later and will give you the top answer if that works. I did a workaround assigning ARGS to the build and passing them the values of my .env variables inside the docker-compose. Which felt insanely stupid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.