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

  • How do I compile python for Alpine?
  • Accessing enviroment variables from a linked container
  • How to create a docker base image?
  • docker hub api not returning images
  • Trouble mounting a folder from host onto my docker image
  • Docker port uncertainty
  • 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)}
    

  • Building the MongoDB Docker image
  • How Can I increase my timeout time in docker?
  • Loading PostgreSQL Database Backup Into Docker/Initial Docker Data
  • Deploy shiny app in rocker/shiny docker
  • Seeing protocol error with ln for mounted volume inside docker
  • How to start a consul client running in a docker container?
  • 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.