Finally, if everything is okay, we will see the image created:
❯ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
miapp 1 309ed83e6e7e 13 seconds ago 1.12GB
But this is not doing anything, because our image is connecting to host 127.0.0.1 of a MySQL container, but to connect containers we need a little more of knowledge.
Well, to make things work, we need to use networks.
Connection between dockers containers
We need to use networks, with it container can talk between them:
Instead of 127.0.0.1, we need to use the name of the container where (in this case) MySQL is running, so, host is now the name of that container.
import mysql.connectorconn = mysql.connector.connect(user='root', password='toor', host='testdbs')cursor = conn.cursor()cursor.execute("CREATE DATABASE test")print("Base de datos test creada")print("Lista de bases de datos:")cursor.execute("SHOW DATABASES")for database in cursor:print(database)conn.close()
If you are having an error in the connection to the MySQL container, try start your app after some seconds, or check logs of MySQL container to know when the container is fully running.
Our containers are talking to each other, using networks (: