Search code examples
reactjsdockerdocker-composedocker-stack

Where to store/manage configuration for a container in a Docker stack?


I'm developing a web app based on React. This app shall be able to run as a Docker container in production. I created an .env.development file in my repository in which I can configure an API endpoint the app will be connecting to.

REACT_APP_API_BASE_URL=http://localhost:8180/api

Running the container on my local machine works fine this way.

Now I'd like to run this container in a Docker stack so I configured a docker-compose.yml.

  1. Where and how should I store/manage the above configuration so that the Docker stack will pick it up? Is there something like a "recommended way"?
  2. Is it enough to define this parameter in the environment section of the docker-compose.yml? Or do I need to use something like Docker config or even Docker Secrets?

Solution

  • If we're subscribing to the build-once-run-anywhere approach then, you're right, we should be aiming to build the container once and run it in any environment by modifying its environment variables.

    Modifying the environment variables doesn't have to mean passing values, like API keys, directly to your application though, you could use the environment variables to point to a vault or etcd service and use these tools to bootstrap your application and build your configuration files.

    I'd recommend adding an initialisation layer to your container (using ENTRYPOINT) that does the configuration of your application before the CMD is triggered. This could be anything from setting up your database connection, warming your cache or installing your dependencies.

    You can certainly shoot yourself in the foot with bad entrypoints. Be sure to review some of the popular images to see how they handle these scenarios - postgres is a good one.