在 docker 中访问 grunt serve

access grunt serve inside docker

我制作了 2 docker 个镜像(nginx、yeoman)并映射端口如下。

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                           NAMES
3a71ee900cc0        webserver:0.1       "nginx"             About an hour ago   Up About an hour    443/tcp, 0.0.0.0:8080->80/tcp   webserver
af57b93ca326        silarsis/yeoman     "/bin/bash"         About an hour ago   Up About an hour    0.0.0.0:9000->9000/tcp          yeoman

并进入 yeoman docker 来制作 grunt 服务器。

yeoman_docker$ yo  ..scafolding stuff  
yeoman_docker$ ls  
Gruntfile.js README.md  app  bower.json  bower_components  node_modules  package.json  test  
yeoman_docker$ grunt serve 
Running "serve" task


    Done, without errors.


    Execution Time (2016-09-23 07:20:28 UTC-0)
    loading tasks                5ms  ▇▇▇▇▇▇▇ 19%
    loading grunt-contrib-copy  13ms  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 50%
    copy:styles                  8ms  ▇▇▇▇▇▇▇▇▇▇▇ 31%
    Total 26ms

Running "autoprefixer:server" (autoprefixer) task Autoprefixer's process() method is deprecated and will removed in next major release. Use postcss([autoprefixer]).process() instead File .tmp/styles/main.css created.

Running "connect:livereload" (connect) task Started connect web server on http://localhost:9000

Running "watch" task Waiting...

保留此 SSH 以维护 grunt 服务。 Nginx 可以从本地访问 (127.0.0.1:8080) 但 grunt 无法访问 (127.0.0.1:9000)。在本地制作 grunt 服务器和 docker 有什么区别吗?我应该用它做什么?

grunt服务器好像不在本地

$ telnet 127.0.0.1 9000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Connection closed by foreign host.

$ netstat -ant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 10.0.8.1:53             0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp6       0      0 :::9000                 :::*                    LISTEN
tcp6       0      0 :::8080                 :::*                    LISTEN
tcp6       0      0 :::21                   :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 127.0.0.1:9000          127.0.0.1:39682         TIME_WAIT

问题是您在 yeoman 容器中的连接服务器侦听容器内的本地主机:

Running "connect:livereload" (connect) task Started connect web server on http://localhost:9000

在使用 docker 创建端口绑定时,docker 容器内的服务必须侦听主机 0.0.0.0,而不是 localhost。您需要更新 gruntfile.js 以便连接服务器侦听 0.0.0.0.