node.js - 在 WordPress docker 镜像上安装 NodeJS - Node : not found

标签 node.js docker

我正在尝试将nodejs和npm安装到基于官方wordpress镜像的docker镜像上,最终安装gulp。

我的 Dockerfile 看起来像这样

FROM wordpress:4.9.8-php7.1-apache
RUN apt-get update
RUN apt-get install -y nodejs
RUN node -v
RUN npm install -g gulp
RUN npm install gulp

当我构建镜像时,它似乎安装了nodejs,但随后失败了

Setting up nodejs (4.8.2~dfsg-1) ... update-alternatives: using /usr/bin/nodejs to provide /usr/bin/js (js) in auto mode update-alternatives: warning: skip creation of /usr/share/man/man1/js.1.gz because associated file /usr/share/man/man1/nodejs.1.gz (of link group js) doesn't exist

最终,我收到以下错误

/bin/sh: 1: node: not found

我不明白为什么会这样?

构建日志的完整输出如下

Building wordpress
Step 1/6 : FROM wordpress:4.9.8-php7.1-apache
 ---> 18747ca95580
Step 2/6 : RUN apt-get update
 ---> Running in 8b7c3e8d34d6
Ign:1 http://cdn-fastly.deb.debian.org/debian stretch InRelease
Get:2 http://cdn-fastly.deb.debian.org/debian stretch-updates InRelease [91.0 kB]
Get:3 http://cdn-fastly.deb.debian.org/debian stretch Release [118 kB]
Get:4 http://security.debian.org/debian-security stretch/updates InRelease [94.3 kB]
Get:5 http://cdn-fastly.deb.debian.org/debian stretch Release.gpg [2434 B]
Get:6 http://cdn-fastly.deb.debian.org/debian stretch-updates/main amd64 Packages [5148 B]
Get:7 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 Packages [7099 kB]
Get:8 http://security.debian.org/debian-security stretch/updates/main amd64 Packages [385 kB]
Fetched 7794 kB in 3s (1984 kB/s)
Reading package lists...
Removing intermediate container 8b7c3e8d34d6
 ---> 1bcbd1ee66ef
Step 3/6 : RUN apt-get install -y nodejs
 ---> Running in 9b732a5af611
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  libuv1
The following NEW packages will be installed:
  libuv1 nodejs
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 3524 kB of archives.
After this operation, 14.5 MB of additional disk space will be used.
Get:1 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 libuv1 amd64 1.9.1-3 [84.4 kB]
Get:2 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 nodejs amd64 4.8.2~dfsg-1 [3440 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 3524 kB in 1s (2812 kB/s)
Selecting previously unselected package libuv1:amd64.
(Reading database ... 13068 files and directories currently installed.)
Preparing to unpack .../libuv1_1.9.1-3_amd64.deb ...
Unpacking libuv1:amd64 (1.9.1-3) ...
Selecting previously unselected package nodejs.
Preparing to unpack .../nodejs_4.8.2~dfsg-1_amd64.deb ...
Unpacking nodejs (4.8.2~dfsg-1) ...
Setting up libuv1:amd64 (1.9.1-3) ...
Processing triggers for libc-bin (2.24-11+deb9u3) ...
Setting up nodejs (4.8.2~dfsg-1) ...
update-alternatives: using /usr/bin/nodejs to provide /usr/bin/js (js) in auto mode
update-alternatives: warning: skip creation of /usr/share/man/man1/js.1.gz because associated file /usr/share/man/man1/nodejs.1.gz (of link group js) doesn't exist
Removing intermediate container 9b732a5af611
 ---> bbf198ce7820
Step 4/6 : RUN node -v
 ---> Running in 39269d307aa6
/bin/sh: 1: node: not found

更新

我的 docker 文件现在看起来像这样

FROM wordpress:4.9.8-php7.1-apache
RUN apt-get update 
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - && apt-get install -y nodejs

RUN nodejs -v
RUN npm -v

使用 docker build . --no-cache 运行此命令结果如下。

总结:Node是v4.8.2,找不到npm

Step 1/6 : FROM wordpress:4.9.8-php7.1-apache
 ---> 18747ca95580
Step 2/6 : RUN apt-get update
 ---> Running in 9bcb0285ff3f
Get:1 http://security.debian.org/debian-security stretch/updates InRelease [94.3 kB]
Ign:2 http://cdn-fastly.deb.debian.org/debian stretch InRelease
Get:3 http://cdn-fastly.deb.debian.org/debian stretch-updates InRelease [91.0 kB]
Get:4 http://cdn-fastly.deb.debian.org/debian stretch Release [118 kB]
Get:5 http://cdn-fastly.deb.debian.org/debian stretch-updates/main amd64 Packages [5148 B]
Get:6 http://cdn-fastly.deb.debian.org/debian stretch Release.gpg [2434 B]
Get:7 http://security.debian.org/debian-security stretch/updates/main amd64 Packages [386 kB]
Get:8 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 Packages [7099 kB]
Fetched 7796 kB in 4s (1592 kB/s)
Reading package lists...
Removing intermediate container 9bcb0285ff3f
 ---> 7fb346c1196b
Step 3/6 : RUN apt-get install -y nodejs
 ---> Running in 6b42686c1c69
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  libuv1
The following NEW packages will be installed:
  libuv1 nodejs
0 upgraded, 2 newly installed, 0 to remove and 1 not upgraded.
Need to get 3524 kB of archives.
After this operation, 14.5 MB of additional disk space will be used.
Get:1 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 libuv1 amd64 1.9.1-3 [84.4 kB]
Get:2 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 nodejs amd64 4.8.2~dfsg-1 [3440 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 3524 kB in 1s (2250 kB/s)
Selecting previously unselected package libuv1:amd64.
(Reading database ... 13068 files and directories currently installed.)
Preparing to unpack .../libuv1_1.9.1-3_amd64.deb ...
Unpacking libuv1:amd64 (1.9.1-3) ...
Selecting previously unselected package nodejs.
Preparing to unpack .../nodejs_4.8.2~dfsg-1_amd64.deb ...
Unpacking nodejs (4.8.2~dfsg-1) ...
Setting up libuv1:amd64 (1.9.1-3) ...
Processing triggers for libc-bin (2.24-11+deb9u3) ...
Setting up nodejs (4.8.2~dfsg-1) ...
update-alternatives: using /usr/bin/nodejs to provide /usr/bin/js (js) in auto mode
update-alternatives: warning: skip creation of /usr/share/man/man1/js.1.gz because associated file /usr/share/man/man1/nodejs.1.gz (of link group js) doesn't exist
Removing intermediate container 6b42686c1c69
 ---> cd6622204549
Step 4/6 : RUN nodejs -v
 ---> Running in 103bfc7b3396
v4.8.2
Removing intermediate container 103bfc7b3396
 ---> 978bd59b62df
Step 5/6 : RUN npm -v
 ---> Running in 9762ed0c24b4
/bin/sh: 1: npm: not found
The command '/bin/sh -c npm -v' returned a non-zero code: 127

最佳答案

对于 Ubuntu,node.js 可执行文件名为 nodejs,而不是 node

$ cat Dockerfile
FROM wordpress:4.9.8-php7.1-apache
RUN apt-get update
RUN apt-get install -y nodejs
RUN nodejs -v

docker build .
Sending build context to Docker daemon  2.048kB
Step 1/6 : FROM wordpress:4.9.8-php7.1-apache
 ---> 18747ca95580
Step 2/6 : RUN apt-get update
 ---> Using cache
 ---> 416b334f6ea7
Step 3/6 : RUN apt-get install -y nodejs
 ---> Using cache
 ---> 0606aac728ee
Step 4/6 : RUN nodejs -v
 ---> Running in c300e7140695
v4.8.2

但是,您得到的 Node 版本非常旧。您可以尝试这种安装 Node v8 (the current LTS) 的替代方法通过 Node 的存储库

FROM wordpress:4.9.8-php7.1-apache
RUN apt-get update && apt-get -y install gnupg2
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - && apt-get install -y nodejs
RUN nodejs -v
RUN npm install -g gulp

关于node.js - 在 WordPress docker 镜像上安装 NodeJS - Node : not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51711820/

相关文章:

node.js - WebStorm 远程解释器无法与 TSLint 一起使用

docker - Docker中的503服务暂时不可用nginx/1.13.3

node.js - 在管道传输之前捕获 super 代理请求错误

javascript - 如何在javascript中动态增加变量名称?

docker - docker-compose up 时出现 "Address already in use"错误

windows -/var/run/docker.sock 如何适用于 Windows Docker?

docker - 无法使用静态 IP 在覆盖网络上创建 docker 容器

javascript - 使用 gulp 展平父目录但不展平子目录

node.js - 如何在windows中使用NGINX为node js配置HTTPS

javascript - 使用 Node http 发送和接收纯文本而不是对象