Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/felipedemacedo/python-3.7-with-pyodbc
Docker environment with Python 3.7 and pyodbc dependencies installed for connection with SQL Server database.
https://github.com/felipedemacedo/python-3.7-with-pyodbc
docker docker-image environment environment-variables mssql mssql-database mssqlserver pyodbc python python3 sqlserver
Last synced: about 2 months ago
JSON representation
Docker environment with Python 3.7 and pyodbc dependencies installed for connection with SQL Server database.
- Host: GitHub
- URL: https://github.com/felipedemacedo/python-3.7-with-pyodbc
- Owner: felipedemacedo
- License: gpl-3.0
- Created: 2019-09-03T18:31:20.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-01-23T19:31:02.000Z (almost 5 years ago)
- Last Synced: 2024-10-12T07:21:07.238Z (3 months ago)
- Topics: docker, docker-image, environment, environment-variables, mssql, mssql-database, mssqlserver, pyodbc, python, python3, sqlserver
- Language: Dockerfile
- Homepage:
- Size: 19.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Python 3.7.4 + pyodbc (SQL Server)
[Docker environment](https://hub.docker.com/r/felipederodrigues/python37withpyodbc) with Python 3.7 and pyodbc dependencies installed for connection with SQL Server database.
## Dockerize your Python app using this environment
### Create this Dockerfile (inside your Python app main directory)
```java
FROM felipederodrigues/python37withpyodbc:v1COPY . /app
WORKDIR /appRUN pip install --upgrade pip
RUN python -m pip install -r requirements.txtENTRYPOINT [ "python", "Main.py" ]
```
### Create a Docker image with it. This image will have your app inside it (e.g.: /app directory).
```java
docker build -t the-name-of-your-image:latest .
```
### Run your app:
```java
docker run the-name-of-your-image:latest
```## Suggestion
As a good practice, you can feed your app with environment variables for database connection.
For instance, you may call your application like this:
```java
docker run \
-e SQL_SERVER_SERVER="hostname.your.network" \
-e SQL_SERVER_DATABASE="YourDatabaseName" \
-e SQL_SERVER_USERNAME="YourDatabaseUsername" \
-e SQL_SERVER_PASSWORD="YourDatabasePassword" \
the-name-of-your-image:latest
```
And the code may pick this information up, without hardcoding credentials.
```python
@staticmethod
def connect_db():
"""
Parameters
----------
"""print("Conecting to database ...")
server = os.environ['SQL_SERVER_SERVER']
database = os.environ['SQL_SERVER_DATABASE']
username = os.environ['SQL_SERVER_USERNAME']
password = os.environ['SQL_SERVER_PASSWORD']
return db.DBConnection(server, database, username, password)
```## Environment Info
```
root@84e7f4ffff9e:/# python --version
Python 3.7.4
root@84e7f4ffff9e:/# odbcinst -j
unixODBC 2.3.4
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
root@84e7f4ffff9e:/# uname -r
4.9.125-linuxkit
root@84e7f4ffff9e:/# pip --version
pip 19.2.3 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
```## Special Thanks:
- [@iuryxavier](https://github.com/iuryxavier)## Credits:
- https://github.com/mkleehammer/pyodbc/wiki