1

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?

2
  • i guess you just need to expose what port you will expose your container in your computer. try adding EXPOSE <port_number> to your dockerfile and see if that works. EX: EXPOSE 5000 Commented Apr 29, 2019 at 13:35
  • Thanks for your input, @MarceloFonseca. I am going to look further into creating a whole stack with a decent docker-compose.yml setup; for now my task is just to create an image that will work an run my small Python app... So, no need to expose anything so far... Commented Apr 29, 2019 at 13:50

1 Answer 1

1

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 :-)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.