host-docker-internal
v0.2.4
Published
Fetch Docker host IP/hostname from inside container.
Maintainers
Readme
host-docker-internal
Fetch Docker host IP/hostname from inside containers.
在容器内部获取 Docker 宿主机的 IP 或主机名。
🔍 What it does
Helps you quickly fetch the IP or hostname of the Docker host from inside a container, making it easy to access services on the host or in other containers.
帮你在容器内部,快速获取 Docker 宿主机的 IP 或主机名,以便访问宿主机或其他容器中的服务。
🚀 When to use it
This library comes in handy when you encounter any of the following situations:
- 🐧 When Docker runs on Linux (not macOS or Windows), you can't directly use
host.docker.internal - ⚙️ When it's inconvenient to add
--add-host=host.docker.internal:host-gatewayduringdocker run - 🌍 When you don't want to switch between
localhostandhost.docker.internalbetween development and production environments - 💡 When you face any of the above but don't want to implement the fetching logic yourself
当你遇到以下任意情况时,本库可以派上用场:
- 🐧 当 Docker 在 Linux 而不是 macOS 或 Windows 上运行时,无法直接使用
host.docker.internal- ⚙️ 当不方便在
Docker run时添加--add-host=host.docker.internal:host-gateway- 🌍 当你不想在开发环境与生产环境之间,来回切换
localhost和host.docker.internal- 💡 当你遇到以上几种情况,但又不想自己实现获取逻辑
📦 Install
npm install host-docker-internal💻 Example
Docker Host
import { getDockerHost, isValidIp } from 'host-docker-internal';
const host = getDockerHost();
console.log(host);
console.log(isValidIp(host));
console.log(host === 'host.docker.internal');
await fetch(`http://${host}:3000/api/hello`);Container IP
import { getContainerIp, isValidIp } from 'host-docker-internal';
const containerIp = getContainerIp();
console.log(containerIp);
console.log(isValidIp(containerIp));📝 Remark
- During local development and debugging (not in a container),
hostis127.0.0.1 - When the container runs on macOS or Windows,
hostishost.docker.internal - When the container runs on Linux,
hostis the host machine's IP - If fetching fails,
hostdefaults to172.17.0.1
- 本地开发调试时(不在容器中),
host为127.0.0.1- 容器运行在 macOS 或 Windows 上时,
host为host.docker.internal- 容器运行在 Linux 上时,
host为宿主机 IP- 如果获取失败,
host为默认值172.17.0.1
