skip to Main Content

In my .env file I have setup the USERNAME:
USERNAME=myuser

When I do a docker compose build, it is unable to resolve the placeholders $USERNAME in the Dockerfile.

My Dockerfile:

FROM busybox:latest as build
ENV USERNAME=$USERNAME

.
.
.

How can I achieve this

2

Answers


  1. Chosen as BEST ANSWER

    I figured out the reason - the .env file must reside in the same folder as docker-compose.yml


  2. Try this:

    docker-compose.yml

    version: '3.5'
    
    services:
        container:
            build:
                context: .
                args:
                    USERNAME: ${USERNAME} # from .env file
            env_file:
                - .env
    

    Dockerfile

    # docker-compose args
    ARG USERNAME 
    ...
    

    .env

    USERNAME=value
    

    If you not use .env file, just remove env_file properties and set env in args.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search