如何在 docker 容器中使用 gulp 运行 livereload?
How to run livereload with gulp within a docker container?
我使用 gulp 创建了一个 docker 容器用于 运行 任务。
所有任务都是 运行ning,问题是我无法在 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),我收到以下消息:"Could not connect to LiveReload server.."
有人知道我错过了什么或没有做什么吗?
感谢阅读!
在容器中公开端口并不意味着这些端口将在 docker 主机上打开。您应该使用 docker run
-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 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
处理端口的更简单方法是 运行 您的 docker 容器在 host networking mode 中。在这种模式下,容器上打开的任何端口实际上都是在主机网络接口上打开的(它们实际上共享同一个接口)。
然后您将启动您的容器:
docker run --name=gulp_container -i -t --rm \
-v /var/www/my_app:/var/www/my_app:rw \
--net=host \
gulp_image bash
我使用 gulp 创建了一个 docker 容器用于 运行 任务。 所有任务都是 运行ning,问题是我无法在 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),我收到以下消息:"Could not connect to LiveReload server.."
有人知道我错过了什么或没有做什么吗? 感谢阅读!
在容器中公开端口并不意味着这些端口将在 docker 主机上打开。您应该使用 docker run
-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
andcontainerPort
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 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
处理端口的更简单方法是 运行 您的 docker 容器在 host networking mode 中。在这种模式下,容器上打开的任何端口实际上都是在主机网络接口上打开的(它们实际上共享同一个接口)。
然后您将启动您的容器:
docker run --name=gulp_container -i -t --rm \
-v /var/www/my_app:/var/www/my_app:rw \
--net=host \
gulp_image bash