0

this is my docker-compose-yml file. first i tried to run the db service it work fined and docker running successfully. but when I run the app service, in the terminal it says connected to database but I get an error as (2003, "Can't connect to MySQL server on '127.0.0.1')

 version: '4'
    
    services:
      app:
        build: .
        ports:
          - "8000:8000"
        env_file:
          - .env
        depends_on:
          - db
        links:
          - db  
      db:
        environment:
              - MYSQL_ROOT_PASSWORD=root
              -
        image: mysql
        ports:
          - "3307:3307"
        env_file:
          - .env

enter image description here

Raxz
  • 17
  • 1

1 Answers1

0

What environment variable is the FastAPI app using to connect to the MySQL host?

If you're using docker-compose networking, services need to use the container name in order to reach each other (i.e. mysql://db:3307/db_name_here) not localhost.

By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

More information on networking can be found in their docs here.

Also worth noting, since you're using links you can set aliases as well like so:

version: "3.9"
services:

  web:
    build: .
    links:
      - "db:database"
  db:
    image: postgres

Links Docs Source

Baily
  • 1,290
  • 2
  • 14
  • 35
  • mysql://db:3307/db_name_here what do you mean db in here? i have mentioned the same string. i got an different error – Raxz Aug 21 '21 at 17:10
  • How are you trying to connect to MySQL inside your FastAPI app? Can you show the env vars and/or config being used? It's hard to tell what's going wrong without that. What I meant was when connecting to MySQL inside a docker-compose you need to use the container name as the hostname for internal DNS – Baily Aug 21 '21 at 17:21
  • fastapi database connection-> SQLALCHEMY_DATABASE_URL=mysql+pymysql://'+USER_NAME+':'+PASSWORD+'@'+HOST+'/'+DB env vars HOST=127.0.0.1 PORT=3307 USER_NAME=root PASSWORD=root DB=eventDb this is how it has been connected inside fastapi. I understand docker not working because I'm using localhost. how to overcome this. highly appreciatable for your valuable time – Raxz Aug 21 '21 at 17:28
  • try changing `HOST` to `db` which is the container name for your MySQL DB based on your `docker-compose.yaml` – Baily Aug 21 '21 at 17:32
  • it saying as unknown database – Raxz Aug 21 '21 at 17:49
  • @Raxz can you please show logs and/or the exact issue you're getting in the question. Kinda hard to help without it. I assume changing the HOST to `db` got rid of the initial error `Can't connect to MySQL server on '127.0.0.1`? – Baily Aug 21 '21 at 18:01
  • `links:` is obsolete, and you should just delete it. When you're making connections between containers you always need to use the "normal" port number, for MySQL 3306; `ports:` aren't used or required here. – David Maze Aug 21 '21 at 19:40