3

I'm trying to connect my application to my mysql database which I've got up and running in a docker-compose file. I'm using flask and trying to connect using DBUtils I keep getting the error message described in my title:

(pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'db' ([Errno 8] nodename nor servname provided, or not known)"))

I've tried using the IPAddresses of my docker instances as well as several other solutions in similar problems discussed here on StackOverflow: Docker-Compose can't connect to MySQL
Connecting to MySQL from Flask Application using docker-compose.

however, the offered solutions don't seem to be working for me.

my docker-compose file looks as follows:

version: '3.3' services: db: image: mysql:8.0 container_name: mysql restart: always environment: MYSQL_ROOT_PASSWORD: 'myPassword' MYSQL_DATABASE: 'databaseName' volumes: - .:/dockerFiles ports: - "3306:3306" expose: - "3306" phpmyadmin: image: phpmyadmin/phpmyadmin container_name: phpmyadmin restart: always ports: - "8080:80" volumes: - /sessions 

and my connect string looks as follows:

def connect_db(): # Connects to the database and takes care of the connection return PersistentDB( creator=pymysql, host='db', user='root', password='myPassword', database='databaseName', port=3306, autocommit=True, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) 

1 Answer 1

0

The error [Errno 8] nodename nor servname provided, or not known states that it can't find the database server. Thus, host='db' will only work if your Python code is inside a docker in the same network of the database service db. Try adding a service for the Python code in your docker-compose:

version: '3.3' services: db: image: mysql:8.0 container_name: mysql restart: always environment: MYSQL_ROOT_PASSWORD: 'myPassword' MYSQL_DATABASE: 'databaseName' volumes: - .:/dockerFiles ports: - "3306:3306" expose: - "3306" web: build: . # where is your dockerfile command: sh -c 'python app.py' # this should be the command to start your application ports: - "8082:8082" volumes: - .:/code # it depends on the WORKDIR of your dockerfile links: - db 
Sign up to request clarification or add additional context in comments.

2 Comments

<apologies for all the edits, I did not realize that hitting 'enter' automatically posts rather than jumping to a new line> Adding this to my docker-compose, I realized I needed to build a Dockerfile, so I went ahead and did so. However, When I docker-compose up my new file, I do not see how I can now run my app from within Docker. When I do PS, I get the following: ``` mysql docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp, 33060/tcp phpmyadmin /docker-entrypoint.sh apac ... Up 0.0.0.0:8080->80/tcp ``` There is no sign of my new web service @Andre
To run your program from within docker, you need to create a command in the web service in you docker-compose.yml. I edited the answer with a more correct command sh -c 'python app.py'. The command itself depends on your side. For example, if you are using Flask, you may change the command to sh -c 'flask run'. The sh -c means that docker will use sh to run your program's starting command inside the docker. Then, you can access it in the browser through the ports you exposed, in the case, port 8082. Just go to http://127.0.0.1:8082 and check if it worked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.