Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 395
Anders Andersen Avatar asked May 14 '18 06:05

Anders Andersen


People also ask

Can't connect remotely SQL Server?

Check to see if allow remote connections for this server is enabled. In SSMS, right click on the instance name and select Properties. Go to the Connections tab and make sure Allow remote connections to this server is checked. If you need to make a change, you must restart the SQL Server instance to apply the change.


1 Answers

Docker Bridge networks don't connect to the world outside of Docker at all by default; they only allow containers to talk to each other. The documentation for Docker Bridge Networks does offer some advice for allowing Bridge network traffic to talk to the outside world by making changes on the Docker host:

First enable IP forwarding in the kernel:

$ sysctl net.ipv4.conf.all.forwarding=1

Then change the host firewall to allow the forwarding

$ sudo iptables -P FORWARD ACCEPT

This is a fairly permissive firewall configuration, so you may want to look at keeping that a bit more locked down.

The other way would be to attach the container to two different networks: your current bridge network for communication between container, and a second Host network for talking to MySQL. Since this bypasses all of Docker's NAT configuration, the scalability of your service may be impacted, although I think outgoing connections might be OK.

like image 183
CantankerousBullMoose Avatar answered Sep 23 '22 08:09

CantankerousBullMoose