wankdanker / node-odbc

ODBC bindings for node
MIT License
173 stars 79 forks source link

How to use the odbc lib on the Docker? #52

Open maciejkamela opened 5 years ago

maciejkamela commented 5 years ago

I am trying to run my app on the Docker. One of the library I am using is https://www.npmjs.com/package/odbc. In order to install that lib I need to meet the requirements described in the odbc readme:

As per Microsoft doc in order to install ODBC Driver 13 for 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 I manage to install all the stuff locally on my Mac and successfully connect with the SQL Server on Azure but still have some issues with installing them on the Docker and then run on VSTS. My 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"]

But so far have an issue with installing NodeJS in line with

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

error: /bin/sh: 1: sudo: not found I was trying to install only the driver and for installing NodeJs just use some existing 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

But that approach throws an error:

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
asztal commented 5 years ago

You have two FROM statements in your second Dockerfile. This is what is known as a multi-stage build but I think you are doing it by accident. Normally it is used to build something in the first stage and then copy it into the second (smaller) stage. The things you installed in the ubuntu:16.04 part of the build won't be available in the node:9-alpine part unless you copy them.

However I don't think it would work for what you're doing here. You'd have to copy the unixODBC libraries into the second build stage and it's doubtful if that would work copying libraries built for ubuntu into an alpine image. (I might be wrong but I think this only really works for statically linked binaries)

I came up with this, it may not work but it's a starting point (particularly, it will produce quite a large image because of all the build tools installed). I didn't test if the odbc module itself actually worked, just that the docker build succeeded.

FROM ubuntu:18.04
RUN apt-get update && apt-get install --yes wget gnupg2 apt-transport-https build-essential make
RUN wget -O - https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN wget -O - https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN wget -O - https://deb.nodesource.com/setup_10.x | bash -
RUN apt-get update && ACCEPT_EULA=Y apt-get install -y msodbcsql unixodbc-dev nodejs
ADD . /var/www/app
WORKDIR /var/www/app
RUN npm install && npm cache clean --force
RUN npm run build 
EXPOSE 3000:80

I had to use wget instead of curl because curl conflicted with packages required by msodbcsql. Also, the issue with sudo was just that you needed to remove the sudo, the commands are running as root anyway so it isn't needed.

maciejkamela commented 5 years ago

Thank for the answer. I can install the odbc driver and other stuff on Docker but have even though have still some issue with MS ODBC Driver:

{ [Error: [unixODBC][Driver Manager]Can't open lib '/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.1.so.9.2' : file not found]
  errors:
   [ { message:
        '[unixODBC][Driver Manager]Can\'t open lib \'/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.1.so.9.2\' : file not found',
       state: '01000' } ],
  error: '[node-odbc] SQL_ERROR',
  message:
   '[unixODBC][Driver Manager]Can\'t open lib \'/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.1.so.9.2\' : file not found',
  state: '01000' }

But looks like the driver is installed when I run odbcinst -j

unixODBC 2.3.7
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /root/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

Any idea what else might be wrong?

evpozdniakov commented 5 years ago

We also tried to use node odbc in docker container. In our case the error is

SSL Provider: [error:140A90A1:SSL routines:SSL_CTX_new:library has no ciphers]