Reputation: 1356
I want to deploy a small Python app that connects with a (currently running locally) MySQL server. Within ./fzwk-app/
I created a module database
with DBController.py
:
class DbController(object):
def __init__(self):
self._db_connection = mysql.connector.connect(
host='localhost',
user='foo',
passwd='bar',
database='mydb'
)
self._db_cur = self._db_connection.cursor()
# ...
When running my small app by running ./fzwk-app/__init__.py
locally on my dev device, the db connection works just fine.
Now I wanted to start deploying to a docker container.
First, I set up the Dockerfile like this:
FROM python:3.6
ADD ./fzwk-app/ /
RUN pip3 install mysql-connector
CMD [ "python", "./__init__.py" ]
Building the Docker Image works just fine (docker build -t fzwk-app:0.1 .
), but when I try to create and run a container (docker run fzwk-app
), I am greeted with the following Python traceback:
Traceback (most recent call last):
File "./__init__.py", line 21, in <module>
from database import DbController as db
File "/database/DbController.py", line 18, in <module>
import mysql.connector
ModuleNotFoundError: No module named 'mysql'
Where's my mistake?
Upvotes: 1
Views: 471
Reputation: 1356
I managed to make it work by changing my Dockerfile to
FROM python:3.6
ADD ./fzwk-app/ /fzwk-app/
RUN pip3 install mysql-connector-python
CMD [ "python", "/fzwk-app/__init__.py" ]
So, it seems that pip install mysql-connector-python
made the change, I guess (even though I also moved the location of my app on the image, but that's more for a better overview when doing a docker exec -it fzwk-app /bin/bash
...
At least now I am getting the expected
_mysql_connector.MySQLInterfaceError: Can't connect to MySQL server on 'localhost' (99)
error cause it can't connect with my db, but for that I am planning on building a stack with docker-compose
anyway.
Hope this will help anyone -- if not, it'll probably help future-me :-)
Upvotes: 1