0

I want to create a temporary MySQL database for unit testing in Python. I decided to go with docker as the tests will be run on different machines.

As a first thing I run the container:

client = docker.from_env()
container = client.containers.run("ubuntu/mysql", detach=True, ports={3306:3307},environment={
    'MYSQL_USER':'mysqluser',
    'MYSQL_PASSWORD':'password',
    'MYSQL_DATABASE':'mydb',
                                                                                              })

print('Container Started : {}'.format(container.status))

And then I try to connect using SQLAlchemy:

engine = sa.create_engine(f'mysql+pymysql://mysqluser:password@localhost:3307/mydb')
engine.connect()

which returns:

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2013, 'Lost connection to MySQL server during query')

EDIT

After putting sleep(10) after container.run, the response is different:

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")

Do you know what I'm doing wrong?

What I want to do is to connect to MySQL database on port 3307 (as one server already runs on the host). Then I want to create and populate a table.

EDIT2

Switching ports to {3307:3306} returns error:

docker.errors.APIError: 500 Server Error for http+docker://localhost/v1.41/containers/67a04b384bf3d39407881d4dfc73961932a3ce72fe8dc82c3cd6aedd57da2821/start: Internal Server Error ("driver failed programming external connectivity on endpoint hopeful_leakey (97840eb42d2c09b5e9754b634481d8e048c57ba2de8ffc10b34261de2f76600b): Error starting userland proxy: listen tcp4 0.0.0.0:3306: bind: address already in use")
Milano
  • 18,048
  • 37
  • 153
  • 353
  • The database is running on `3306`. `ports={3306:3307}` means "map 3306 on my local machine to the containers port 3307" and you are connecting to it on `localhost` on your host machine. – super Jan 11 '22 at 18:56
  • @super Thanks, I tried also the opposite but with no success: `docker.errors.APIError: 500 Server Error for http+docker://localhost/v1.41/containers/67a04b384bf3d39407881d4dfc73961932a3ce72fe8dc82c3cd6aedd57da2821/start: Internal Server Error ("driver failed programming external connectivity on endpoint hopeful_leakey (97840eb42d2c09b5e9754b634481d8e048c57ba2de8ffc10b34261de2f76600b): Error starting userland proxy: listen tcp4 0.0.0.0:3306: bind: address already in use") ` Do yo uhave any idea on how to make it work? – Milano Jan 11 '22 at 18:59
  • @super I just managed to make it run with the original `ports` argument. Are you sure what you've written is correct? (just want to understand) – Milano Jan 11 '22 at 19:07
  • For many applications, SQLite fits very well in this space: you can launch an in-memory, in-process database for your local tests, which won't actually store very much data. This will also be much easier to run since you don't need any of the Docker-related permissions or complications. Would that approach work for your particular use case? – David Maze Jan 11 '22 at 19:47
  • @DavidMaze It's for testing and unittesting of database connectors so I can't just use Sqlite3 – Milano Jan 11 '22 at 20:51

0 Answers0