title: How do you manage configuration in a dockerised nextjs application?
body: <p>We have a nextjs frontend that's been developed by an outside agency.  We have developed a Python backend that does all the application logic.</p>
  <p>The nextjs server is configured with a <code>.env</code> file.  We're now trying to dockerise the frontend for deployment.  We have two deployment environments, one for testing and one for production.  We can also run the stack locally during development.  So developers usually have a <code>.env</code> file that is configured for their local deployment.  When they want to deploy to the testing environment, they build docker images, push them to a (private) registry, log in to the testing server, pull the images and restart the docker stack.</p>
  <p>The problem is that the images then include their <code>.env</code> file that is configured for a local deployment; lots of references to <code>http://localhost:3000</code> rather than the testing site's HTTPS URL.  How do people go about managing this configuration in deployments?</p>
  <p>Some options I've thought of:</p>
  <ul>
  <li>I can ask developers (there are only two of us) to just make sure they substitute the right <code>.env</code> file when building the images for testing/production.  This is a bit annoying as it means there are three copies of each image with only the .env file differing (and the follow-on differences in the <code>.next</code> folder) and seems rather error-prone.</li>
  <li>I can map the <code>.env</code> file in as a volume in the stack's docker-compose configuration.  But this doesn't work, because nextjs has already build its optimised version at this point and got it wrong.  I'm trying to avoid having to build the optimised version each time the server starts (what's the point of dockerising it anyway?)</li>
  <li>I can have a CI / build server automatically substitute in the right .env file, build the images and deploy them automatically.  This is attractive but has a couple of issues.  It has the same problem as the first option, that the images for each environment are different.  It's not the end of the world but seems wasteful (and the difference is the whole optimised <code>.next</code> folder, not just the <code>.env</code> file).  It also means all the configuration has to be committed to git - not idea, since this includes multiple API keys.  The repo is not public but it's still not good practice, right?</li>
  </ul>
  <p>Is there some other strategy I'm missing here?</p>