gpt4 book ai didi

ubuntu - 如何在 Ubuntu 16.04 的 docker 容器中自定义主机名(URL)?

转载 作者:行者123 更新时间:2023-12-02 18:31:18 25 4
gpt4 key购买 nike

我有工作的 docker 容器。我通过 172.17.0.2:8000 而不是 www-local.dev-build.com 在浏览器中看到我的站点(并且它正常工作)。我尝试使用 --add-host(172.17.0.2 和 127.0.0.1)运行 docker 并在/etc/hosts 中添加相同的字符串。不工作。怎么了?我糊涂了。

docker info(容器号是其他的,因为它是旧容器)

Containers: 1
Running: 1
Paused: 0
Stopped: 0
Images: 4
Server Version: 17.03.0-ce
Storage Driver: aufs
Root Dir: /var/lib/docker/aufs
Backing Filesystem: extfs
Dirs: 7
Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 977c511eda0925a723debdc94d09459af49d082a
runc version: a01dafd48bc1c7cc12bdb01206f9fea7dd6feb70
init version: 949e6fa
Security Options:
apparmor
seccomp
Profile: default
Kernel Version: 4.8.0-41-generic
Operating System: Ubuntu 16.04.2 LTS
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 7.796 GiB
Name: pc-linux
ID: PBV5:TOE6:2O5T:C7YG:GONP:SPSK:IVK2:YYGP:AEN3:HAYT:VOUK:QK7A
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: No swap limit support
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false

docker 版本

Client:
Version: 17.03.0-ce
API version: 1.26
Go version: go1.7.5
Git commit: 3a232c8
Built: Tue Feb 28 08:01:32 2017
OS/Arch: linux/amd64

Server:
Version: 17.03.0-ce
API version: 1.26 (minimum version 1.12)
Go version: go1.7.5
Git commit: 3a232c8
Built: Tue Feb 28 08:01:32 2017
OS/Arch: linux/amd64
Experimental: false

ping www-local.dev-build.com

PING www-local.dev-build.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.061 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.058 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.104 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.065 ms
64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.113 ms
^C
--- www-local.dev-build.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4082ms
rtt min/avg/max/mdev = 0.058/0.080/0.113/0.024 ms

cat/etc/hosts

127.0.0.1   localhost
127.0.1.1 pc-linux
127.0.0.1 www-local.dev-build.com
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

cat/var/lib/docker/containers/ec1cc64d0c5c9c163a82191e171aed15cb11f73033eff3266e0e3273c9871b28/hosts

127.0.0.1       localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 ec1cc64d0c5c

P. S. 我已经测试了 https://docs.docker.com/engine/installation/linux/linux-postinstall 中的所有故障排除部分一切都好。但我的问题仍然存在。

最佳答案

如果你不想从你的主机访问你的容器化网站,那么你必须将你的本地域名关联到 docker 容器 IP:

在主机的etc/hosts

172.17.0.2   www-local.dev-build.com

但是如果您运行多个 docker 容器(取决于它们的启动顺序),这个 IP 可能会改变。 publish the containerized application port on host 可能是个更好的主意.为此,您可以使用 -p8000:8000 docker run 您的镜像,以便能够从主机端口 8000 访问容器的网络。例如:

docker run -it --name myapp -p8000:8000 myappimage

那么你的主机的 etc/hosts 应该是:

127.0.0.1   www-local.dev-build.com

并通过 http(s)://www-local.dev-build.com:8000 在主机的浏览器中访问您的应用程序。

关于ubuntu - 如何在 Ubuntu 16.04 的 docker 容器中自定义主机名(URL)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42972789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com