Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Port is occupied when starting from container #23

Closed
yf-yang opened this issue Jun 23, 2024 · 6 comments · Fixed by #47
Closed

Error: Port is occupied when starting from container #23

yf-yang opened this issue Jun 23, 2024 · 6 comments · Fixed by #47
Labels
bug Something isn't working

Comments

@yf-yang
Copy link

yf-yang commented Jun 23, 2024

I tried to migrate the example to our workspace. It is a vscode editor + docker container. Then I found when executing storybook in the container, the port would be occupied before rsbuild dev server is created.

I haven't created something reproducible yet, so it might not be an issue with container environment.

Current observation:
By adding breakpoints, here

await readTemplate(
require.resolve(
'storybook-builder-rsbuild/templates/virtualModuleModernEntry.js.handlebars',
),
),

When readTemplate get executed, a browser window pops up. That's where two environment diverges. If I run storybook natively in the laptop, there is no port occupation. If I execute it in the container, after it executes, the port gets occupied, so rsbuild dev server would stop.

I'll continue investigating it. Do you have any idea what storybook does here and what is its possible cause?

@yf-yang
Copy link
Author

yf-yang commented Jun 23, 2024

I don't quite understand what readTemplate does here. It should be a helper that calls fs.readFile, then how does it open the browser and occupies the port? It is an intercepted API? I may understand what happens if I know how it opens the browser.

https://github.com/storybookjs/storybook/blob/41fd263fb420e61cf90388964d6fad02dfae075f/code/lib/core-common/src/utils/readTemplate.ts#L4

@yf-yang
Copy link
Author

yf-yang commented Jun 24, 2024

Reproduction: https://github.com/yf-yang/rsbuild-storybook-esm/tree/container-bug
Steps:

  1. Open the folder with vscode
  2. Show all commands (command/ctrl + shift + p)
  3. Select "reopen in container"

Then it would use the ubuntu22.04 image to create a container and install node/pnpm
After the container is set

  1. pnpm i
  2. pnpm storybook

I've added breakpoints myself and found after readTemplate, before rsbuild dev server is up, the port is already occupied.

Alternatively (if not using devcontainer),
docker run -it -v $(pwd):/workspace -w /workspace node:22.3.0 /bin/bash
Then,

npm i
npm run storybook

@yf-yang yf-yang changed the title Error: Port is occupied Error: Port is occupied when starting from container Jun 24, 2024
@fi3ework fi3ework added the bug Something isn't working label Jun 24, 2024
@yf-yang
Copy link
Author

yf-yang commented Jun 24, 2024

Additional information:

By adding BROWSER=none (storybookjs/storybook#6201 (comment)) and starts cli, it reports Error: spawn none ENOENT. It may help debugging. I guess current implementation has something different with webpack, but I don't go through the storybook source code deeply.

@fi3ework
Copy link
Member

I did reproduce the issue, when running without Docker, there could be two process listening the same port whereas in Docker it throws. I'm not sure does webpack/Vite builder will fail in Docker, either.

@yf-yang
Copy link
Author

yf-yang commented Jun 24, 2024

I used webpack builder + container previously, but I am reluctant to reproduce it 🤨.
Currently, I patch the package with strictPort=false, so for example, sb starts at 6006, rsbuild dev server starts at 6007, then 6006 is accessible, changes are taking effect after refreshing, too.

I don't know what dev server does here, but based on the observation it doesn't need to be exposed. (Maybe the claim is wrong, since outside of a container, a port is still needed).

@CptLemming
Copy link

Coming from WSL and also getting the port in use error. If I edit the start function to strictPort: false as suggested Storybook starts with the warning:

info => Starting manager..
info => Starting preview..
info Addon-docs: using MDX3
info Port 9001 is in use, using port 9002.

However in the browser log I see:

hmr.js:237 WebSocket connection to 'ws://localhost:9002/rsbuild-hmr' failed:

There doesn't appear to be any server running on 9002, so I guess the error is valid. I get the same startup error if I run the example "react-rsbuild" sandbox app if that helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants