Question:
How to connect the Sql Server in the Docker container?

Solution


The following steps will help you connect the SQL Server to the Docker container. 


 In your running container of the web server, start an interactive bash shell.


docker exec -it 523 'bash'


After this you need to install mssql-tools (refer to this>> >doc)


sudo ACCEPT_EULA=Y apt-get install -y mssql-tools18


Lastly, you need to connect to the SQL Server instance by using the following command:


/opt/mssql-tools18/bin/sqlcmd -S 172.23.0.2,1433 -U SA -P 'Pass@word' -No


where the internal port is 1433 and the internal IP address assigned to the database in Docker is 172.23.0.2. The database container has a self-signed certificate, so I had to use the -No option; otherwise, I get the following error:


Sqlcmd: Error: Microsoft ODBC Driver 18 for SQL Server : SSL Provider: [error:0A000086:SSL routines::certificate verify failed:self-signed certificate].


Add 2 connection properties in the .env file in Laravel


DB_ENCRYPT=no

DB_TRUST_SERVER_CERTIFICATE=true


The same goes for the database.php file

'sqlsrv' => [

    'driver' => 'sqlsrv',

    'url' => env('DATABASE_URL'),

    'host' => env('DB_HOST', 'localhost'),

    'port' => env('DB_PORT', '1433'),

    'database' => env('DB_DATABASE', 'forge'),

    'username' => env('DB_USERNAME', 'forge'),

    'password' => env('DB_PASSWORD', ''),

    'charset' => 'utf8',

    'prefix' => '',

    'prefix_indexes' => true,

    'encrypt' => env('DB_ENCRYPT', 'yes'),

    'trust_server_certificate' => env('DB_TRUST_SERVER_CERTIFICATE', 'false'),

],



Eventually, after launching my containers, I was able to connect to SQL Server Management Studio using the connection panel's parameters: 

  • server name = localhost,5434 

  • disable encryption

  • trust server certificate


where 5434 is the port exposed out of the db container see docker-compose file


sql-server-db:

    container_name: sql-server-db

    image: mcr.microsoft.com/mssql/server:2019-latest

    environment:

      - SA_PASSWORD=Pass@word

      - ACCEPT_EULA=Y

      - MSSQL_PID=Express

    ports:

      - "5434:1433"

    networks:

      - sail



Answered by:>> >agodoo

Credit:>> >Stack Overflow


Read more:

>Use Firebase Realtime Database with ASP.NET MVC App

>Setting up a Cloud Composer environment: Step-by-step guide

>Testing react components using Hooks and Mocks

>Plugins and Presets for Vuejs project

>How to Create an array based on two other arrays in Php

>Built Web API using ASP.NET (C#)


Ritu Singh

Ritu Singh

Submit
0 Answers