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

  • php composer inside container lost vendor directory
  • Docker: Issue with pulling from a private registry from another server
  • Set up swarm mode Docker Swarm using Vagrant
  • Is it possible run syslog inside Docker and expose that to the host as host's syslog daemon?
  • npm package.json and docker (mounting it…)
  • Docker Machine Virtual Box Driver Issue
  • 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)}
    

  • When using Docker Containers, where are shared Python libraries stored?
  • Docker + Kubernetes build
  • Use Docker Compose offline by using local images and not pulling images
  • Error while connecting Wordpress Docker Container with MySQL
  • How can the current build context directory be referenced inside of a Dockerfile?
  • Why the image that built by myself dont run on the kubernetes,and the pod always restart?
  • One Solution collect form web for “Docker compose mysql connection failing”

    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.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.