11

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.

4
  • There is no connection info in these files: server name or address, port, instance name, database, etc. Maybe you haven't configured the MSSQL extension. The error says the client is unable to find the server. Commented May 21, 2018 at 13:18
  • What's the os and docker version running on your host machine? Commented May 21, 2018 at 14:01
  • it seems like you are facing similar issue like this Commented May 23, 2018 at 5:57
  • Sorry for the late reply, I have been on a longer vacation. The MSSQL extension is configured correcly and the information for the SQL server is correct. I'm running windows 10 as the host machine. Commented May 28, 2018 at 9:44

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Are you running theese commands inside the container or on the host machine? I'm using Windows 10, as the host machine where i tried disable the firewall.
The commands should be run on the host. Disabling the firewall should have a similar effect to the iptables command, but I'm not sure where to go in Windows to set up IP forwarding. The process is similar to what you would do to set up the host machine to act as a router. Maybe that will point you in the right direction for finding the windows settings?
10x life saver, I was close to jump after the train :-)
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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.