我正在尝试在Docker上运行我的应用程序。我正在使用的库之一是https://www.npmjs.com/package/odbc。为了安装该lib,我需要满足ODBC读数中描述的要求:
- UnixODBC二进制文件和模块编译的开发库
- 在Ubuntu/debian sudo apt-get安装unixodbc unixoDBC-dev
- 在redhat/centos sudo yum安装unixodbc unixoDBC-Devel
- 目标数据库的ODBC驱动程序
- 正确配置了odbc.ini和odbcinst.ini。
根据Microsoft Doc,以安装SQL Server 13-for-sql-server?view = sql-server-2017#ubuntu-1604-1我设法在Mac上安装了本地的所有内容,并在Azure上成功连接了SQL Server,但仍存在将它们安装在Docker上,然后在VST上运行的问题。我的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
我试图仅安装驱动程序和安装nodejs,只使用一些现有的docker images:
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
您正在从ubuntu:16.04
映像上进行工作,从本质上讲,Nodejs家伙已经完成了很多步法。
如果我是你,我会去看image node:10-stretch-slim
。然后使用apt-get
安装所需的驱动程序(如果有的话,否则脚本下载并安装在您的Dockerfile
中)。
sudo
命令通常不安装在Docker映像上,因为在容器会话中默认情况下用户为root
。如果您看到有关sudo
的任何错误,通常可以从引起问题的命令行中删除sudo
。
可能的解决方案
在此处更新我的答案,并为您提供一个可能的解决方案。
此解决方案将根据Debian Stretch 9将您的应用程序放在节点10映像中。它将从Debian 9 Microsoft存储库中为您提供数据库驱动程序,并安装我从您的那里看到的所有包装问题。
我还在脚本的底部添加了ENTRYPOINT
和CMD
。但是这些行是猜测的,因为您的问题尚未指出您如何实际启动应用程序。(如果您添加了,那么我将更新我的答案)。
注意。请注意,我将--host 0.0.0.0
传递给npm run start
命令。这是为了避免将实时服务器绑定到Localhost,这将使容器外部无法访问。除非您使用--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看到它工作。