guri
guri

Reputation: 1701

Docker compose mysql connection failing

I am trying to run 2 docker containers using docker-compose and connect mysql container to app container.Mysql container is running but app container is failing to start with the error Error:2003: Can't connect to MySQL server on '127.0.0.1:3306' (111 Connection refused) It seems like my app container is trying to connect my host mysql instead of mysql container.

docker-compose.yml

version: '2'
services:
  mysql:
    image: mysql:5.7
    container_name: database
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: malicious
      MYSQL_USER: root
      MYSQL_PASSWORD: root

  app:
    build: .
    restart: unless-stopped
    volumes:
      - .:/Docker_compose_app   #app directory
    depends_on:
     - "mysql"
    command: [ "python", "database_update.py"]
    restart: unless-restart
    environment:
    # Environment variables to configure the app on startup.
     MYSQL_DATABASE: malicious
     MYSQL_HOST: database

Dockerfile

 FROM python:2.7
 ADD . /Docker_compose_app
 WORKDIR /Docker_compose_app
 RUN apt-get update
 RUN pip install --requirement requirement.txt

This is my database_update.py file.

    def create_TB(cursor,connection): 
      query = '''CREATE TABLE {}(malicious VARCHAR(100) NOT NULL)'''.format("url_lookup")
      cursor.execute(query)
      connection.commit()

    def connection():           
     try:
       cnx = mysql.connector.connect(user="root",password = 'root',database=malicious)
       cursor = cnx.cursor()
       create_TB(cursor,cnx)

     except mysql.connector.errors.Error as err:
       data = {"There is an issue in connection to DB":"Error:  {}".format(err)}

Upvotes: 4

Views: 10778

Answers (2)

T4gmaster
T4gmaster

Reputation: 11

You might want to consider using Docker Networks.

I was having a similar problem while having two seperate Python container connecting to one mysql-Container, while those 2 were connected to a Vue-Frontend.

First I tried using links (which was not optimal, because the communication-flow is not entirely linear), just like you but the I ran across this great post: https://www.cbtnuggets.com/blog/devops/how-to-share-a-mysql-db-with-multiple-docker-containers

Using Networks shift the port mapping off and lets you enhance your overall App-Architecture.

Therefore I think you should try something like:

services:
  python-app:
    networks:
      - network_name
    ...
  
  mysql:
    networks:
      - network_name
    ...

networks:
  network_name:

Upvotes: 1

nwinkler
nwinkler

Reputation: 54477

There are two issues I can see:

  1. Try to add

    links: 
      - mysql:mysql
    

    to the app service in your Docker Compose file. This will make sure that you can reach the mysql container from app. It will set up a hostname mapping (DNS) in your app container, so when you ping mysql from app, it will resolve it to the mysql container's IP address.

  2. In your .py file, where are you defining which host to connect to? Add host="mysql" to the connect call. By default, it will connect to 127.0.0.1, which is what you're seeing.

    cnx = mysql.connector.connect(host="mysql", user="root", password = 'root', database=malicious)
    

Fixing both of these should solve your problem.

Upvotes: 4

Related Questions