【问题标题】:How to connect the application container to the mysql containier如何将应用程序容器连接到mysql容器
【发布时间】:2019-10-14 08:05:23
【问题描述】:

我有一个应用程序服务和一个 MySQL 服务,但我无法连接这两个容器,并且它一直向我返回此错误 jango.db.utils.OperationalError: (2002, "Can't connect to MySQL server on '127.0.0.1' (115)")

我已将链接包含在我的应用程序服务中,但没有任何结果。

我的 MySQL 容器运行良好,甚至我可以登录 MySQL 容器。

这是服务的快照:

CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS                      PORTS                              NAMES
cc26d09a81d1        gmasmatrix_worker:latest        "/entrypoint.sh /sta…"   17 seconds ago      Exited (1) 11 seconds ago                                      gmasmatrix_celeryworker_1
749f23c37b16        gmasmatrix_application:latest   "/entrypoint.sh /sta…"   18 seconds ago      Exited (1) 9 seconds ago                                       gmasmatrix_application_1
666029ad063a        gmasmatrix_flower               "/entrypoint.sh /sta…"   18 seconds ago      Exited (1) 10 seconds ago                                      gmasmatrix_flower_1
50ac0497e66b        mysql:5.7.10                    "/entrypoint.sh mysq…"   21 seconds ago      Up 17 seconds               0.0.0.0:3306->3306/tcp             gmasmatrix_db_1
669fbbe0a81d        mailhog/mailhog:v1.0.0          "MailHog"                21 seconds ago      Up 18 seconds               1025/tcp, 0.0.0.0:8025->8025/tcp   gmasmatrix_mailhog_1
235a46c8d453        redis:5.0                       "docker-entrypoint.s…"   21 seconds ago      Up 17 seconds               6379/tcp                           gmasmatrix_redis_1

Docker 编写文件

version: '2'

services:

  application: &application
    image: gmasmatrix_application:latest
    command: /start.sh
    volumes:
    - .:/app
#    env_file:
#    - .env
    ports:
    - 8000:8000
#    cpu_shares: 874
#    mem_limit: 1610612736
#    mem_reservation: 1610612736
    build:
      context: ./
      dockerfile: ./compose/local/application/Dockerfile
      args:
      - GMAS_ENV_TYPE=local
    links:
      - "db"


  celeryworker:
    <<: *application
    image: gmasmatrix_worker:latest
    depends_on:
    - redis
    - mailhog
    ports: []
    command: /start-celeryworker
    links:
      - "db"


  flower:
    <<: *application
    image: gmasmatrix_flower
    ports:
    - "5555:5555"
    command: /start-flower
    links:
      - "db"

  mailhog:
    image: mailhog/mailhog:v1.0.0
    ports:
    - "8025:8025"

  redis:
    image: redis:5.0

  db:
    image: mysql:5.7.10
    environment:
      MYSQL_DATABASE: gmas_mkt
      MYSQL_ROOT_PASSWORD: pulkit1607
    ports:
    - "3306:3306"
``

【问题讨论】:

  • 您在哪里配置数据库的位置? docker-compose.yml 文件中没有显示。

标签: docker docker-compose dockerfile


【解决方案1】:

您的应用程序正在尝试连接到127.0.0.1 - 在 docker 中它指向应用程序容器本身。

您应该使用 db 容器的 IP。您可以利用内置的 docker DNS 服务来执行此操作。在您的应用程序配置中,使用db(mysql 容器的名称)作为要连接的主机,而不是使用localhost127.0.0.1

【讨论】:

  • 基本上你的应用试图连接到错误的IP,你需要找到正确的IP——db容器
猜你喜欢
  • 2016-07-14
  • 2022-11-05
  • 2021-11-16
  • 1970-01-01
  • 2021-05-13
  • 2018-09-26
  • 2017-04-11
  • 2017-07-05
相关资源
最近更新 更多