Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I provide application config to my .NET Core Web API services running in docker containers?

I am using Docker to deploy my ASP.NET Core Web API microservices, and am looking at the options for injecting configuration into each container. The standard way of using an appsettings.json file in the application root directory is not ideal, because as far as I can see, that means building the file into my docker images, which would then limit which environment the image could run in.

I want to build an image once which can they be provided configuration at runtime and rolled through the dev, test UAT and into Production without creating an image for each environment.

Options seem to be:

  1. Providing config via environment variables. Seems a bit tedious.
  2. Somehow mapping a path in the container to a standard location on the host server where appsettings.json sits, and getting the service to pick this up (how?)
  3. May be possible to provide values on the docker run command line?

Does anyone have experience with this? Could you provide code samples/directions, particularly on option 2) which seems the best at the moment?

like image 290
Peter Avatar asked Nov 15 '25 02:11

Peter


1 Answers

It's possible to create data volumes in the docker image/container. And also mount a host directory into a container. The host directory will then by accessible inside the container.

Adding a data volume

You can add a data volume to a container using the -v flag with the docker create and docker run command.

$ docker run -d -P --name web -v /webapp training/webapp python app.py

This will create a new volume inside a container at /webapp.

Mount a host directory as a data volume

In addition to creating a volume using the -v flag you can also mount a directory from your Docker engine’s host into a container.

$ docker run -d -P --name web -v /src/webapp:/webapp training/webapp python app.py

This command mounts the host directory, /src/webapp, into the container at /webapp.

Refer to the Docker Data Volumes

like image 54
Pradeep Kumar Avatar answered Nov 17 '25 18:11

Pradeep Kumar