I am trying to create my own Docker image with MSSQL DB for development. It's based on microsoft/mssql-server-linux
image. During the build I want to copy some .sql
files into the container and then run these scripts (to create DB schemas, tables, insert some data etc.). My Dockerfile
looks like this:
# use MSSQL 2017 image on Ubuntu 16.04 FROM microsoft/mssql-server-linux:2017-latest # create directory within SQL container for database files RUN mkdir -p /opt/mssql-scripts # copy the database files from host to container COPY sql/000_create_db.sql /opt/mssql-scripts # set environment variables ENV MSSQL_SA_PASSWORD=P@ssw0rd ENV ACCEPT_EULA=Y # run initial scripts RUN /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'P@ssw0rd' -i /opt/mssql-scripts/000_create_db.sql
Content of 000_create_db.sql
is not important in my opinion.
The real problem is when I am trying to build this Dockerfile
with command docker build -t demo .
I always get these errors:
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired. Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : TCP Provider: Error code 0x2749. Sqlcmd: Error: Microsoft ODBC Driver 13 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..
But when I delete the last command (running initial scripts), build and run image, and call the same command like this:
docker build -t demo . docker run -p 1433:1433 --name mssql -d demo docker exec -it mssql "bash" /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'P@ssw0rd' -i /opt/mssql-scripts/000_create_db.sql
Then everything is going well. Why cannot I run script from Dockefile
?
Initialize SQL server in Docker container Line #1: Run a command to create a new directory creating all the directories in the path. Line #2: Copy all the SQL files into the created directory. Line #4: Set the working directory for the subsequent instructions. Line #6: Starts the SQL server.
Here are the steps you can follow to set up and deploy a SQL Server Docker Container seamlessly: SQL Server Docker Setup: Install Docker on your System. SQL Server Docker Setup: Execute and Run Docker. SQL Server Docker Setup: Pull & Run the Docker Image for SQL Server.
You can look at the SQL Server setup and error logs in /var/opt/mssql/log. If the container isn't running, first start the container. Then use an interactive command-prompt to inspect the logs. You can get the container ID by running the command docker ps .
I ended up using a slightly modified version of VDR's solution which waits for the sqlservr to start by checking the logs instead of sleeping 10 seconds:
RUN ( /opt/mssql/bin/sqlservr --accept-eula & ) | grep -q "Service Broker manager has started" \ && /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'P@ssw0rd' -i /opt/mssql-scripts/000_create_db.sql \ && pkill sqlservr
From the mssql-server-linux dockerfile, looks like mssql is started on docker run, so you have to modify your last "RUN" command in your dockerfile to start sql-server in the background, run your sql file and stop the sql-server.
RUN /opt/mssql/bin/sqlservr --accept-eula & sleep 10 \ && /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'P@ssw0rd' -i /opt/mssql-scripts/000_create_db.sql \ && pkill sqlservr
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With