Search code examples
gitversion-controldockerfileenvironment-variablesdotenv

Environment variables should not be checked in to repositories... what about dockerfille?


I learned it's best to keep environment variable values outside of your source control. This is done by default obviously, but when using dot-env, they strongly advises against checking in the .env file.

However, I need to have a docker file for my app so I can build and deploy it. I also decided to not use the ENV instruction inside my dockerfile because I check in that file into my repository.

But then how do I test my docker image on my machine if it doesn't have environment variables in it? I need to either:

  • Set the environment variables on my machine to have production environment values
    • Which would be very annoying if I have to keep setting them and reverting them
  • Store them in the docker file
    • Which would mean I would check it in my source control

My question is: Should I store environment variables in my dockerfile if it gets checked into my repository?


Solution

  • Should I store environment variables in my dockerfile if it gets checked into my repository?

    You should not: Any value your Dockerfile need (value that you can get from an external referential, like a Git repository) can be passed as ARG.

    You can then docker build your image using:

    docker build --build-arg arg1=arg1Value .
    

    In your Dockerfile, you would then have (combining ARG and ENV):

    ARG arg1
    ENV myvar=${arg1}
    ...