NodeJS在Docker上安装

时间:2019-03-08 20:56:34

标签: node.js linux azure docker odbc

我正在尝试在Docker上运行我的应用程序。我正在使用的一种图书馆是https://www.npmjs.com/package/odbc。 为了安装该库,我需要满足odbc自述文件中所述的要求:

  • unixODBC二进制文件和开发库,用于模块编译
    • 在Ubuntu / Debian上sudo apt-get install unixodbc unixodbc-dev
    • 在RedHat / CentOS上sudo yum安装unixODBC unixODBC-devel
  • 目标数据库的odbc驱动程序
  • 正确配置了odbc.ini和odbcinst.ini。

根据Microsoft文档,为SQL Server https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-2017#ubuntu-1604-1安装ODBC驱动程序13 我设法将所有内容本地安装在Mac上,并成功与Azure上的SQL Server连接,但是在Docker上安装它们然后在VSTS上运行时仍然存在一些问题。 我的Dockerfile:

FROM ubuntu:16.04
USER root
RUN apt-get update
RUN apt-get install --yes curl
RUN curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
RUN apt-get install --yes nodejs
RUN apt-get install --yes build-essential
RUN apt-get install -y npm 
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get install -y build-essential
RUN apt-get install -y make
RUN apt-get install apt-transport-https
RUN apt-get update && ACCEPT_EULA=Y apt-get install -y msodbcsql unixodbc-dev
ADD . /var/www/app
WORKDIR /var/www/app
RUN npm install && \
    npm cache clean --force
RUN npm run build 
EXPOSE 3000:80
CMD ["npm", "start"]

但是到目前为止,在按照以下方式安装NodeJS方面存在问题

RUN curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -

错误:/bin/sh: 1: sudo: not found 我试图只安装驱动程序,而要安装NodeJ,请使用一些现有的Docker映像:

FROM ubuntu:16.04
USER root
RUN apt-get update
RUN apt-get install --yes curl
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get install -y build-essential
RUN apt-get install -y make
RUN apt-get install apt-transport-https
RUN apt-get update && ACCEPT_EULA=Y apt-get install -y msodbcsql unixodbc-dev

FROM node:9-alpine
ADD . /var/www/app
WORKDIR /var/www/app
RUN npm install && \
    npm cache clean --force
RUN npm run build 
EXPOSE 3000:80

但是这种方法会引发错误:

gyp ERR! configure error 
gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.
gyp ERR! stack     at PythonFinder.failNoPython (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:483:19)
gyp ERR! stack     at PythonFinder.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:397:16)
gyp ERR! stack     at F (/usr/local/lib/node_modules/npm/node_modules/which/which.js:68:16)
gyp ERR! stack     at E (/usr/local/lib/node_modules/npm/node_modules/which/which.js:80:29)
gyp ERR! stack     at /usr/local/lib/node_modules/npm/node_modules/which/which.js:89:16
gyp ERR! stack     at /usr/local/lib/node_modules/npm/node_modules/which/node_modules/isexe/index.js:42:5
gyp ERR! stack     at /usr/local/lib/node_modules/npm/node_modules/which/node_modules/isexe/mode.js:8:5
gyp ERR! stack     at FSReqWrap.oncomplete (fs.js:170:21)
gyp ERR! System Linux 4.9.125-linuxkit
gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "build"
gyp ERR! cwd /var/www/app/node_modules/odbc
gyp ERR! node -v v9.11.2
gyp ERR! node-gyp -v v3.6.2
gyp ERR! not ok 
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.7 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.7: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! odbc@1.4.5 install: `node-gyp configure build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the odbc@1.4.5 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2019-03-08T20_51_17_496Z-debug.log

1 个答案:

答案 0 :(得分:2)

您正在处理ubuntu:16.04图像,并且实际上做了NodeJS家伙已经完成的很多工作。

如果我是你,我会去图片node:10-stretch-slim。然后使用apt-get安装所需的驱动程序(如果可用,否则编写下载脚本并安装在Dockerfile中)。

sudo命令通常不安装在Docker映像上,因为默认情况下,用户在容器会话中为root。如果您发现与sudo有关的任何错误,通常可以从导致问题的命令行中删除sudo

可能的解决方案

在这里更新我的答案,并为您提供可能的解决方案。

此解决方案会将您的应用程序放置在基于debian Stretch 9的节点10映像中。它将从debian 9 microsoft存储库中为您获取数据库驱动程序,并从您的计算机上安装所有您需要的软件包问题。

我还在脚本底部添加了ENTRYPOINTCMD。但是这些行是猜测,因为您的问题没有说明您实际如何启动应用程序。 (如果添加的话,我将更新我的答案)。

注意。请注意,我正在将--host 0.0.0.0传递给npm run start命令。这是为了避免将实时服务器绑定到本地主机,因为本地主机将无法从容器外部进行访问。除非您使用--network="host"启动容器。

您可能还有另一种启动应用程序的方法,该方法比实时开发服务器更“生产级”。如果是这样,只需替换Dockerfile底部的行,或在这个答案上问我。

Dockerfile

# from debian stretch 9.8, node 10
FROM node:10-stretch-slim

# get apt-transport-https, etc., so that we can install by https protocol
RUN apt-get update \
 && apt-get install -y \
      apt-transport-https \
      build-essential \
      make

# add and accept the microsoft signature
RUN curl -q https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
# retrieve the microsoft packagelist for debian 9
RUN curl -q https://packages.microsoft.com/config/debian/9/prod.list > /etc/apt/sources.list.d/mssql-release.list

# install the ms odbc sql driver and unixodbc header stuff
RUN apt-get update \
 && ACCEPT_EULA=Y apt-get install -y \
      msodbcsql17 \
      unixodbc-dev \
 && rm -rf /var/lib/apt/lists

# expose port 80 in containers of this image
EXPOSE 80

# copy working directory into the image and set as pwd
ADD . /var/www/app
WORKDIR /var/www/app

# install dependencies for the application
RUN npm install \
 && npm cache clean --force

# build the application
RUN npm run build

# i am just guessing how you want your app started here, npm?
ENTRYPOINT ["npm"]
# and then this, which makes "npm run start --host 0.0.0.0"
CMD ["run", "start", "--host", "0.0.0.0"]

使用以下图像构建图像:

docker build -t mynodeapp:0.1 .

使用以下命令运行应用程序映像:

docker run -p 3000:80 --name mynodeapp mynodeapp:01

最后访问:http://localhost:3000以查看其工作情况。