問題描述
我創(chuàng)建了一個 docker 容器來使用 gulp 運行任務(wù).所有任務(wù)都在運行,問題是我無法在 Chrome 中啟用 livrereload,盡管我在容器中暴露了 35729 端口.
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.
這里是 Dockerfile:
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"]
我使用以下命令創(chuàng)建圖像:
I create the image with the following command :
docker build -t gulp_image .
我創(chuàng)建了一個容器:
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
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']);
當(dāng)我編輯一個 js 文件時,我可以在我的容器中看到文件已被處理,但是當(dāng)我嘗試在我的瀏覽器 (Chrome) 中啟用實時重新加載時,我收到以下消息:無法連接到 LiveReload 服務(wù)器.."
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 !
推薦答案
在容器中暴露端口并不意味著將在 docker 主機上打開端口.您應(yīng)該使用 docker run
-p
選項.文檔說:
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=[] : 將容器的端口或一系列端口發(fā)布到主機
-p=[] : Publish a container?s port or a range of ports to the host
格式: ip:hostPort:containerPort
|ip::containerPort
|hostPort:containerPort
|containerPort
hostPort
和 containerPort
都可以指定為一個端口范圍.
Both hostPort
and containerPort
can be specified as a range of ports.
為兩者指定范圍時,范圍內(nèi)的容器端口數(shù)必須與范圍內(nèi)的主機端口數(shù)匹配.(例如,-p 1234-1236:1234-1236/tcp
)(使用 'docker port' 查看實際映射)
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
形式,當(dāng)您運行 docker run
命令時,docker 會隨機選擇在您的主機(Linux mint)上打開的實際端口.要確定選擇了哪個端口,您必須使用 docker 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. To figure out what port was chosen, you have to use the docker port
command.
由于不方便,所以應(yīng)該使用-p hostPort:containerPort
形式,并指定hostPort
為35729
.(我還假設(shè)您希望端口 80、8080 和 3000 可以以相同的方式訪問)
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)
運行容器的命令是:
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
處理端口的更簡單方法是在 主機網(wǎng)絡(luò)模式下運行 docker 容器.在這種模式下,在容器上打開的任何端口實際上都是在主機網(wǎng)絡(luò)接口上打開的(它們實際上都是共享同一個接口).
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
這篇關(guān)于如何在 docker 容器中使用 gulp 運行 livereload?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!