无法在 docker 容器中安装 npm?

Posted

技术标签:

【中文标题】无法在 docker 容器中安装 npm?【英文标题】:can't install npm in the docker container? 【发布时间】:2015-03-15 12:59:12 【问题描述】:

我正在数字海洋服务器的 docker 平台上部署一个简单的 node.js 应用程序。

// package.json


  "name": "docker-centos-hello",
  "private": true,
  "version": "0.0.1",
  "description": "Node.js Hello world app on CentOS using docker",
  "author": "Daniel Gasienica <daniel@gasienica.ch>",
  "dependencies": 
    "express": "3.2.4"
  

// app.js

var express = require('express');
var PORT = 3000;
var app = express();
app.get('/', function (req, res) 
  res.send('Hello world\n');
);  
app.listen(PORT);
console.log('Running on http://localhost:' + PORT);

// 泊坞窗文件

FROM dockerfile/nodejs
# Set the working directory
WORKDIR   /src
EXPOSE  3000
CMD ["/bin/bash"]

docker基础镜像是dockerfile/nodejs,里面已经构建了一个node.js,我构建了镜像:

docker build -t test1 /home/sizhe/docker/test

并运行图像:

docker run -it -p 8080:3000 -v /home/sizhe/docker/test1:/src test

通过运行镜像,我可以成功进入容器,应用文件全部复制到容器中。当我尝试安装 node.js 依赖项的命令时:

npm install

但是npm无法下载所有包,报错:

Linux 3.13.0-40-generic
npm ERR! argv "node" "/usr/local/bin/npm" "install"
npm ERR! node v0.10.35
npm ERR! npm  v2.1.16
npm ERR! code ENOTFOUND
npm ERR! errno ENOTFOUND
npm ERR! syscall getaddrinfo

npm ERR! network getaddrinfo ENOTFOUND
npm ERR! network This is most likely not a problem with npm itself
npm ERR! network and is related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network 
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly.  See: 'npm help config'

【问题讨论】:

【参考方案1】:

确保 dns 设置正确。在docker 服务重新启动后,我遇到了一些问题。如果没有帮助,您可能需要使用 --dns 8.8.8.8 docker 开关。

重启docker服务:

关于 systemd 架构 - sudo systemctl restart docker boot2docker - boot2docker restart Docker 机器 - docker-machine restart &lt;machine_name&gt;

另外,我做了类似的事情(nodejs 图像),但我使用了另一个基础图像,请随意使用我的repo 中的任何你需要的东西。

【讨论】:

谢谢!重新启动 docker (boot2docker restart) 对我有用。 适用于 Docker Machine @Leons 建议对我有用:docker-machine restart &lt;machine_name&gt;

以上是关于无法在 docker 容器中安装 npm?的主要内容,如果未能解决你的问题,请参考以下文章

如何在高山容器中安装 Docker?

无法访问Docker容器中安装的Wordpress

由于从 `https://pypi.python.org/simple/` 返回的“永久移动”错误,`pip` 无法在 Docker 容器中安装包

在 docker 容器中安装 PostgreSQL

docker 在容器中安装yum等软件

如何在ubunto中安装docker