sxiii
sxiii

Reputation: 43

A way to pass build-time arguments from .env file to Docker

Is there a way to pass .env-file variable from docker-compose up (or docker-compose build) invocation into the Dockerfile execution environment without really weird hacks?

I need to pass USER NAME variable that will be used in build-time environment to become this user.

I've tried to do this today and spent over 6 hours with different approaches and not a single one allows me to use the pre-made .env file user variable. It works for me only when I hardcode user name into Dockerfile, sadly.

I've spend so much time on stackoverflow today reading all the answers and tailoring my environment, but I still can't reach what I'm trying to do. I know there are many similar questions. But they don't seem to help whatsoever...

Help would be much appreciated! Thanks...

P.S. Example .env:

version: '3'
services:
  terra-validator:
    image: terra-validator
    env_file:
      - terra-validator/variables.env
    environment:
      - NEWUSER=${NEWUSER}
    build:
      context: terra-validator
      dockerfile: Dockerfile
      args:
        - NEWUSER=$NEWUSER

Dockerfile:

ARG NEWUSER
ENV NEWUSER $NEWUSER
RUN adduser $NEWUSER
USER $NEWUSER
CMD whoami

Adduser and useradd commands does not work... They always prints out same 3-4 type of errors depending on my approaches...

Upvotes: 4

Views: 1062

Answers (2)

atline
atline

Reputation: 31674

You didn't give your full error, so here just give you a workable solution, FYI:

docker-compose.yaml:

version: '3'
services:
  terra-validator:
    image: terra-validator
    build:
      context: terra-validator
      dockerfile: Dockerfile
      args:
        - NEWUSER=$NEWUSER

.env:

NEWUSER=tester

terra-validator/Dockerfile:

FROM ubuntu:16.04

ARG NEWUSER
ENV NEWUSER $NEWUSER
RUN adduser $NEWUSER --disabled-password --gecos "First Last,RoomNumber,WorkPhone,HomePhone"
USER $NEWUSER
CMD whoami

Execution:

$ docker-compose up
Creating network "20210811_default" with the default driver
Building terra-validator
Step 1/6 : FROM ubuntu:16.04
 ---> 065cf14a189c
Step 2/6 : ARG NEWUSER
 ---> Using cache
 ---> 6c11feb31ac0
Step 3/6 : ENV NEWUSER $NEWUSER
 ---> Using cache
 ---> 365472fb8bf2
Step 4/6 : RUN adduser $NEWUSER --disabled-password --gecos "First Last,RoomNumber,WorkPhone,HomePhone"
 ---> Running in 3156249774f8
Adding user `tester' ...
Adding new group `tester' (1000) ...
Adding new user `tester' (1000) with group `tester' ...
Creating home directory `/home/tester' ...
Copying files from `/etc/skel' ...
Removing intermediate container 3156249774f8
 ---> 8debf4402db6
Step 5/6 : USER $NEWUSER
 ---> Running in 5f86175c5d7b
Removing intermediate container 5f86175c5d7b
 ---> 400f7951c4be
Step 6/6 : CMD whoami
 ---> Running in 5fe9cf8cc6f8
Removing intermediate container 5fe9cf8cc6f8
 ---> f63f2e284104
Successfully built f63f2e284104
Successfully tagged terra-validator:latest
WARNING: Image for service terra-validator was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating 20210811_terra-validator_1 ... done
Attaching to 20210811_terra-validator_1
terra-validator_1  | tester
20210811_terra-validator_1 exited with code 0

Explanation:

The value defined in .env will be used by args in docker-compose.yaml, then be passed to Dockerfile ARG. During user setup, you'd better add --disabled-password --gecos "First Last,RoomNumber,WorkPhone,HomePhone" to remove some error message. Finally, in the docker-compose up log, you could see whoami prints tester.

Upvotes: 3

Antonio Petricca
Antonio Petricca

Reputation: 11070

I solved a similar problem by the followinf approach:

Docker compose file

version: '3'

services:
  terra-validator:
    image: terra-validator
    environment:
      - NEWUSER=${NEWUSER}
    build:
      context: terra-validator
      dockerfile: Dockerfile
      args:
        - NEWUSER=${NEWUSER}

PS: I removed the env file.

Bash script file


NEWUSER=$1 # For example coming from command line
NEWUSER=${NEWUSER} docker-compose up

Let me know if it works for you too.

Regards.

Upvotes: 0

Related Questions