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

  • Is it safe to extract the root filesystem of a Docker.io image and use it in a chroot?
  • Ubuntu docker swarm error “docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.”
  • Redirect traffic from docker container into localhost
  • How to change the dns of a running docker container
  • File is not created in Docker
  • Kubernetes: Dynamically create configuration json files from j2 templates
  • 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)}
    

  • Using private registry hosted on docker
  • docker-compose up via Windows bat file?
  • docker-machine ls shows state timeout
  • How do I give an own ip address to docker for Windows container?
  • Connect Docker Containers: Frontend to GraphQL Backend via Docker Compose on the same Host
  • docker ubuntu container: shell linked to bash still starts shell
  • 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.