xddq
xddq

Reputation: 381

docker-compose passing environment variables into container possible?

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

Upvotes: 1

Views: 2717

Answers (1)

Cyril G.
Cyril G.

Reputation: 2017

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.

Upvotes: 3

Related Questions