Connect to SQL Server inside Docker image (Linux) whilst in build

Viewed 34

I have a simple docker-compose file which builds a SQL Server database, restores a .bak file.

Now, if I run this independently - i.e. wait for the image to build and exclude the run.sh and run it with docker exec it sql-server-db /opt/mssql-tools/bin/sqlcmd it works no problem.

However, if I run this inside the build process I get

#0 8.435 Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired. #0 8.435 Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2749. #0 8.435 Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : A network-related or instance-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 if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.

Where is my error?

docker-compose

version: '3.9'

services:

  sql-server-db:
    container_name: sql-svr-db
    build: 
      context: .
      dockerfile: docker/mssql.Dockerfile
    ports:
      - "5433:1433"
    volumes:
      - /drive:/var/opt/mssql

mssql.Dockerfile

FROM 'mcr.microsoft.com/mssql/server'

ENV  SA_PASSWORD=Passw0rd!
ENV  ACCEPT_EULA="Y"

USER root

WORKDIR /app/

RUN mkdir -p /var/opt/mssql/backup

COPY /drive/db.bak /var/opt/mssql/backup

ENTRYPOINT [ "/app/run.sh" ]

.run.sh

#!/bin/bash

echo "waiting for sql server to warm up ..."
sleep 45

echo "restoring database... from BAK file"

/opt/mssql-tools/bin/sqlcmd -S localhost -U SA \
    -P 'Passw0rd!' \
    -Q 'RESTORE DATABASE MYDB  FROM DISK = "/var/opt/mssql/backup/db.bak" WITH MOVE "db_DATA" TO "/var/opt/mssql/data/db.mdf", MOVE "db_Log" TO "/var/opt/mssql/data/db.ldf"'
echo "database restored"
0 Answers
Related