From 444871cfb2604a6ef8a41f6d6ba317b7e7ac42a7 Mon Sep 17 00:00:00 2001 From: Jamie Penney Date: Wed, 23 Aug 2023 15:01:53 +1200 Subject: [PATCH] Use host.docker.internal instead of local IP address in chrome.docker Resolves https://github.com/oblador/loki/issues/473 Fixes an issue where the Chrome docker container cannot access Storybook running on the host machine via IP address, instead using the Docker `host.docker.internal` DNS entry to get access to it. --- .../src/create-chrome-docker-target.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/target-chrome-docker/src/create-chrome-docker-target.js b/packages/target-chrome-docker/src/create-chrome-docker-target.js index d00e0620..cd534a7f 100644 --- a/packages/target-chrome-docker/src/create-chrome-docker-target.js +++ b/packages/target-chrome-docker/src/create-chrome-docker-target.js @@ -47,20 +47,19 @@ function createChromeDockerTarget({ } runArgs.push('--add-host=host.docker.internal:host-gateway'); - if (dockerUrl.indexOf('http://localhost') === 0 || isLocalFile) { + if (isLocalFile) { + const ip = 'host.docker.internal'; + staticServerPort = getRandomPort(); + staticServerPath = dockerUrl.substr('file:'.length); + dockerUrl = `http://${ip}:${staticServerPort}`; + } else if (dockerUrl.indexOf('http://localhost') === 0) { const ip = getLocalIPAddress(); if (!ip) { throw new Error( 'Unable to detect local IP address, try passing --host argument' ); } - if (isLocalFile) { - staticServerPort = getRandomPort({ start: 1025 }); - staticServerPath = dockerUrl.substr('file:'.length); - dockerUrl = `http://${ip}:${staticServerPort}`; - } else { - dockerUrl = dockerUrl.replace('localhost', ip); - } + dockerUrl = dockerUrl.replace('localhost', ip); } async function getIsImageDownloaded(imageName) {