Docker Swarm部署Windows容器:节点web.config文件复制问题求助
Since you've already got your Docker Swarm cluster (1 manager + 5 workers) and Stack up and running, here's how to tweak your setup to copy each node's unique web.config into its corresponding Windows container:
1. Refine Your Entrypoint Script
Let's adjust your entrypoint.bat to handle the file copy before launching the container's core service. This script will take the node-specific web.config you mount and overwrite the default one in your website directory.
Here's a sample script (update paths to match your container's website root):
@echo off echo Copying node-specific web.config to website directory... copy "C:\mounted-config\web.config" "C:\inetpub\wwwroot\web.config" /Y if %errorlevel% neq 0 ( echo Error copying web.config! Exiting. exit /b %errorlevel% ) echo Starting web service... rem Replace this line with your container's original entrypoint command "C:\ServiceMonitor.exe" w3svc
Option A: Embed the Script in Your Container Image (Recommended)
Skip deploying the script to every node by building a custom image based on your existing Windows container:
- Create a
Dockerfile:
FROM your-existing-windows-image:tag COPY entrypoint.bat C:\scripts\entrypoint.bat ENTRYPOINT ["cmd", "/c", "C:\\scripts\\entrypoint.bat"]
- Build and push this image to your registry so all Swarm nodes can pull it.
Option B: Bind-Mount the Script from Each Node
If you don't want to rebuild the image, place entrypoint.bat at the same path on every worker node (e.g., C:\swarm-scripts\entrypoint.bat), then we'll mount it in the Stack compose.
2. Update Your Stack Compose File
Modify your docker-compose.yml to add a bind mount for the node-specific web.config and set the custom entrypoint:
version: "3.8" services: your-web-service: image: your-custom-image:tag # Use original image if using Option B deploy: replicas: 5 placement: # Ensure one container runs per worker node (matches your per-node config need) constraints: [node.role == worker] volumes: # Mount the node's local web.config to a temporary container path - type: bind source: C:\path\on\node\web.config # This path must exist on every worker node target: C:\mounted-config\web.config read_only: true # Only required for Option B (bind-mounting the entrypoint script) - type: bind source: C:\swarm-scripts\entrypoint.bat target: C:\scripts\entrypoint.bat read_only: true entrypoint: ["cmd", "/c", "C:\\scripts\\entrypoint.bat"] # Omit if script is embedded in image # Add your existing service configs (ports, environment variables, etc.)
Key points:
- The
sourcepath forweb.configmust be identical across all worker nodes—each node holds its own version of the file at this location. - The
placement.constraintsensures one container per worker node, aligning with your per-node configuration requirement.
3. Deploy the Updated Stack
Run the standard Swarm deploy command to apply your changes:
docker stack deploy -c docker-compose.yml your-stack-name
4. Verify the Setup
To confirm the web.config was copied correctly, exec into a container on each node and check the file content:
# Replace <container-id> with your container's ID docker exec -it <container-id> type C:\inetpub\wwwroot\web.config
You should see content matching the node's local web.config file.
内容的提问来源于stack exchange,提问作者Suhas

