无法在docker容器中安装npm?

编程入门 行业动态 更新时间:2024-10-12 01:24:56
本文介绍了无法在docker容器中安装npm?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am deploying a simple node.js app on a digital ocean server 's docker platform.

// package.json

// 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

// 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 localhost:' + PORT);

// docker文件

// docker file

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

docker基本镜像是dockerfile / nodejs,它已经构建了一个node.js,我构建了该镜像:

The docker base image is the dockerfile/nodejs, which has built a node.js, I built the image:

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

并运行映像:

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

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

By running the image, I can successfully into the container, the app files are all copied into the container. when I tried the command to install the node.js dependency:

npm install

但是,npm无法下载所有软件包,但出现错误:

However, npm can't download all the packages, with a error:

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'

推荐答案

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

Make sure the dns is set properly. I had some issues that were gone after the docker service restart. If it didn't help you may want to use the --dns 8.8.8.8 docker switch.

重新启动docker服务:

Restarting docker service:

  • 基于systemd架构- sudo systemctl restart docker
  • boot2docker- boot2docker restart
  • Docker Machine- docker-machine restart< machine_name>
  • on systemd architecture - sudo systemctl restart docker
  • boot2docker - boot2docker restart
  • Docker Machine - docker-machine restart <machine_name>

我也做了类似的事情(nodejs图像),但是我使用了另一个基础图片,请随时使用我的 repo 中的任何内容。

Also, I did something similar (nodejs image), but I've used another base image, feel free to use whatever you need from my repo.

更多推荐

无法在docker容器中安装npm?

本文发布于:2023-10-29 00:19:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538179.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容器   docker   npm

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!