我有一个带有Dockerfile的容器A ,如下所示:
...
FROM ubuntu:latest
MAINTAINER arpitaggarwal "aggarwalarpit.89@gmail.com"
EXPOSE 8080
和另一个带有Dockerfile的容器B ,如下所示:
...
FROM ubuntu:latest
MAINTAINER arpitaggarwal "aggarwalarpit.89@gmail.com"
RUN apt-get install -q -y mysql-server
EXPOSE 3306
然后我使用命令
启动容器Bdocker run -P -it --name db B /bin/bash
运行命令:docker run --rm --name web --link db A env
给出了以下输出:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=a5dd66b72ca8
DB_PORT=tcp://172.17.0.2:3306
DB_PORT_3306_TCP=tcp://172.17.0.2:3306
DB_PORT_3306_TCP_ADDR=172.17.0.2
DB_PORT_3306_TCP_PORT=3306
DB_PORT_3306_TCP_PROTO=tcp
DB_NAME=/web/db
HOME=/root
然后我登录容器A 并运行命令: ping db
给了我输出:
PING db (172.17.0.2) 56(84) bytes of data.
64 bytes from db (172.17.0.2): icmp_seq=1 ttl=64 time=0.082 ms
64 bytes from db (172.17.0.2): icmp_seq=2 ttl=64 time=0.063 ms
64 bytes from db (172.17.0.2): icmp_seq=3 ttl=64 time=0.065 ms
64 bytes from db (172.17.0.2): icmp_seq=4 ttl=64 time=0.061 ms
64 bytes from db (172.17.0.2): icmp_seq=5 ttl=64 time=0.066 ms
当我使用端口3306尝试telnet
命令时,如下所示:
telnet 172.17.0.2 3306
给我输出:
root@9b078c1fed82:/# telnet 172.17.0.2 3306
Trying 172.17.0.2...
telnet: Unable to connect to remote host: Connection refused
任何想法,我如何telnet链接容器?
任何帮助将不胜感激!
答案 0 :(得分:2)
默认情况下,MySQL通常只会监听127.0.0.1
。您需要在构建期间修改my.cnf
并将bind-address
设置为0.0.0.0
。你可以通过以下方式实现这一目标:
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf
答案 1 :(得分:1)
在收到@Andy Shinn的回答后,我更新了容器B Dockerfile,如下所示:
FROM ubuntu:latest
MAINTAINER arpitaggarwal "aggarwalarpit.89@gmail.com"
RUN apt-get install -q -y mysql-server
RUN apt-get install -q -y mysql-client
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf
EXPOSE 3306
当我尝试从容器A 访问 mysql-server 时:
mysql -u root -ppassword -h 172.17.0.2
给我错误:
ERROR 1130 (HY000): Host '172.17.0.3' is not allowed to connect to this MySQL server
我认为这是因为容器A试图通过root用户连接到db容器。 但是在mysql中不允许你通过root用户远程连接。所以我在使用root权限在mysql-server中创建另一个用户。