docker:从一个容器连接到另一个容器

时间:2017-12-01 21:17:34

标签: mysql docker

我有2个docker容器在同一个物理主机上运行。一个运行MySQL服务器。我想从另一个容器连接到MySQL服务器。我链接了2个容器,我尝试连接:

mysql -h 172.17.0.3 -u root -p

它失败了:

ERROR 2003 (HY000): Can't connect to MySQL server on '172.17.0.3' (111)

如果我进入运行MySQL的容器,我可以正常连接。

我看过很多很多人都有相同问题,但这些答案都没有帮助我。

我在相同的2个容器和其他端口上的其他服务之间做同样的事情,它们都有效。

当我在端口上运行nmap时,我得到了这个:

PORT     STATE  SERVICE
3306/tcp closed mysql

对于其他有效的服务,我得到了:

PORT     STATE SERVICE
5820/tcp open  unknown

如果我在运行MySQL(172.17.0.3)的容器上运行tcpdump并尝试从另一个容器(172.17.0.4)连接,我会看到:

15:52:57.048838 IP 172.17.0.4.47550 > 91fa9dcbc2a9.mysql: Flags [S], seq 3355905714, win 29200, options [mss 1460,sackOK,TS val 890919597 ecr 0,nop,wscale 7], length 0
15:52:57.048874 IP 91fa9dcbc2a9.mysql > 172.17.0.4.47550: Flags [R.], seq 0, ack 3355905715, win 0, length 0
15:52:57.050627 IP 91fa9dcbc2a9.47225 > 192.168.10.3.domain: 14265+ PTR? 4.0.17.172.in-addr.arpa. (41)
15:52:57.064719 IP 192.168.10.3.domain > 91fa9dcbc2a9.47225: 14265 NXDomain 0/0/0 (41)
15:52:57.064933 IP 91fa9dcbc2a9.39489 > 192.168.10.3.domain: 28024+ PTR? 3.10.168.192.in-addr.arpa. (43)
15:52:57.077012 IP 192.168.10.3.domain > 91fa9dcbc2a9.39489: 28024 NXDomain 0/0/0 (43)

主机上没有运行防火墙。任何人都有任何想法,为什么这不起作用和/或我如何进一步排除故障?

根据要求,Dockerfile:

FROM debian:buster
MAINTAINER Larry Martell <larry.martell@gmail.com>

ENV HOME /opt/django/CAPgraph/

RUN echo "deb http://http.debian.net/debian buster main" >> /etc/apt/sources.list
RUN (apt-get update -y && DEBIAN_FRONTEND=noninteractive apt-get install -y --force-yes build-essential git python python-dev python-setuptools nginx sqlite3 supervisor def
ault-mysql-server default-libmysqlclient-dev vim cron unzip software-properties-common python2.7 openjdk-8-jre-headless ca-certificates-java openjdk-8-jre xvfb wkhtmltopdf 
sendmail-bin sendmail r-cran-ggplot2 r-cran-caret net-tools traceroute nmap tcpdump)
RUN (easy_install pip &&\
  pip install uwsgi)

ADD . /tmp/CAPgraph
RUN cp -rp /tmp/CAPgraph/* $HOME
RUN pip install -r $HOME/requirements.txt

# stardog
ENV STARDOG_HOME /AppData/stardog_knowledgegraph
ENV STARDOG_JAVA_ARGS "-Xms10g -Xmx10g -XX:MaxDirectMemorySize=10g"
ADD deploy/stardog/stardog-license-key.bin $HOME/deploy/stardog/stardog-license-key.bin
ADD deploy/stardog/init $HOME/deploy/stardog/init

# install utilities
COPY deploy/util /usr/local/bin
RUN chmod a+x /usr/local/bin/reset_CAPgraph /usr/local/bin/start_CAPgraph /usr/local/bin/stop_CAPgraph

RUN (echo "daemon off;" >> /etc/nginx/nginx.conf &&\
  rm /etc/nginx/sites-enabled/default &&\
  rm -f /etc/nginx/sites-enabled/django.conf &&\
  ln -s /opt/django/CAPgraph/supervisord.conf /etc/supervisor/conf.d/)

RUN chown -R root:root $HOME
RUN chmod 777 $HOME
RUN chmod 777 /opt/django

VOLUME ["/opt/django/CAPgraph"]
EXPOSE 80 8006 443 3306
ADD crontab /etc/cron.d/backupMySQL
RUN chmod 0644 /etc/cron.d/backupMySQL
RUN touch /var/log/cron.log

CMD ["/opt/django/CAPgraph/run.sh"]

和docker run命令:

sudo docker run -d --restart=always -v /home/lmartell/devel1/Repos/CAPgraph:/opt/django/CAPgraph -v /home/lmartell/devel1/AppData/mysql_CAPgraph:/var/lib/mysql -v /etc/localtime:/etc/localtime -v /home/lmartell/devel1/AppData/cap_data:/AppData/cap_data -v /home/lmartell/devel1/bin:/opt/bin -v /home/lmartell/devel1/AppData/stardog_knowledgegraph:/AppData/stardog_knowledgegraph -p 18080:80 -p 18000:18000 -p 15820:5820 -p 3306:3306 --name devel1 elucidbio/capgraph:devel

从我连接的其他容器运行命令:

sudo docker run -d --restart=always --link devel1 -v /home/lmartell/devel1/AppData:/AppData -v /home/lmartell/devel1/AppData:/projects -v /home/lmartell/devel1/Repos/CAPbase/capcompute:/opt/capcompute -v /home/lmartell/devel1/Repos/CAPgraph:/opt/CAPgraph -v /home/lmartell/devel1/Repos:/opt/django -v /home/lmartell/devel1/bin:/opt/bin -v /etc/localtime:/etc/localtime -v /bartok_SDBexports:/bartok_SDBexports --name capcompute-devel1 capcompute:latest

1 个答案:

答案 0 :(得分:0)

不要使用IP地址在内部引用服务。如果您创建一个mysql服务容器并为其指定名称&#34; db&#34;你可以通过名称&#34; db&#34;来访问它。在你的其他容器中。

我还建议首先创建一个新的docker网络,在docker run语句中为其分配容器,然后你应该没有相互通信的问题。

这最初仅适用于docker服务,但他们添加了功能以弃用链接。