Gulp Docker:Browsersync无法正常工作

bxfogqkk  于 12个月前  发布在  Gulp
关注(0)|答案(2)|浏览(217)

我在使用Gulp、Browsersync和Docker时遇到了Node.js应用程序的问题。
当在本地运行gulp watch时,一切都很好。
但是,当使用docker-compose up时,我得到一个错误
无法获取/
端口3001上的Browsersync UI按预期工作。
在本地运行应用程序:

gulp watch

字符串
x1c 0d1x的数据
在Docker容器中运行:

docker-compose up


我得到错误:
无法获取/



Web浏览器控制台中的错误消息:
拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“default-src 'self'”。启用内联执行需要“unsafe-inline”关键字、散列(“sha 256-ZfFIHrd 9 MifrQdadZrM 3 hznhYlx + PRQo 8 +OCWjaPDaY =')或随机数(”nonce-...“)。另请注意,未显式设置”script-src ",因此使用“default-src”作为回退
Docker compose logs:它不会产生任何错误,只是记录与我在本地使用gulp watch运行时相同的输出。

docker-compose up

[+] Building 0.0s (0/0)                                                                                                                                  docker:desktop-linux
WARN[0000] Found orphan containers ([boilerplate-app-1]) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
[+] Running 1/0
 ✔ Container boilerplate-node-1  Created                                                                                                                                 0.0s
Attaching to boilerplate-node-1
boilerplate-node-1  | [07:52:39] Using gulpfile /Users/filiphuhta/Documents/Projects/Bitbucket/boilerplate/gulpfile.js
boilerplate-node-1  | [07:52:39] Starting 'watch'...
boilerplate-node-1  | [07:52:39] Starting 'cleanDist'...
boilerplate-node-1  | [07:52:39] Finished 'cleanDist' after 1.61 ms
boilerplate-node-1  | [07:52:39] Starting 'buildScripts'...
boilerplate-node-1  | [07:52:39] Starting 'lintScripts'...
boilerplate-node-1  | [07:52:39] Starting 'buildStyles'...
boilerplate-node-1  | [07:52:39] Starting 'copyFiles'...
boilerplate-node-1  | Browserslist: caniuse-lite is outdated. Please run:
boilerplate-node-1  |   npx update-browserslist-db@latest
boilerplate-node-1  |   Why you should do it regularly: https://github.com/browserslist/update-db#readme
boilerplate-node-1  | [07:52:40] Finished 'copyFiles' after 413 ms
boilerplate-node-1  | [07:52:40] Finished 'buildScripts' after 417 ms
boilerplate-node-1  | [07:52:40] Finished 'lintScripts' after 417 ms
boilerplate-node-1  | [07:52:40] Finished 'buildStyles' after 419 ms
boilerplate-node-1  | [07:52:40] Starting 'startServer'...
boilerplate-node-1  | [07:52:40] Finished 'startServer' after 28 ms
boilerplate-node-1  | [07:52:40] Starting 'watchSource'...
boilerplate-node-1  | [07:52:40] Finished 'watchSource' after 1.43 ms
boilerplate-node-1  | [07:52:40] Finished 'watch' after 454 ms
boilerplate-node-1  | [Browsersync] Access URLs:
boilerplate-node-1  |  -----------------------------------
boilerplate-node-1  |        Local: http://localhost:3000
boilerplate-node-1  |     External: http://172.18.0.2:3000
boilerplate-node-1  |  -----------------------------------
boilerplate-node-1  |           UI: http://localhost:3001
boilerplate-node-1  |  UI External: http://localhost:3001
boilerplate-node-1  |  -----------------------------------
boilerplate-node-1  | [Browsersync] Serving files from: assets/
boilerplate-node-1  | [Browsersync] Couldn't open browser (if you are using BrowserSync in a headless environment, you might want to set the open option to false)

gulpfile.js

let startServer = function (done) {
    if (!settings.reload) return done();

    // Initialize BrowserSync
    browserSync.init({
        server: {
            baseDir: paths.reload,
            directory: true
        },
        proxy: "node:3000",
    });
    done();
};

Dockerfile:

FROM node:20

WORKDIR /usr/src/app
COPY gulpfile*.js ./
COPY package*.json ./
RUN npm install --global gulp-cli
RUN npm install

EXPOSE 3000 3001

CMD ["gulp watch"]

docker-compose.yml:

version: "1"
services:
  node:
    build: .
    command: gulp watch
    ports:
      - "3000:3000"
      - "3001:3001"


浏览器同步UI在端口3001上工作正常:

我不太擅长配置Docker容器,所以这肯定是我错过的东西。当我在机器上使用docker容器而不是本地时,Cannot GET /的问题是什么?
谢谢您的帮助!

nkcskrwz

nkcskrwz1#

您的Docker设置在使用BrowserSync运行Node.js应用时遇到了“Cannot GET /“错误。

Local Machine:
Browser ─> Gulp Watch ─> BrowserSync ─> Node.js App

Docker Container:
Browser ─> Docker ─> Gulp Watch ─> BrowserSync ─> Node.js App

字符串
BrowserSync server在Docker中运行时无法正确提供文件。
您可能需要更改BrowserSync配置中的代理设置。node:3000代理似乎不正确,因为Docker可能需要docker-compose.yml文件中的服务名称。如果node是您的服务名称,这很好,但请确保Node.js应用程序正在运行并可在容器内的端口3000上访问。
确保文件被正确复制到Docker容器中。您可能需要在docker-compose.yml中定义一个volume,以确保本地文件得到服务:

services:
 | node:
 | volumes:
 | - .:/usr/src/app


您的Dockerfile仅复制gulpfile.jspackage.json。请确保将整个项目目录(或至少运行应用程序所需的文件)复制到Docker容器中。

# Add this line to your Dockerfile
COPY . .


但是请确保将node_modules目录从复制到容器中排除。您可以通过将.dockerignore文件添加到项目目录来完成此操作:

node_modules/
npm-debug.log
.git/
.gitignore
.env
Dockerfile
docker-compose.yml
**/.DS_Store
**/Thumbs.db


Web浏览器控制台中的错误消息指示Content Security Policy (CSP)问题。如果您设置了CSP,请确保它允许运行必要的脚本。
你的docker-compose.yml是:

version: "1"
services:
  node:
    build: .
    command: gulp watch
    ports:
      - "3000:3000"
      - "3001:3001"
    volumes:
      - .:/usr/src/app


Dockerfile

FROM node:20

WORKDIR /usr/src/app
COPY . .
RUN npm install --global gulp-cli
RUN npm install

EXPOSE 3000 3001

CMD ["gulp watch"]

cwxwcias

cwxwcias2#

检查Docker容器中的BrowserSync配置:

let startServer = function (done) {
    if (!settings.reload) return done();

    // Inisialisasi BrowserSync
    browserSync.init({
        server: {
            baseDir: paths.reload,
            directory: true
        },
        proxy: "node:3000",
    });

    console.log("Configuration BrowserSync:", browserSync.config);

    done();
};

字符串
gulpfile.js中添加一些调试输出,以便在Docker容器中运行时打印BrowserSync配置。这可以帮助您验证配置是否正确并满足您的期望。

相关问题