如何通过命令暴露Docker for Windows守护进程?含WSL配置与磁盘共享需求
Hey there! Let's tackle your Docker for Windows command-line questions one by one, skipping all those GUI clicks:
1. Expose Docker daemon on tcp://localhost:2375 via command line (no GUI)
Absolutely! Docker Desktop for Windows stores its core configuration in %APPDATA%\Docker\settings.json. You can modify this file with PowerShell to enable the unencrypted TCP endpoint, then restart the service to apply changes. Here's how:
- First, stop the Docker Desktop service if it's running:
Stop-Service "Docker Desktop Service" - Update the settings to add the TCP host entry. This script checks if the
hostsproperty exists, adds it if not, and appends the TCP endpoint if it's missing:$settingsPath = "$env:APPDATA\Docker\settings.json" $settings = Get-Content $settingsPath | ConvertFrom-Json if (-not $settings.PSObject.Properties['hosts']) { # Initialize hosts with default npipe + TCP endpoint $settings | Add-Member -MemberType NoteProperty -Name "hosts" -Value @("npipe:////./pipe/docker_engine", "tcp://0.0.0.0:2375") } else { # Add TCP endpoint only if it's not already present if ($settings.hosts -notcontains "tcp://0.0.0.0:2375") { $settings.hosts += "tcp://0.0.0.0:2375" } } # Save the updated settings $settings | ConvertTo-Json -Depth 10 | Set-Content $settingsPath - Restart the Docker Desktop service to apply the change:
Start-Service "Docker Desktop Service"
⚠️ Important Security Note: Enabling unencrypted TCP (no TLS) exposes your Docker daemon to potential risks. Only use this setup in trusted, local development environments.
2. Command-line alternative to the GUI toggle for WSL Docker setup
The GUI step you mentioned (enabling the TCP endpoint) can be replaced with the command-line method from question 1. You can also automate the WSL environment variable setup without touching any GUIs:
- From Windows PowerShell, append the
DOCKER_HOSTexport to your WSL user's.bashrc(replaceyour_usernamewith your actual WSL username—get it viawsl whoami):wsl -u your_username echo 'export DOCKER_HOST=tcp://localhost:2375' >> ~/.bashrc - To apply the change immediately in an active WSL session, run:
source ~/.bashrc
Now you can run Docker commands directly in WSL without ever opening the Docker Desktop settings GUI.
3. Command-line solution for Docker disk sharing
Docker Desktop's drive sharing is also controlled via the settings.json file. Use PowerShell to enable shared drives without the GUI:
- Stop the Docker Desktop service first:
Stop-Service "Docker Desktop Service" - Update the
sharedDrivessection to enable the drives you want to share. For example, to share the C drive:$settingsPath = "$env:APPDATA\Docker\settings.json" $settings = Get-Content $settingsPath | ConvertFrom-Json if (-not $settings.PSObject.Properties['sharedDrives']) { # Initialize shared drives with C: enabled $settings | Add-Member -MemberType NoteProperty -Name "sharedDrives" -Value @{ "C" = $true } } else { # Enable C: drive sharing $settings.sharedDrives["C"] = $true # Add more drives if needed, e.g.: # $settings.sharedDrives["D"] = $true } # Save the changes $settings | ConvertTo-Json -Depth 10 | Set-Content $settingsPath - Restart the Docker Desktop service:
Start-Service "Docker Desktop Service"
ℹ️ Note: The first time you access the shared drive from a container, Docker may prompt you for your Windows credentials to verify you have permission to access the drive. This is a standard security check.
内容的提问来源于stack exchange,提问作者AnjK

