使用Docker / docker-compose设置连接到'localhost'或'127.0.0.1' [英] Connecting to 'localhost' or '127.0.0.1' with a Docker/docker-compose setup

查看:1728
本文介绍了使用Docker / docker-compose设置连接到'localhost'或'127.0.0.1'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎是一个常见问题,但是具有不同的上下文,但是使用Docker时我无法连接到本地数据库。

Seems to be a common question but with different contexts but I'm having a problem connecting to my localhost DB when using Docker.

如果我检查mysql容器使用 docker inspect 并找到IP地址并将其用作CMS的数据库主机,它运行良好...唯一的问题是mysql容器IP地址更改了(在每个 docker-compose up 上,并且如果我更换wifi网络),因此理想情况下,我想使用'localhost'或'127.0.0.1',但是由于某种原因,结果在 SQLSTATE [HY000] [2002]连接被拒绝错误中。

If I inspect the mysql container using docker inspect and find the IP address and use this as the DB host as part of the CMS, it runs fine... the only issue is the mysql container IP address changes (upon eachdocker-compose up and if I change wifi networks) so ideally I'd like to use 'localhost' or '127.0.0.1' but for some reason this results in a SQLSTATE[HY000] [2002] Connection refused error.

如何使用 localhost或 127.0.0.1'作为CMS应用程序中的数据库主机名,因此我不必在容器IP地址更改时继续更改它?

How can I use 'localhost' or '127.0.0.1' as the DB hostname in CMS applications so I don't have to keep changing it as the container IP address changes?

这是我的docker-compose。 yml文件:

This is my docker-compose.yml file:

version: "3"

services:
  webserver:
    build:
      context: ./bin/webserver
    restart: 'always'
    ports:
      - "80:80"
      - "443:443"
    links:
      - mysql
    volumes:
      - ${DOCUMENT_ROOT-./www}:/var/www/html
      - ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
      - ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
      - ${LOG_DIR-./logs/apache2}:/var/log/apache2
    networks:
      mynet:
          aliases:
            - john.dev
  mysql:
    image: 'mysql:5.7'
    restart: 'always'
    ports:
      - "3306:3306"
    volumes:
      - ${MYSQL_DATA_DIR-./data/mysql}:/var/lib/mysql
      - ${MYSQL_LOG_DIR-./logs/mysql}:/var/log/mysql
    environment:
      MYSQL_ROOT_PASSWORD: example
    networks:
      - mynet
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    links:
      - mysql
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3306
    ports:
      - '8080:80'
    volumes:
      - /sessions
    networks:
      - mynet
networks:
  mynet:


推荐答案

尝试使用 mysql 代替 localhost

您正在定义 webserver 容器和 mysql 容器之间的链接,因此 webserver 容器能够解析 mysql IP。

You are defining a link between webserver container and mysql container, so webserver container is able to resolve mysql IP.

根据 Docker文档:


Docker Cloud为您的容器提供了两种查找其他服务的方式:

Docker Cloud gives your containers two ways find other services:


  • 直接使用服务和容器名称作为主机名

  • Using service and container names directly as hostnames

使用基于Docker Compose链接的服务链接

Using service links, which are based on Docker Compose links

服务和容器主机名在服务
向上或向下扩展或重新部署时自动更新。作为用户,您可以配置服务
的名称,Docker Cloud使用这些名称为您查找
服务和容器的IP。您可以在代码中使用主机名,以
提供抽象性,使您可以轻松地交换服务容器
或组件。

Service and Container Hostnames update automatically when a service scales up or down or redeploys. As a user, you can configure service names, and Docker Cloud uses these names to find the IP of the services and containers for you. You can use hostnames in your code to provide abstraction that allows you to easily swap service containers or components.

服务链接创建允许容器将
在堆栈内彼此通信,或与堆栈外的其他服务
通信。您可以在
创建新服务或编辑现有服务时明确指定服务链接,或在
堆栈文件中为服务堆栈指定它们。

Service links create environment variables which allow containers to communicate with each other within a stack, or with other services outside of a stack. You can specify service links explicitly when you create a new service or edit an existing one, or specify them in the stackfile for a service stack.

来自 Docker撰写文档


可以通过与别名相同的主机名访问链接服务的容器,如果未指定别名,则可以使用服务名。

Containers for the linked service are reachable at a hostname identical to the alias, or the service name if no alias was specified.

这篇关于使用Docker / docker-compose设置连接到'localhost'或'127.0.0.1'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆