Anders Andersen
Anders Andersen

Reputation: 2497

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.

Upvotes: 11

Views: 8503

Answers (3)

user10278644
user10278644

Reputation:

If you are using Windows 10 (HOME VERSION ) may you don't have the virtualization and you are using (docker toolbox ), they have solved the problem using a static ip. Check it. I mean localhost won't work if you are using docker toolbox, search the ip where is virtualized docker toolbox.

My case ip: 192.168.99.100

Upvotes: 0

For my case: I ran these below:

To see all containers:

docker ps -a

Then restart container:

docker container restart yourIdContainer

If there is error: Error response from daemon: Cannot restart container ...

Please restart Docker then restart container again.

Connect to Sql Server by MSSM, server name: localhost,1433 or IP

Hope this is helpful.

Upvotes: 0

CantankerousBullMoose
CantankerousBullMoose

Reputation: 512

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.

Upvotes: 2

Related Questions