如何在Docker IIS容器中绑定多IP并运行多个网站?
Hey there! As someone who’s worked with IIS and Docker quite a bit, let’s break this down step by step for you—since you’re new to Docker, I’ll keep things straightforward and avoid overly technical jargon where I can.
By default, Windows Docker containers use a NAT network and only get one IP address. To assign multiple IPs, we’ll use a custom network and add extra IPs to the container after launch:
- First, create a custom NAT network (we’ll define a subnet so we have full control over IP assignments):
docker network create -d nat --subnet=192.168.10.0/24 --gateway=192.168.10.1 my-iis-network - Launch your IIS container with the first IP address assigned:
docker run -d --name my-iis-container --network my-iis-network --ip 192.168.10.10 mcr.microsoft.com/windows/servercore/iis:windowsservercore-ltsc2019 - Add a second IP to the same container by connecting it again to the custom network with a new IP:
docker network connect --ip 192.168.10.11 my-iis-network my-iis-container - Verify the container has both IPs by running:
You should see bothdocker exec my-iis-container ipconfig192.168.10.10and192.168.10.11listed under the Ethernet adapter.
Now that your container has multiple IPs, let’s configure separate websites to bind to each one. We’ll use PowerShell inside the container to set this up:
- First, open an interactive PowerShell session in the container:
docker exec -it my-iis-container powershell - Create directories and sample content for each website:
# Create Site 1 files New-Item -Path C:\inetpub\site1 -ItemType Directory New-Item -Path C:\inetpub\site1\index.html -Value "<h1>Welcome to Site 1 (IP: 192.168.10.10)</h1>" # Create Site 2 files New-Item -Path C:\inetpub\site2 -ItemType Directory New-Item -Path C:\inetpub\site2\index.html -Value "<h1>Welcome to Site 2 (IP: 192.168.10.11)</h1>" - Use IIS’s PowerShell module to create each website and bind it to a specific IP:
Import-Module WebAdministration # Create Site 1 bound to 192.168.10.10:80 New-Website -Name "Site1" -Port 80 -IPAddress "192.168.10.10" -PhysicalPath "C:\inetpub\site1" # Create Site 2 bound to 192.168.10.11:80 New-Website -Name "Site2" -Port 80 -IPAddress "192.168.10.11" -PhysicalPath "C:\inetpub\site2"
- From your Docker host, test each site using their container IPs:
# Test Site 1 Invoke-WebRequest http://192.168.10.10 # Test Site 2 Invoke-WebRequest http://192.168.10.11 - If you want external devices to access these sites, you’ll need to map the container’s IPs/ports to your host’s external IPs. For example, if your host has IPs
10.0.0.10and10.0.0.11, launch the container with:
Then external users can visitdocker run -d --name my-iis-container -p 10.0.0.10:80:80 -p 10.0.0.11:80:80 --network my-iis-network --ip 192.168.10.10 mcr.microsoft.com/windows/servercore/iis:windowsservercore-ltsc2019http://10.0.0.10andhttp://10.0.0.11to see each site.
If you don’t want to run these commands every time, you can create a Dockerfile to pre-configure your websites:
FROM mcr.microsoft.com/windows/servercore/iis:windowsservercore-ltsc2019 # Create website directories and content RUN New-Item -Path C:\inetpub\site1 -ItemType Directory; ` New-Item -Path C:\inetpub\site1\index.html -Value "<h1>Welcome to Site 1</h1>"; ` New-Item -Path C:\inetpub\site2 -ItemType Directory; ` New-Item -Path C:\inetpub\site2\index.html -Value "<h1>Welcome to Site 2</h1>" # Configure IIS websites RUN Import-Module WebAdministration; ` New-Website -Name "Site1" -Port 80 -IPAddress "192.168.10.10" -PhysicalPath "C:\inetpub\site1"; ` New-Website -Name "Site2" -Port 80 -IPAddress "192.168.10.11" -PhysicalPath "C:\inetpub\site2"
Build the custom image with:
docker build -t my-custom-iis .
Then launch your container using this image and assign multiple IPs as we did earlier.
内容的提问来源于stack exchange,提问作者user1550159

