Install postgresclient/sqlserver odbc drivers in dockerfile - linux

I'm trying to install postgres odbc driver to connect with different external databases with pyodbc
My app receives credentials from any database of type mysql, sqlserver, postgresql, etc. And I'm having problems installing the drivers.
This is my docker file:
FROM python:3.10.9-bullseye
WORKDIR /code
ENV FLASK_APP app.py
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
RUN apt-get update
RUN apt-get install -y openjdk-17-jdk
#Mysql driver
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \
&& curl https://packages.microsoft.com/config/debian/11/prod.list \
> /etc/apt/sources.list.d/mssql-release.list \
&& apt-get install -y --no-install-recommends \
locales \
apt-transport-https \
&& echo "en_US.UTF-8 UTF-8" > /etc/locale.gen \
&& locale-gen \
&& apt-get update \
&& apt-get -y --no-install-recommends install unixodbc-dev libodbc1\
&& ACCEPT_EULA=Y apt-get -y --no-install-recommends install msodbcsql18
RUN sed -i -E 's/(CipherString\s*=\s*DEFAULT#SECLEVEL=)2/\11/' /etc/ssl/openssl.cnf
ENV ACCEPT_EULA=Y
RUN apt-get update -y && apt-get update \
&& apt-get install -y --no-install-recommends curl gcc g++ gnupg unixodbc-dev
#Postgresql
RUN apt-get update \
&& apt-get install -y libsasl2-modules-gssapi-mit libsasl2-modules
#Postgresql unicode
RUN apt-get install -y postgresql
RUN apt-get install -y odbc-postgresql
COPY . .
CMD ["flask", "run", "--debugger", "--reload", "--host=0.0.0.0"]
When I try to connecto with postgres database I got the error:
pyodbc.OperationalError: ('08001', '[08001] [unixODBC]could not
connect to server: No such file or directory\n\tIs the server running
locally and accepting\n\tconnections on Unix domain socket
"/var/run/postgresql/.s.PGSQL.5432"?\n (101) (SQLDriverConnect)')
When I connect with sql server database, I can extract info, but when I try to send info to the database I got this error:
TCP Provider: Error code 0x2746
I'm trying to set the openSSL to 1.0, and that allow me to extract info from sql servers database, but no send it with postgres, even the psql console is not installed :c
I'm having struggling with this drivers for some days. If anyone can help me, I'll be very greatful

Related

Pyodbc Connection error when trying to connect to remote sql server from azure docker container

Azure Docker Container Error:
pyodbc.OperationalError: ('08S01', '[08S01] [FreeTDS][SQL Server]Unable to connect: Adaptive Server is unavailable or does not exist (20009) (SQLDriverConnect)')
The connection string is:
conn = pyodbc.connect('Driver={FreeTDS};SERVER=server_name,1433;DATABASE=dbname;UID=username;PWD=password')
Odbcinst.ini
[FreeTDS]
Description=FreeTDS Driver
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup=/usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
DockerFile
FROM python:3.8
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# install FreeTDS and dependencies
RUN apt-get update \
&& apt-get install unixodbc -y \
&& apt-get install unixodbc-dev -y \
&& apt-get install freetds-dev -y \
&& apt-get install freetds-bin -y \
&& apt-get install tdsodbc -y \
&& apt-get install --reinstall build-essential -y
# populate "ocbcinst.ini"
RUN echo "[FreeTDS]\n\
Description = FreeTDS unixODBC Driver\n\
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so\n\
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so" >> /etc/odbcinst.ini
RUN pip install --trusted-host pypi.python.org -r requirements.txt
EXPOSE 80
# Run app.py when the container launches
CMD ["python", "app.py"]
When I am running the container on local machine, it is running. But when I am trying to do so on azure platform I am getting this error. Any help would be really appreciated.

Azure CLI in a Docker Container

I have an Ubuntu 18.04 Docker image that I need Azure CLI installed in. For a Docker image, it seems the preferred way is to use pip, however, I have other pip Azure libraries needed in the container that overlap with the CLI install and get blown away because Azure CLI requires older versions; then making it so I can't run my python scripts.
I have tried to use the script installer but that hasn't worked and errored out because I believe the install is interactive.
The last option I can find is the manual apt install, though I am not sure this is a correct way nor do I have a good idea of how to replicate that in a Dockerfile.
Is there a preferred/good way of getting Azure CLI in a container not using pip?
FROM ubuntu:18.04
RUN apt-get update && apt-get -y upgrade && \
apt-get -f -y install curl python3-pip python-pip && \
pip3 install --upgrade pip && \
pip2 install --upgrade pip && \
pip3 install azure-storage-blob==12.3.0 & \\
pip3 install azure-cli
I have a preference to use the package manager to install dependencies, it's why I will do something like that:
Add base dependencies for https repostory and curl
Add the gpg key and repository for the CLI
Add the CLI
This is the Dockerfile with thoses steps:
FROM ubuntu:18.04
RUN apt-get update && apt-get -y upgrade && \
apt-get -f -y install curl apt-transport-https lsb-release gnupg python3-pip python-pip && \
curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > /etc/apt/trusted.gpg.d/microsoft.asc.gpg && \
CLI_REPO=$(lsb_release -cs) && \
echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ ${CLI_REPO} main" \
> /etc/apt/sources.list.d/azure-cli.list && \
apt-get update && \
apt-get install -y azure-cli && \
rm -rf /var/lib/apt/lists/*
In addition, I clean up the apt cache by removing /var/lib/apt/lists. Tt reduces the image size, since the apt cache is not stored in a layer.
See: https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-linux?pivots=apt#option-1-install-with-one-command
RUN curl -LsS https://aka.ms/InstallAzureCLIDeb | bash && rm -rf /var/lib/apt/lists/*

How to build an docker image using dockerfile which includes databases creation

I am trying to build docker image using docker file .
The docker file will contain database creation.
FROM ubuntu:18.04
RUN apt-get update && apt-get install -y curl
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
RUN apt-get update && apt-get install -y nodejs
RUN node -v
RUN npm -v
RUN apt-get install -y redis-server
RUN redis-server -v
RUN apt-get install -my wget gnupg
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv D68FA50FEA312927
RUN echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-3.2.list
RUN apt-get update
RUN apt-get install -y mongodb-org
RUN mongodb -version
I am unable to start redis server after installation in docker container
You should to expose ports for redis:
EXPOSE 6379
And please remember, that each RUN creates a new layer in your image, and you can group all shell commands in one RUN directive. It should be something like this:
FROM ubuntu:18.04
RUN apt-get update && apt-get install -y curl wget gnupg && \
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv D68FA50FEA312927 && \
echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-3.2.list && \
curl -sL https://deb.nodesource.com/setup_8.x | bash - && \
apt-get update && \
apt-get install -y nodejs redis-server mongodb-org redis-server && \
node -v && \
npm -v && \
mongodb -version
EXPOSE 6379
And one more thing. Docker way tell us run only one process in one container, so you should to separete your Redis, Mongo and other apps to different containers and run it with some orchestrator(such as docker-swarm or node or kubernetes) or just docker-compose.

How to run pdfkit from_file in a Docker container

The following is my code for Docker:
FROM python:3.6.2-jessie
## Prepare apt-get:
RUN echo "debconf debconf/frontend select Noninteractive" | debconf-set-selections && \
apt-get update -qy && \
apt-get upgrade -qy && \
apt-get clean autoclean && \
apt-get install wkhtmltopdf -y && \
apt-get autoremove -y && \
apt-get install python3-setuptools -qy &&\
apt-get install python3-dev -qy && \
apt-get install python3-pip -qy && \
apt-get install build-essential -qy && \
apt-get -y install openssh-client -qy && \
apt-get clean
RUN pip3 install --upgrade pip
RUN pip3 install pandas
RUN pip3 install psycopg2==2.7.3.2
RUN pip3 install pdfkit
When I run following code,
docker run my_image python3 pdfkit.py
I get the following error:
OSError: wkhtmltopdf exited with non-zero code -6. error: The switch
--viewport-size, is not support using unpatched qt, and will be ignored.The switch --disable-smart-shrinking, is not support using
unpatched qt, and will be ignored.QXcbConnection: Could not connect to
display
Try the following:
RUN curl -L#o wk.tar.xz https://downloads.wkhtmltopdf.org/0.12/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz \
&& tar xf wk.tar.xz \
&& cp wkhtmltox/bin/wkhtmltopdf /usr/bin \
&& cp wkhtmltox/bin/wkhtmltoimage /usr/bin \
&& rm wk.tar.xz \
&& rm -r wkhtmltox
Rather than installing directly using apt-get install wkhtmltopdf -y.

Docker(compose) installing nodejs broke npm

I need to use a container with nginx and nodejs, so I take the nginx container and install the node:
FROM nginx
ENV DEBIAN_FRONTEND noninteractive
WORKDIR /usr/src/app
VOLUME /usr/src/app
RUN apt-get update && \
apt-get install -y apt-utils && \
apt-get install -y --no-install-recommends curl sudo wget nano && \
curl -sL https://deb.nodesource.com/setup_6.x | bash - && \
apt-get install -y nodejs git build-essential && \
whereis npm && \
npm install grunt grunt-cli bower -g && \
whereis returnme nothing npm:, and npm install... crash the build proccess. so Where ir my mistake, is there a bug or anything? btw I'm using latest docker-compose and Docker version 17.03.1-ce, build c6d412e
Update 1: It is not a dupe of this question, I'm only using one RUN line
FROM nginx
RUN \
echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections && \
apt-get update && \
apt-get install -y apt-utils && \
apt-get upgrade -y && \
apt-get update --fix-missing && \
apt-get install -y curl sudo wget nano git build-essential
# Install NodeJS
RUN \
wget https://deb.nodesource.com/setup_6.x && \
chmod +x setup_6.x && \
./setup_6.x && \
apt-get install -y nodejs && \
npm install grunt grunt-cli

Resources