加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

javascript – 如何在docker容器内运行grep的livereload?

发布时间:2020-12-16 03:29:53 所属栏目:安全 来源:网络整理
导读:我创建了一个docker容器来运行gulp的任务. 所有任务正在运行,问题是我无法在Chrome中启用livrereload,尽管我在我的容器中暴露了35729端口. 这是Dockerfile: FROM ubuntu:latestMAINTAINER jiboulexEXPOSE 80 8080 3000 35729RUN apt-get updateRUN apt-get

我创建了一个docker容器来运行gulp的任务.
所有任务正在运行,问题是我无法在Chrome中启用livrereload,尽管我在我的容器中暴露了35729端口.

这是Dockerfile:

FROM ubuntu:latest
MAINTAINER jiboulex

EXPOSE 80 8080 3000 35729

RUN apt-get update
RUN apt-get install curl -y
RUN apt-get install software-properties-common -y
RUN add-apt-repository ppa:chris-lea/node.js
RUN apt-get update
RUN apt-get install nodejs -y
RUN curl -L https://www.npmjs.com/install.sh | sh
RUN npm install --global gulp -y

# overwrite this with 'CMD []' in a dependent Dockerfile
CMD ["/bin/bash"]

我使用以下命令创建图像:

docker build -t gulp_image .

我创建一个容器

docker run --name=gulp_container -i -t  --rm  -v /var/www/my_app:/var/www/my_app:rw gulp_image bash

然后在我的容器

cd /var/www/my_app
gulp

这是我的Gulpfile.js

var gulp = require('gulp'),livereload = require('gulp-livereload'),exec = require('child_process').exec;
gulp.task('js',function() {
gulp.src([
    './src/js/*.js'
]).pipe(livereload());
});
gulp.task('watch',function(){
var onChange = function (event) {
    console.log('File '+event.path+' has been '+event.type);
};
livereload.listen();
gulp.watch([
    './src/js/*.js'
],['js'])
    .on('change',onChange);
});
gulp.task('default',['watch','js']);

当我编辑一个js文件时,我可以在容器中看到这些文件被处理,但是当我尝试在浏览器(Chrome)中启用实时重新加载时,我收到以下消息:“无法连接到LiveReload服务器..”

任何人都知道我错过了什么或没有做什么?
谢谢阅读 !

最佳答案
将端口暴露在容器中并不意味着端口将在docker主机上打开.您应该使用docker运行-p选项.文件说:

-p=[] : Publish a container?s port or a range of ports to the host

format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort

Both hostPort and containerPort can be specified as a range of ports.

When specifying ranges for both,the number of container ports in the range must match the number > of host ports in the range. (e.g.,-p 1234-1236:1234-1236/tcp)
(use ‘docker port’ to see the actual mapping)

由于您尝试使用-p containerPort表单,因此在运行docker run命令时,Docker会随机选择主机上打开的实际端口(Linux mint).要确定选择了哪个端口,您必须使用docker port命令.

由于这不方便,您应该使用-p hostPort:containerPort表单,并指定hostPort为35729.(我也假设您希望端口80,8080和3000可以以相同的方式访问)

那么运行容器的命令就是:

docker run --name=gulp_container -i -t --rm 
    -v /var/www/my_app:/var/www/my_app:rw 
    -p 35729:35729 
    -p 80:80 
    -p 8080:8080 
    -p 3000:3000 
    gulp_image bash

处理端口的一个更简单的方法是在host networking mode运行您的docker容器.在这种模式下,在容器上打开的任何端口实际上都是在主机网络接口上打开的(实际上它们共享相同的接口).

然后,您可以启动您的容器:

docker run --name=gulp_container -i -t --rm 
    -v /var/www/my_app:/var/www/my_app:rw 
    --net=host  
    gulp_image bash

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读