简体   繁体   English

如何在docker容器中运行livereload和gulp?

[英]How to run livereload with gulp within a docker container?

I created a docker container to run tasks with gulp. 我创建了一个docker容器来运行gulp任务。 All tasks are running, the problem is I can't enable livrereload in Chrome although I exposed the 35729 port in my container. 所有任务都在运行,问题是我无法在Chrome中启用livrereload,尽管我在容器中暴露了35729端口。

Here is the Dockerfile : 这是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 这是我的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.." 当我编辑一个js文件时,我可以在我的容器中看到文件被处理但是当我尝试在我的浏览器(Chrome)中启用实时重新加载时,我收到以下消息:“无法连接到LiveReload服务器..”

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. 在容器中公开端口并不意味着将在docker主机上打开端口。 You should be using the docker run -p option. 您应该使用docker run -p选项。 The documentation says: 文件说:

-p=[] : Publish a container᾿s port or a range of ports to the host -p = []:将容器端口或一系列端口发布到主机

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

Both hostPort and containerPort can be specified as a range of ports. hostPortcontainerPort都可以指定为一系列端口。

When specifying ranges for both, the number of container ports in the range must match the number > of host ports in the range. 为两者指定范围时,该范围内的容器端口数必须与该范围内的主机端口数>相匹配。 (eg, -p 1234-1236:1234-1236/tcp ) (use 'docker port' to see the actual mapping) (例如, -p 1234-1236:1234-1236/tcp )(使用' -p 1234-1236:1234-1236/tcp port'查看实际映射)

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. 由于您尝试了-p containerPort表单,因此当您运行docker run命令时,docker会随机选择在您的主机上打开的实际端口(Linux mint)。 To figure out what port was chosen, you have to use the docker port command. 要确定选择了哪个端口,必须使用docker port命令。

Since this is not convenient, you should use the -p hostPort:containerPort form, and specify that hostPort is 35729 . 由于这不方便,您应该使用-p hostPort:containerPort表单,并指定hostPort35729 (I also assume you expect ports 80, 8080 and 3000 to be accessible in the same manner) (我还假设您希望以相同的方式访问端口80,8080和3000)

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 . 处理端口的更简单方法是在主机网络模式下运行docker容器。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM