从另一个容器连接到 mysql 容器 [英] connect to mysql container from another container

查看:39
本文介绍了从另一个容器连接到 mysql 容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从撰写文件指定的同一网络上的另一个容器中连接到 mysql 容器.

I'm trying to connect to a mysql container from within another container on the same network specified by a compose file.

version: "2"
services:
  web:
    build:
      context: .
      dockerfile: nginx/Dockerfile
    ports:
      - "8080:80"
    volumes:
      - ./data:/srv
  php:
    build:
      context: .
      dockerfile: php-fpm/Dockerfile
    volumes:
      - ./data:/srv
  mysql:
    image: mysql
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
      - MYSQL_USER=dummy_user
      - MYSQL_PASSWORD=12345

如果我尝试从 php 容器连接到 mysql 容器,我不太确定连接参数是什么.

I'm not really sure what the connection parameters would be if I'm trying to connect to the mysql container from the php container.

具体来说,来自同一 docker-compose 网络的另一个容器的 mysql 容器的主机和端口是什么?

Specifically, what are the host and port for the mysql container from within another container of the same docker-compose network?

我试过主机:mysql 端口:3306,但这似乎不起作用.

I've tried host: mysql port: 3306, but that doesn't seem to work.

推荐答案

您应该链接容器.将 ports 部分添加到 mysql 容器和 links 部分到 php 容器.

You should link the containers. Add ports section to mysql container and links section to php container.

version: "2"
services:
  web:
    build:
      context: .
      dockerfile: nginx/Dockerfile
    ports:
      - "8080:80"
    volumes:
      - ./data:/srv
  php:
    build:
      context: .
      dockerfile: php-fpm/Dockerfile
    links:
      - mysql:mysql
    volumes:
      - ./data:/srv
  mysql:
    image: mysql
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
      - MYSQL_USER=dummy_user
      - MYSQL_PASSWORD=12345

通过该配置,您将能够使用 mysql:3306

With that configuration you'll be able to access mysql container from php with mysql:3306

一些文档:https://docs.docker.com/compose/网络/#updating-containers

这篇关于从另一个容器连接到 mysql 容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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