Docker how to use env_file variable in Dockerfile during build

Thaian

I would like to use STAGE variable from env_file in my Dockerfile. Most posts suggest just use -e with docker-compose up command but I would like to achieve this by the env_file.

I tried add args but it's not working.

Env file

STAGE=DEV

docker-compose.yml

version: '2'

services:
  web:
    build: .
    env_file:
      - /etc/web.env
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - ./backend:/code
    ports:
      - "8000:8000"

Dockerfile

FROM python:3.6
ENV PYTHONUNBUFFERED 1
(...)
RUN pip install -r project/requirements/$STAGE.txt

Result of this should be:

RUN pip install -r project/requirements/DEV.txt

I found that it is not possible during build but maybe exist some method to do this. I will be helpful for any tips.

Maokai

You can map the environment variable into the container by adding an environment option in your docker compose yaml file:

version: '2'

services:
  web:
    build: .
    env_file:
      - /etc/web.env
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - ./backend:/code
    ports:
      - "8000:8000"
    environment:
      - STAGE: ${STAGE}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Docker how to inject host ENV into Dockerfile ENV during build?

How do I interpolate ENV variables via env_file in docker-compose.yml to Dockerfile?

Docker-compose: Set a variable in env file and use it in Dockerfile

How to mount host volumes into docker containers in Dockerfile during build

How to do variable substitution in env_file?

Dockerfile - Get output during docker build

Docker-compose use env_file variables in the command

How to get port of docker-compose from env_file?

Dockerfile - use environment variable from env file

How to use an environment variable from a docker-compose.yml in a Dockerfile?

How does Docker know when to use the cache during a build and when not?

How to setup an variable ENV inside Dockerfile to be overriden in a Docker container?

How to pass ENV variable to build process in Docker

docker-compose not recognizing env_file file/location, and still tries to use the default .env

How to build Docker with env file

Setting build args in docker-compose.yml using env_file

How to pass GitLab CI file variable to Dockerfile and docker container?

How to use environment variable in Dockerfile?

How do you print to console from a docker file during build?

How can I docker-build with Dockerfile?

how to write a dockerfile and build a docker image?

How to use Azure variable $(build.buildnumber)-$(build.sourcebranchname) in Dockerfile as build artifact/jar is getting generated with buildNumber?

How to assign variable and use sed to replace contents of configuration file in Dockerfile?

How to specify which env_file is used in docker-compose via command line

Skip error in dockerfile during the build

How to execute script during image build in Docker

How to add an elasticsearch index during docker build

How to change ownership in docker image during build?

Docker and DockerFile build