无法在Docker Container中运行AngularJS站点

时间:2015-07-27 05:16:54

标签: angularjs node.js docker boot2docker

我有一个角度站点,我试图在docker容器中运行(现在只能通过boot2docker)。

Dockerfile如下所示:

FROM ubuntu:14.04

RUN sudo apt-get update
RUN sudo apt-get install -y npm

# Set in what directory commands will run
WORKDIR /home/app

# Put all our code inside that directory that lives in the container
ADD . /home/app

RUN sudo npm install && \
    sudo npm install -g grunt-cli

#Need to do this for npm and bower
RUN ln -s /usr/bin/nodejs /usr/bin/node

# Install dependencies
RUN sudo npm install -g bower && \
    sudo npm install -g grunt-cli && \
    sudo npm install && \
    bower install --config.interactive=false --allow-root

EXPOSE 9000

# The command to run our app when the container is run
ENTRYPOINT ["npm","start"]

当我运行docker build -t web-app .时,图像构建正常。

我运行以下命令为容器加注星标

docker run --name my-web-app -p 9000:9000 -d web-app

如果我运行docker logs我可以看到它正在运行......

> sb-admin@0.0.0 start /home/app
> grunt serve

Running "serve" task

Running "clean:server" (clean) task
Cleaning .tmp...OK

Running "concurrent:server" (concurrent) task

    Running "copy:styles" (copy) task
    Copied 3 files

    Done, without errors.


    Execution Time (2015-07-27 05:04:02 UTC)
    loading tasks  2ms  ▇▇▇▇▇▇▇▇▇ 18%
    copy:styles    9ms  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 82%
    Total 11ms

Running "autoprefixer:dist" (autoprefixer) task
File .tmp/styles/main.css created.
File .tmp/styles/sb-admin-2.css created.
File .tmp/styles/timeline.css created.

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

Running "watch" task
Waiting...

如果我手动运行npm start(不在容器中,只在我的本地),则http://localhost:9000可以访问该网站,但是当我导航到IP=$(boot2docker ip)的IP地址时我拒绝连接。

如果我通过SSH连接到容器并运行以下任何一项,也会发生同样的情况:

IP=$(docker inspect -f "{{ .NetworkSettings.IPAddress }}" my-web-app)
curl -X GET $IP:9000

我觉得我已经运行了像这样的容器并且访问它们很好,所以我不确定我做错了什么。

1 个答案:

答案 0 :(得分:0)

在容器内部而不是localhost中工作时,应将主机名设置为0.0.0.0

例如,我们使用docker开发NPM,Bower& amp;的Angular应用程序。 Grunt构建堆栈。我们在开发过程中使用grunt serve任务。为了在容器中完成此工作,我们在container任务中创建了connect目标,以覆盖默认设置。

看起来与此类似:

connect: {
  options: {
    port: 9000,
    hostname: 'localhost',
    livereload: 35729
  },
  container: {
    options: {
      hostname: '0.0.0.0',
      open: true,
      middleware: function (connect) {
        return [
          connect.static('.tmp'),
          connect().use(
              '/bower_components',
              connect.static('./bower_components')
          ),
          connect.static(appConfig.app)
        ];
      }
    }
  },
  ...
 }

此处container目标是livereload目标的副本,但hostname覆盖。

根据您用于为应用程序提供服务的NPM模块,您可以配置主机名,或者可以在localhost上创建模块,也可以在本地计算机上侦听所有IP地址:{{ 1}}。