是否可以从 Docker 修改(添加)到主机操作系统(Windows 或 Ubuntu 中的同等操作系统)的主机文件并创建 URL 和 IP 地址条目?
希望能够在执行命令的过程中动态添加:
docker-compose up -d
注:
127.0.0.1 www.lh-2.dock lh-2.dock
127.0.0.1 www.lh-2.pma.dock lh-2.pma.dock
尝试:
version: '3'
services:
myapp:
image: image_app
container_name: container_app
volumes:
- E:\Web\LH2\docker\hosts:C:\Windows\System32\drivers\etc\hosts
command: cmd /c "echo 127.0.0.1 www.lh-2.dock lh-2.dock >> C:\Windows\System32\drivers\etc\hosts && echo 127.0.0.1 www.lh-2.pma.dock lh-2.pma.dock >> C:\Windows\System32\drivers\etc\hosts && your_start_command"
和
version: '3'
services:
myapp:
image: image_app
container_name: container_app
volumes:
- /media/developer1/device/Web/LH2/docker/hosts:/etc/hosts
command: sh -c "echo '127.0.0.1 www.lh-2.dock lh-2.dock' >> /etc/hosts && echo '127.0.0.1 www.lh-2.pma.dock lh-2.pma.dock' >> /etc/hosts && tu_comando_de_inicio"
如何合并Windows/Linux上部署的两个环境,让系统选择合适的环境?
我能想到的唯一方法是通过主机操作系统中的脚本,虽然方法略有变化,但docker和容器的想法是不影响主机操作系统,因此很重要考虑编写脚本。
万一有人到这里,用Windows操作系统作为主机解决的方法是创建一个
.ps1
文件:
$main_url = "lh-2.dock"
$pma_url = "pma.lh-2.dock"
$hosts_file = "${env:SystemRoot}\System32\drivers\etc\hosts"
$host_entry = "127.0.0.1 $main_url $pma_url"
$current_content = Get-Content $hosts_file -Raw
$pattern = "(?ms)(# Developer Area Docker.*?# End of section)"
if ($current_content -match $pattern) {
$current_content = $current_content -replace $pattern, ("# Developer Area Docker`n$host_entry`n# End of section")
$current_content = $current_content -replace "(\r?\n)+\z", ""
Set-Content -Path $hosts_file -Value $current_content
Write-Host " --> Updated operating system hosts file."
} else {
$current_content += "`n`n# Developer Area Docker`n$host_entry`n# End of section"
$current_content = $current_content -replace "(\r?\n)+\z", ""
Set-Content -Path $hosts_file -Value $current_content
Write-Host " --> The Host section was added at the end of the hosts file."
}
$path = Join-Path -Path $scriptDirectory -ChildPath "docker-compose.yaml"
docker-compose -f $path up -d --force-recreate
如果有人知道如何在 Linux 和 macOS 中做到这一点,欢迎他们给出答案。