sql-server - Unable to connect to remote SQL server from container
问题描述
I'm trying to connect to my remote SQL server from a docker container hosted on my computer.
But are just reciving the following error:
A network-related or in stance-specific error has occurred while establishing a connection to SQL Server . Server is not found or not accessible. Check if instance name is correct and i f SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.. Sqlcmd: Error: Microsoft SQL Server Native Client 10.0 : Login timeout expired.
But if I try to connect to my SQL server from SQL server management studio on the host machine everything works properly. Also note that 2 weeks ago everything also worked inside the docker container.
Here is my docker compose file and the docker file which has the SQL driver installed:
Compose:
version: '3'
services:
nginx:
image: nginx:1.10
volumes:
- ./:/var/www
- .docker/nginx/vhost.conf:/etc/nginx/conf.d/default.conf
ports:
- ${DOCKER_IP}80:80
links:
- php
networks:
- app-net
php:
build:
context: ./
dockerfile: .docker/php/DockerFile
volumes:
- ./:/var/www
networks:
- app-net
networks:
app-net:
driver: bridge
Docker file
FROM phpdockerio/php71-fpm:latest
# Install selected extensions and other stuff
RUN export DEBIAN_FRONTEND=noninteractive && \
apt-get update && apt-get -y --no-install-recommends install \
php7.1-mysql \
php7.1-mbstring \
php7.1-gd \
php7.1-soap \
php7.1-dev \
apt-transport-https \
git \
ssh \
curl \
php-pear \
&& apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
# Install Composer
RUN cd /usr/src
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Install MSSQL extention
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update
RUN ACCEPT_EULA=Y apt-get -y install msodbcsql mssql-tools g++ unixodbc-dev make
RUN pear config-set php_ini `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"` system
RUN pecl install sqlsrv
RUN pecl install pdo_sqlsrv
RUN echo "extension=sqlsrv.so" >> /etc/php/7.1/fpm/php.ini
RUN echo "extension=pdo_sqlsrv.so" >> /etc/php/7.1/fpm/php.ini
# Fixed locals for MSSQL extention
RUN apt-get install -y locales
RUN echo "en_US.UTF-8 UTF-8" > /etc/locale.gen
RUN locale-gen
WORKDIR /var/www
Inside the docker container I can't ping the SQL server. So for me i sounds like a network issue, but I'm unable find a solution.
Please note the SQL server is hosted locally on a server in the office.
UPDATE/Solved for now After downgrading the dokcer for windows to 18.03.0-CE everything worked as expected.
解决方案
默认情况下,Docker Bridge 网络根本不连接到 Docker 之外的世界;它们只允许容器相互通信。Docker Bridge Networks的文档确实提供了一些建议,通过在 Docker 主机上进行更改来允许 Bridge 网络流量与外界通信:
首先在内核中启用IP转发:
$ sysctl net.ipv4.conf.all.forwarding=1
然后更改主机防火墙以允许转发
$ sudo iptables -P FORWARD ACCEPT
这是一个相当宽松的防火墙配置,因此您可能希望将其锁定更多。
另一种方法是将容器附加到两个不同的网络:当前用于容器之间通信的桥接网络,以及用于与 MySQL 通信的第二个主机网络。由于这绕过了 Docker 的所有 NAT 配置,因此您的服务的可扩展性可能会受到影响,尽管我认为传出连接可能没问题。
推荐阅读
- c++ - C++ 将派生类的成员函数指针作为参数传递时选择了错误的模板特化
- java - 为对象创建运行除 toString 以外的方法的自定义 JList
- c# - 使用 Azure SDK for .Net ResourceManagementClient 进行身份验证
- youtube - youtube 应用程序中的 360 度视频卡顿
- arrays - Golang graphql用子图迭代地图
- python - 我可以在管理员之外使用 StreamField 编辑器吗?
- python - python:返回没有给定元素的列表的函数
- dynamics-crm - 工作流是否保证以先进先出的顺序执行?(CRM 2011 RU18 本地)
- node.js - 如何在反应构建中使用代理?
- vba - 使用 VBA Like 运算符比较两个变量中的值