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...
dockerdoes 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
.envfile 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
.envfile 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 projectsdotenvis a popular module to do this.So to override say an
API_ENDPOINTvariable specified in.envyou simply need to pass an environment variable with the same name and desired value to the container:If for some reason your applications do not work according to this convention and you actually need to change the values in the
.envfile you will need to write a custom script that updates/generates.envfrom the values of passed environment variables and use this script asENTRYPOINT