如何在docker容器内运行grep的livereload? [英] How to run livereload with gulp within a docker container?

查看:262
本文介绍了如何在docker容器内运行grep的livereload?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是Dockerfile:

  FROM ubuntu:最新
维护者jiboulex

EXPOSE 80 8080 3000 35729

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

#在依赖Dockerfile中用'CMD []'覆盖这个
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 =要求( '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。类型);
};
livereload.listen();
gulp.watch([
'./src/js/*.js'
],[ 'js'])
.on('change',onChange);
});
gulp.task('default',['watch','js']);

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



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

解决方案

将容器中的端口隐藏并不意味着端口将在码头打开主办。您应该使用 docker run -p 选项。文档说:


-p = []:发布容器端口或一系列端口到主机



格式: ip:hostPort:containerPort ip :: containerPort | hostPort:containerPort | containerPort



hostPort containerPort 可以指定为一系列端口。



当指定两者的范围时,范围中的容器端口数必须与number>主机端口。 (例如, -p 1234-1236:1234-1236 / tcp
(使用'docker port'查看实际映射)


由于您尝试了 -p containerPort 表单,主机上打开的实际端口(Linux mint)当您运行 docker run 命令时,由docker随机选择。要确定选择了哪个端口,您必须使用 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容器。在这种模式下,容器上打开的任何端口实际上都是在主机网络接口上打开的(实际上它们都是共享相同的接口)。



容器与:

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


I created a docker container to run tasks with gulp. All tasks are running, the problem is I can't enable livrereload in Chrome although I exposed the 35729 port in my container.

Here is the 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"]

I create the image with the following command :

docker build -t gulp_image .

I create a container :

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

then in my container

cd /var/www/my_app
gulp

Here is my 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']);

When I edit a js file, I can see in my container that the files are processed but when I try to enable live reload in my browser (Chrome), I got the following message : "Could not connect to LiveReload server.."

Anyone got a clue about what I missed or didn't do ? Thanks for reading !

解决方案

Exposing ports in a container does not imply that the ports will be opened on the docker host. You should be using the docker run -p option. The documentation says:

-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)

Since you tried the -p containerPort form, the actual port opened on your host (Linux mint) was randomly chosen by docker when you run the docker run command. To figure out what port was chosen, you have to use the docker port command.

Since this is not convenient, you should use the -p hostPort:containerPort form, and specify that hostPort is 35729. (I also assume you expect ports 80, 8080 and 3000 to be accessible in the same manner)

The command to run your container would then be:

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

An easier way to deal with ports is to run your docker container in host networking mode. In this mode, any port opened on the container is in fact opened on the host network interface (they are actually both sharing the same interface).

You would then start your container with:

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

这篇关于如何在docker容器内运行grep的livereload?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆