连接问题与flask-sqlalchemy和Docker数据库

huangapple go评论63阅读模式
英文:

Connection problem with flask-sqlalchemy and Docker database

问题

我在使用Flask-SQLAlchemy的Flask应用程序和数据库容器之间的连接方面遇到了问题,我想创建一个Compose项目。
我目前正在使用Python3 Flask和MySQL作为我的数据库,我尝试将这两个容器连接到一个网络中,但它们无法连接。
需要补充说明的是,如果我在本地运行我的代码,只使用编辑器,它可以完美地连接到我的数据库容器,问题出现在我想要创建和使用我的项目容器时。

这是我的URI:
app.config['SQLALCHEMY_DATABASE_URI'] = f'mysql+mysqlconnector://{db_user}:{db_psw}@{db_host}:{db_port}/{db_schema}?connect_timeout=60'

这是我的环境变量,我已经使用dotenv加载了它们:

database_host = 'db'
database_port = 3308
database_user = 'root'
database_psw = 'toor'
database_schema = 'db_news'

这是我的docker-compose.yml:

version: '3'
services:
  db:
    container_name: mysqldb
    image: mysql:8.0.33
    ports:
      - "3308:3306"
    environment:
      MYSQL_DATABASE: db_news
      MYSQL_ROOT_PASSWORD: toor
    volumes:
      - database_data:/var/lib/mysql
    networks:
      - default

  app:
    container_name: app
    build: .
    ports:
      - "5000:4000"
    links:
      - db
    volumes:
      - .:/app
    networks:
      - default

volumes:
  database_data:

networks:
  default:
    driver: bridge

最后是我的dockerfile:

FROM python:3.11

WORKDIR /app

COPY . /app

COPY .env .env

RUN pip install --no-cache-dir -r requirements.txt

EXPOSE 4000

CMD ["python3", "app/app.py"]

我已经尝试过更改端口,使用'localhost'作为主机,但似乎都不起作用。

英文:

I have a problem with my connection between my flask app using flask-sqlalchemy and my database container, i want to make a compose project.
I'm currently using Python3 Flask and MySQL as my database, im trying to connect both containers into a network but i just cant them to get connect.
Need to add that if i run my code locally, just using my editor it works perfectly connecting to my db container, the problem is when i want to create and use my project container.

This is my URI:
app.config['SQLALCHEMY_DATABASE_URI'] = f'mysql+mysqlconnector://{db_user}:{db_psw}@{db_host}:{db_port}/{db_schema}?connect_timeout=60'

Here are my env vars, i already have loaded them with dotenv:

database_host = 'db'
database_port = 3308
database_user = 'root'
database_psw = 'toor'
database_schema = 'db_news'

My docker-compose.yml:

version: '3'
services:
  db:
    container_name: mysqldb
    image: mysql:8.0.33
    ports:
      - "3308:3306"
    environment:
      MYSQL_DATABASE: db_news
      MYSQL_ROOT_PASSWORD: toor
    volumes:
      - database_data:/var/lib/mysql
    networks:
      - default

  app:
    container_name: app
    build: .
    ports:
      - "5000:4000"
    links:
      - db
    volumes:
      - .:/app
    networks:
      - default

volumes:
  database_data:

networks:
  default:
    driver: bridge

And finally my dockerfile:

FROM python:3.11

WORKDIR /app

COPY . /app

COPY .env .env

RUN pip install --no-cache-dir -r requirements.txt

EXPOSE 4000

CMD ["python3", "app/app.py"]


I have tried changing the port, using 'localhost' as host but nothing seems to work.

答案1

得分: 0

请确认这是否正确?

database_host = 'db'
database_port = 3306 # 不是 3308
database_user = 'root'
database_psw = 'toor'
database_schema = 'db_news'

=====================================

重建镜像

docker compose rm # 移除容器
docker compose build # 重建镜像
docker compose up
英文:

Please confirm if this is correct?

database_host = 'db'
database_port = 3306 # not 3308
database_user = 'root'
database_psw = 'toor'
database_schema = 'db_news'

=====================================

Rebuild Image

docker compose rm # remove container
docker compose build # rebuild image
docker compose up

答案2

得分: 0

我的 docker-compose.yml 已更新

version: '3'
services:
  db:
    container_name: mysqldb
    image: mysql:8.0.33
    ports:
      - "3308:3306"
    environment:
      MYSQL_DATABASE: db_news
      MYSQL_ROOT_PASSWORD: toor
    volumes:
      - database_data:/var/lib/mysql

  app:
    env_file:
      - .env
    container_name: app
    build: .
    ports:
      - "5000:4000"
 
volumes:
  database_data:
英文:

My docker-compose.yml updated

services:
  db:
    container_name: mysqldb
    image: mysql:8.0.33
    ports:
      - "3308:3306"
    environment:
      MYSQL_DATABASE: db_news
      MYSQL_ROOT_PASSWORD: toor
    volumes:
      - database_data:/var/lib/mysql

  app:
    env_file:
      - .env
    container_name: app
    build: .
    ports:
      - "5000:4000"
 
volumes:
  database_data:

huangapple
  • 本文由 发表于 2023年7月11日 02:06:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定