first of all I must say that I am totally new on Docker. I want to make an app that uses a Python script to connect to a mysql database and read a table showing the first rows. I am working on ubuntu 18.04
I have a dockerfile file with reference to the Python script, I have the Python script and I have the docker-compose (.yml) file.
The dockerfile is:
FROM python:latest
WORKDIR /app
COPY . /app
RUN pip install --trusted-host pypi.python.org -r requirements.txt
EXPOSE 80
CMD ["python", "reg.py"]
The Docker compose file is:
version: '3.3'
services:
mysqldev:
image: mysql:5.7
volumes:
- type: volume
source: my-db
target: /home/pakin/Documents/db-on-docker
command: --secure-file-priv=/home/pakin/Documents/db-on-docker
environment:
MYSQL_ROOT_PASSWORD: 1234
MYSQL_DATABASE: pruebas
ports:
- "3308:3306"
restart: always
reg-py:
build: .
depends_on:
- mysqldev
# Names our volume
volumes:
my-db:
And the Python script is:
import numpy as np
import pandas as pd
from sqlalchemy import create_engine
import pymysql
engine = create_engine('mysql+pymysql:/root:1234@mysqldev:3306/pruebas')
df = pd.read_sql_query('SELECT * FROM reg_db', engine)
df.head()
Using docker-compose up the app initiates but the Python script shows an error 2003, "Can't connect to MySQL server on 'localhost' ([Errno 99] Cannot assign requested address
then the mysql container works fine but python simply does not connect to it.
I appreciate any help you can provide me!