Reputation: 342
I am using DDEV as my local hosting environment, and many of my projects implement front end automation via Gulp. Browsersync is a major component to our front end set-up, and requires ports to be exposed by the DDEV container to the host machine. The problem at hand is two fold, what is the best way to expose a port to the host machine from the container, and what is the best set-up for a Browser-Sync Gulp task in a DDEV environment?
Upvotes: 5
Views: 3982
Reputation: 342
Edit 2024-08-20: Most people use the excellent ddev-browsersync add-on these days to work with Browsersync, and skip all of the below.
They also normally use the web_extra_exposed_ports
feature of DDEV instead of fiddling with HTTPS_EXPOSE.
(Original answer follows)
Part one of this situation requires using a Docker Compose file to expose your container's port to the hose machine. Based on this Answer you need to create a docker-compose.browsersync.yaml
file in your .ddev
directory.
An example of that file for Browser-Sync would be as follows:
# Override the web container's standard HTTP_EXPOSE and HTTPS_EXPOSE
# This is to expose the browsersync port.
services:
web:
# ports are a list of exposed *container* ports
expose:
- '3000'
environment:
- HTTP_EXPOSE=${DDEV_ROUTER_HTTP_PORT}:80,${DDEV_MAILHOG_PORT}:8025,3001:3000
- HTTPS_EXPOSE=${DDEV_ROUTER_HTTPS_PORT}:80,${DDEV_MAILHOG_HTTPS_PORT}:8025,3000:3000
We expose Port 3000 here because it is the default for Browser-Sync, but could be updated to reflect the needs of your projects.
Note: After adding this file to your .ddev
directory you should restart your ddev project.
For more information on defining new services with docker compose, read the DDEV docs.
This assumes you have a working gulpfile.js
ready to do with your other required packages included already. If you're not fully familiar with Browser-Sync and Gulp, please refer to their docs for full details.
const browserSync = require('browser-sync').create();
/*
* Set 'url' (the host and proxy hostnames) to match the canonical url of your ddev project.
* Do not include the http/s protocol in the url. The ddev-router will route the
* host machine's request to the appropriate protocol.
*
* ex: yoursite.ddev.site
*/
const url = 'yoursite.ddev.site';
/*
* This function only includes the most basic browser-sync settings needed
* to get a watch server running. Please refer to the browser-sync docs for other
* available options.
*/
const startServer = function (done) {
// Initialize BrowserSync
browserSync.init({
proxy: url,
host: url,
port: 3000,
});
done();
};
exports.startServer = startServer;
You can test this using gulp startServer
after initial set-up. Gulp will output a http
as the External URL for testing. However thanks to the ddev-router it can be accessed via http
or https
.
Upvotes: 7
Reputation: 111
The above tips and from other posts like:
Using Docker Compose to expose DDEV web container ports to host machine
Chrome can't open localhost:3000 with Gulp / BrowserSync
may be fine for a website on the HTTP protocol but not for one on HTTPS.
Specifically, I am testing a CMS installation in DDEV, with secure URLs in the Frontend/Backend. After running the ddev start
command, it is displayed that I access it in the browser using https://myproject.ddev.site
or https://127.0.0.1:62751
(the socket changes with each run of DDEV).
I didn't install drud/ddev-browsersync
package because it doesn't proxy to HTTPS, in my case it didn't help me at all.
In a directory I created the gulpfile.js file to perform complex tasks on scss, js, css, html files. In order not to manually reload the page in the browser, I obviously need BrowserSync. The only configuration that worked is the following:
const browserSync = require('browser-sync').create();
function startServer () {
browserSync.init({
proxy: 'https://127.0.0.1:62751',
});
};
exports.startServer = startServer;
When I run the gulp startServer
command, first https://localhost:3002
is displayed in the address bar, then after a while it redirects to https://myproject.ddev.site
. From this moment on, I make any changes to the files.
The only issue I have is that after each run of the ddev start
command I have to manually change the port in the line https://127.0.0.1:XXXXX
. If DDEV provides this value I could use it and then the issue is solved.
Upvotes: 0
Reputation: 980
I was unable to run browsersync from within the container. I updated my local system and ran browsersync locally
I used Stephen's solution elsewhere on this page (https://stackoverflow.com/a/70190271/18779 for quick reference).
I am running a Mac so I also did the following:
cd [subtheme dir]
and npm run watch
Upvotes: 0
Reputation: 1
If you are a mac user and browsersync still doesn't work with the above solution, you might need to update your /etc/hosts file as I had to.
You can check more details in this answer.
Upvotes: 0
Reputation: 11
piggy-backing on the answer from @TXChetG and the answer from @Mario Hernandez
create a file called "docker-compose.browsersync.yaml" inside your hidden /.ddev/ config folder with code below, tabbed properly (which is very important), then run the command "ddev restart"
# Override the web container standard HTTP_EXPOSE and HTTPS_EXPOSE
# This is to expose the browsersync port.
version: '3.6'
services:
web:
# ports are a list of exposed *container* ports
expose:
- '3000'
environment:
- HTTP_EXPOSE=${DDEV_ROUTER_HTTP_PORT}:80
- HTTPS_EXPOSE=${DDEV_ROUTER_HTTPS_PORT}:80
Upvotes: 1