Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build Docker with env file

I'm stuck with env file during docker build.

I'm trying to build docker image for my ruby project which uses environment variables for a number of reasons. One of the reason is checking either database is available or not(Using ActiveRecord::Base.connection), and based on that going to create/update a database with some master data.

I can use env file during docker run via docker run --env-file='.env' command.

But I want to use those ENV variables during my docker build.

Can anyone help me out with this situations?

like image 598
Ravi Patel Avatar asked Dec 25 '17 12:12

Ravi Patel


People also ask

Does Docker build .env files?

If you are using docker-compose (which now comes bundled with Docker), . env is the default filename for the file that contains variables that are made available to the parser for the docker-compose. yml file ONLY, and not to the build process or container environment variables.

What is env Docker file?

ENV is mainly meant to provide default values for your future environment variables. Running dockerized applications can access environment variables. It's a great way to pass configuration values to your project. ARG values are not available after the image is built.


1 Answers

You can also set build environment variables with --build-arg. Example on documentation

As you can see, it is an equivalent to docker run --env, there isn't one for --env-file

like image 159
Gabriel Miretti aka gmiretti Avatar answered Sep 22 '22 18:09

Gabriel Miretti aka gmiretti