0

So I have been tasked with taking an existing dockerized version of a service, and creating docker images from this repository.

Creating the images is not the problem however, since the build command starts it up no problem. The issue is that this dockerfile copies an .env file during build, that holds variables that must be customizable after the build process is done (expected db and other endpoint info).

Is there some way to set that file to automatically be changed to reflect the environmental variables used in the docker run command? (I do want to note, that the docker image does copy the .env file into the working directory, it is not docker-compose reading that .env file)

I am sure that there has to be an easy way to do this, but all the tutorials I am pulling up just show you how to declare these variables, not how to get the files in docker to use them! Most of the code being run is javascript, and uses npm and yarn if that makes any difference...

Flotolk
  • 313
  • 1
  • 11
  • 37

1 Answers1

0

docker does not provide any way to update files from environment variables on container start. But I don't think this is what you need anyway:

As I understand a .env file with default values is copied into the image at build time and you want to be able to change some of the values at runtime via container environment variables?

Usually such an .env file is read by the application and complemented by any variables set in the environment, i.e. you can override values from the file with environment variables. For javascript projects dotenv is a popular module to do this.

So to override say an API_ENDPOINT variable specified in .env you simply need to pass an environment variable with the same name and desired value to the container:

docker run -e API_ENDPOINT=/other/endpoint ...

If for some reason your applications do not work according to this convention and you actually need to change the values in the .env file you will need to write a custom script that updates/generates .env from the values of passed environment variables and use this script as ENTRYPOINT

acran
  • 7,070
  • 1
  • 18
  • 35