Skip to content

mariadb-RobSchwyzer/run-python-script-with-mariadb-connector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Run Python Script with MariaDB Connector

Uses a CentOS 7 base image to deploy a container with MariaDB Connector/Python installed. Immediately runs file script.py and exits on completion.

Dependencies

Install and Run

Download/clone/extract the code to a directory. Open a terminal and change into the directory the code is in. Then-

docker build -t python-script . ;
docker run python-script ;

View Logs

docker logs python-script ;

Included Code

A default script.py comes with this repository. Expect the following output when using it without any modifications-

Error connecting to MariaDB Platform: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

To test connectivity to your MariaDB server, edit Dockerfile and change ENV lines to the correct MariaDB user, password, IPv4 address or FQDN, and port for your remote MariaDB server. You can then rebuild this container and see if the output of the test script changes.

Use Your Script

Move the existing script.py and then copy (or move) the Python script you want to run to script.py. Then rebuild the image and run as normal.

Note on ENV

The included script.py is coded to pull data from ENV. This functionality is not inherent though. If you would like your Python script to support this, please review the source code of the included script.py and adapt it for your needs.

Note on Secrets

The included script.py checks /run/secrets/mariadb_pass for a file. If one exists at that location, it will use the content of that file for password instead of the ENV variable. In practice, this can take advantage of Docker Secrets or a mounted volume. This provides a basic demonstration of how you can more securely pass a password into the container.

Connecting to MariaDB

The container only provides connector infrastructure, it does not provide a running MariaDB server. Reference Connector/Python documentation to learn how to specify parameters to open a connection to a remote MariaDB server.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published