- Docker on Windows
- Elton Stoneman
- 423字
- 2021-07-02 12:47:59
Mounting configuration files in Docker volumes
Traditional application platforms use configuration files to change behavior between environments. .NET Framework apps have a rich XML-based configuration framework, and Java apps commonly use key-value pairs in properties files. You can add these configuration files to your application image in a Dockerfile, and when you run a container from the image, it will use this default configuration.
Your application setup should use a specific directory for configuration files, and this will let you overwrite them at runtime by mounting a Docker volume. I've done this with a simple ASP.NET WebForms application in dockeronwindows/ch03-aspnet-config:2e. The Dockerfile only uses commands you've already seen:
# escape=`
FROM mcr.microsoft.com/dotnet/framework/aspnet
COPY Web.config C:\inetpub\wwwroot
COPY config\*.config C:\inetpub\wwwroot\config\
COPY default.aspx C:\inetpub\wwwroot
This uses Microsoft's ASP.NET image as the base, and copies in my application files – an ASPX page and some config files. In this example I'm using the default IIS website, which loads content from C:\inetpub\wwwroot, so I just have the COPY instructions in the Dockerfile, I don't need to run any PowerShell scripts.
ASP.NET expects to find the Web.config file in the application directory, but you can split out sections of config into separate files. I've done this with the appSettings and connectionStrings sections, which are loaded from files in a subdirectory:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings configSource="config\appSettings.config" />
<connectionStrings configSource="config\connectionStrings.config" />
</configuration>
The config directory is populated with default configuration files, so I can run a container from the image without having to specify any extra settings:
docker container run -d -P dockeronwindows/ch03-aspnet-config:2e
When I fetch the container's port and browse to it, I see the web page displaying values from the default config files:

I can run the app for a different environment by loading configuration files from a directory on the host, mounting the local directory as a volume targeting C:\inetpub\wwwroot\config inside the container. When the container runs, the contents of that directory will be loaded from the directory on the host:
docker container run -d -P `
-v $pwd\prod-config:C:\inetpub\wwwroot\config `
dockeronwindows/ch03-aspnet-config:2e
When I browse to this container's port, I see different config values displayed:

The important thing here is that I'm using the exact same Docker image, with the same setup and the same binaries in every environment. Only the configuration files change, and Docker provides an elegant way of doing this.
- vSphere Virtual Machine Management
- VMware Horizon View 6 Desktop Virtualization Cookbook
- 循序漸進(jìn)學(xué)Docker
- SharePoint 2013 應(yīng)用開發(fā)實(shí)戰(zhàn)
- Windows Phone應(yīng)用程序開發(fā)
- Docker+Kubernetes應(yīng)用開發(fā)與快速上云
- Android物聯(lián)網(wǎng)開發(fā)細(xì)致入門與最佳實(shí)踐
- 嵌入式系統(tǒng)及其應(yīng)用(第三版)
- Fedora 12 Linux應(yīng)用基礎(chǔ)
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)(Windows 7+Office 2016)
- Kali Linux高級滲透測試(原書第3版)
- Linux軟件管理平臺設(shè)計(jì)與實(shí)現(xiàn)
- Linux 從入門到項(xiàng)目實(shí)踐(超值版)
- 統(tǒng)信UOS應(yīng)用開發(fā)進(jìn)階教程
- 完美應(yīng)用Ubuntu(第2版)