I’m attempting to set up a simple Docker application consisting of a container running MySql, and a second container running some Python. Following this example: https://mothishdeenadayalan.medium.com/containerizing-a-python-app-mysql-python-docker-1ce64e444ed9
I’m able to stand up the database container, docker exec
into it, and access the MySql service, so that much is working.
However nothing that I try will let me access it from outside; the connection is always refused:
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '172.20.0.2:3306' (111 Connection refused)
My Code
Docker Compose
version: "3"
services:
db:
container_name: mydb
image: mysql:8.0
ports:
- "32001:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: token_data
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_USER: admin
MYSQL_PASSWORD: asdf
app:
container_name: myapp
links:
- "db"
build: ./
ports:
- "5001:5000"
DockerFile
WORKDIR /context
USER root
COPY . /context
RUN pip3 install -r /context/requirements.txt
RUN chmod -R 777 /context/*
CMD ["python3", "/context/setup_db.py"]
and this line in Python is where it quits:
db = mysql.connector.connect(host='172.20.0.2', user='root', # password='asdf', port=3306)
In this case I’ve pulled the IP from docker inspect
. I think that part works, but the database container just won’t let me authenticate from outside.
I’ve tried using the root user as well as configuring a secondary user with a different password, and allowing passwordless access, as you can see in the compose file. Always the same result.
I have tried accessing the database container and viewing the MySql conf file. The folder /etc/mysql/conf.d seems to be empty. Not sure what to make of that. If it’s a config issue, maybe a workaround could be to copy a correct conf file into the container, but I currently can’t see how to pull that off.
I have looked at every other post I can find on this topic, none seems to address my issue or solve the problem. This can’t really be so complicated, it is a common tech stack, isn’t it? What in the world am I missing here?
Thanks for reading, any help is appreciated.