3

Starting with Docker, I'm currently trying to setup a container which runs a simple Python script. This script is supposed to connect to a remote database using the mysql.connector library. When I run it from my standard Windows environment, it works perfectly but it failed to connect to the database when I launch it from the container.

here is my Dockerfile:

# Docker image to build the container from 
FROM python:3 

# Path to script
ADD DLinvoices.py /



#Download the Mysql library

RUN pip install mysql-connector-python

#Download requests library
RUN pip install requests

#Running the script
CMD [ "python", "./DLinvoices.py" ]

Here is the console output when i run the docker image:

$docker run docker_invoice
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 179, in _open_connection
    self._cmysql.connect(**cnx_kwargs)
_mysql_connector.MySQLInterfaceError: Access denied for user 'user'@'ip.isp.overthebox.ovh' (using password: YES)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./DLinvoices.py", line 15, in <module>
    cnx = mysql.connector.connect(user='user', password='***', host='ip_host', database='dbName')
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/__init__.py", line 172, in connect
    return CMySQLConnection(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 78, in __init__
    self.connect(**kwargs)
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/abstracts.py", line 736, in connect
    self._open_connection()
  File "/usr/local/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 182, in _open_connection
    sqlstate=exc.sqlstate)
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'user'@'ip.isp.overthebox.ovh' (using password: YES)

edit: python script

import mysql.connector
import datetime
import requests
import io
import xmlrpc.client
import base64
import shutil
import errno

__PATH_TO_NAS__ = "./"

# DataBase connection
try:
    cnx = mysql.connector.connect(user='user', password='pwd', host='ip_host, database='dbname')
    cursor = cnx.cursor(buffered=False, dictionary=False)
except mysql.connector.Error as err:
    if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
        print("Something is wrong with your user name or password")
    elif err.errno == errorcode.ER_BAD_DB_ERROR:
        print("Database does not exist")
    else:
        print(err)

## variables ##

dtnow = datetime.datetime.today()
dtnow_tt = dtnow.timetuple()
day = dtnow_tt.tm_mday
month = dtnow_tt.tm_mon
year = dtnow_tt.tm_year

listId = []

## queries ##

query = "SELECT id_order, YEAR(invoice_date) year FROM ps_orders WHERE YEAR( invoice_date ) = " + str(
    year - 1) + " AND MONTH( invoice_date ) = " + str(month) + " AND DAY( invoice_date ) = " + str(day - 1)

cursor.execute(query, year)

##
#recuperation des IDs des factures a imprimer
##

for (id_order, year) in cursor:
    listId.append(id_order)
cursor.close()
cnx.close()



for id in listId:
    data = {"id": id}
    r = requests.post("link_to_the_pdf_file", data=data)
    print(r.headers['Content-type'])
    r.raw.decode_content = True
    try:
        with open(str(id) + "F.pdf", 'wb') as f:
            f.write(r.content)

    except IOError:

        print("Erreur! Le fichier n\' pas pu être ouvert ou est deja existant")

Thank you in advance for your help!

Adiii
  • 54,482
  • 7
  • 145
  • 148
  • Share the code, the issue is not with docker, there is password error in python script `Access denied for user 'user'@'ip.isp.overthebox.ovh' (using password: YES)` – Adiii Jul 16 '19 at 10:41
  • 1
    I don't think this is an issue with docker. Try granting access to all hosts in your MySQL database as per https://stackoverflow.com/questions/17975120/access-denied-for-user-rootlocalhost-using-password-yes-no-privileges – Ishank Gulati Jul 16 '19 at 10:43
  • Thank you for your quick responses! What i don't understand is that my script is running well on my current envirronment, so my connection configuration is apparently right.. – Reviron Arthur Jul 16 '19 at 10:49
  • @RevironArthur Please check the updated answer, the error is nothing to do with docker – Adiii Jul 16 '19 at 11:31

1 Answers1

1

First thing, if it's not typo

cnx = mysql.connector.connect(user='user', password='pwd', host='ip_host', database='dbname')

' is missing in the MySQL connection string.

I am pretty sure that the error is not from docker, here is the way to confirm it.

docker run --rm --name mariadb -p 3306:3306 -e MYSQL_ROOT_PASSWORD=hellopass -it mariadb:10.4

Now build the python base image and link it with maria DB container

FROM python:3.7.4-alpine3.10
RUN pip install mysql-connector

RUN echo $'#!/usr/bin/python \n\
import mysql.connector \n\
try:\n\
    cnx = mysql.connector.connect(user=\'root\', password=\'hellopass\', host=\'dbhost\') \n\
    cursor = cnx.cursor(buffered=False, dictionary=False) \n\
except mysql.connector.Error as err: \n\
    if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: \n\
        print("Something is wrong with your user name or password") \n\
    elif err.errno == errorcode.ER_BAD_DB_ERROR: \n\
        print("Database does not exist") \n\
    else: \n\
        print(err) \n\
print(cnx.is_connected())' >> /root/dbtest.py 
CMD [ "python" , "/root/dbtest.py"]

Run the DB container

 docker run --rm --link mariadb:dbhost -it pymsql ash -c "python /root/dbtest.py"

Here is the connection output

enter image description here

Adiii
  • 54,482
  • 7
  • 145
  • 148
  • 1
    As you were probably expected, the error was me being dumb and mixing my test database and production database login and pwd. Sorry for the not so interesting question, and thank you for those interesting tests to see if the error comes from Docker! Have a good day – Reviron Arthur Jul 16 '19 at 12:18