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

dev-server inside docker? #59

Closed
samjarrett opened this issue Jun 27, 2017 · 8 comments · Fixed by #96
Closed

dev-server inside docker? #59

samjarrett opened this issue Jun 27, 2017 · 8 comments · Fixed by #96

Comments

@samjarrett
Copy link

Hi,

I'm trying to get encore dev-server to work inside a docker container.

I noticed at first that it listens only on the container's loopback interface ('localhost'), which was OK, because I could use --host 0.0.0.0 to get it to listen on the proper network interface...

But when I do this the manifest.json ends up using http://0.0.0.0:8080/.... as it's url prefix, and this stops the assets from loading.

I tried the --public ... flag to set a hostname for the public URL, but that doesn't seem to affect manifest generation.

How can I do this?

Thanks.

@davidmpaz
Copy link
Contributor

I haven't tried this yet but one hint would be to treat your container like another named virtual host. If you are referencing them (containers) inside docker network use the container name, if it is from outside I think you will need to modify your /etc/hosts file to recognize the name you are using.

Then, use the assets as you would like with a CDN. Look here at end this add the prefix you want with a know url.

If this doesn't helps, please add more information, if possible, configuration files etc.

Cheers

@samjarrett
Copy link
Author

samjarrett commented Jun 27, 2017

Hey @davidmpaz, good thought. Doesn't work though. :(

webpack.config.js:

// .. rest of configuration

if (!Encore.isProduction()) {
    Encore.setPublicPath('http://localhost');
    Encore.setManifestKeyPrefix('/assets/build/');
}

and got this error:
Error: You cannot pass an absolute URL to setPublicPath() and use the dev-server at the same time. Try using Encore.isProduction() to only configure your absolute publicPath for production.

(Note: same error regardless of hostname)

@davidmpaz
Copy link
Contributor

@samjarrett lets try to separate issues. From:

I'm trying to get encore dev-server to work inside a docker container.

I assume this is not related to Encore but with a development enviroment which can work without Encore. So my question are:

  1. Does It works if you remove Encore from the dev stack?
  2. Can you start webpack-dev-server and see your project's assets being served?

If so, next comes Encore troubleshooting. If not, then docker, webpack setup toguether need to be addressed first.

@samjarrett
Copy link
Author

Hey @davidmpaz, actually it's an issue with how encore bundles a few webpack configuration items into one setting, which in a docker (or any remote dev style environment) you would hit an issue with.

To recap the problem:

  1. webpack dev server defaults to binding only on local loopback
  2. Official --help on encore dev-server says to use --host= to change the bind
  3. This also then changes the host name configured in webpack manifest plugin

Encore doesn't provide a way of altering the config manually for the manifest plugin. If I was using standard webpack config I would easily be able to override that.

@davidmpaz
Copy link
Contributor

Ok, sorry for not being of more help ;) Maybe @weaverryan has an idea on how this can be solve?

@weaverryan
Copy link
Member

Thanks for breaking this down guys. Indeed, you've described it perfectly: whwn you use dev-server, we automatically use whatever its host is as the publicPath (this affects the manifest.json paths but also internal paths in webpack in case you use code splitting).

Once fix might be to allow you to set the publicPath while use the dev-server, but issue a warning on the terminal. We want to avoid someone setting a CDN URL (in production and dev) and not having things work when they use the dev-server. I think this would still cover that :)

weaverryan added a commit that referenced this issue Jul 21, 2017
…ausk, weaverryan)

This PR was merged into the master branch.

Discussion
----------

Relaxed public path requirements with dev-server

Fixes #59 and finishes #66.

* Adds a new `--keep-public-path` option for `dev-server`. When used, your `publicPath` is not prefixed with the dev server URL. For #59, this means you can use `setPublicPath('/build')` with the dev-server, and your assets will remain local (i.e. `/build/main.js` instead of `http://localhost:8080/build/main.js`).

* It is now possible to pass an absolute URL to `setPublicPath()` without an error when using `dev-server`. But, we issue a big warning, because this means your assets will point to to that absolute URL, instead of to the dev-server (which for most setups, is not what you want).

@samjarrett I'd love to confirm that this would solve your issue in Docker :).

Commits
-------

4bc1e19 Using real public path, though it doesn't look like it matters
92e22af Allowing an absolute publicPath with dev-server, but showing a warning
830fdb5 Adding --keep-public-path to dev-server to allow you to fully control the publicPath
b27f7c9 Reversing some of the changes we won't do for now, and adding the failing test
e206a12 fix issue in generated public path of manifest.json
eb5565b convert error into warning
910b6bc convert error into warning
@Jeyffrey
Copy link

Jeyffrey commented Apr 25, 2019

My project is based on a Docker and my container listen to the port 8088.
Symfony webpack-dev-server works by default on the port 8080.
In the docker-compose file, i just had to open the 8080 port in both sides :

services:
    ...
    my_container:
        ...
        ports:
            ...
            - "8080:8080".

And my Yarn script is now (according to this comment) :
"dev-server": "encore dev-server --host 0.0.0.0 --disable-host-check"

It just works now ! 🎉

@Kocal
Copy link
Contributor

Kocal commented Apr 25, 2019

You can also check symfony/symfony-docs#11422 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants