0

I'm trying to have a SQL Server Docker container run and have a database backup file restored using Terraform. So far I can get the SQL server Docker image to download and run as a container but I can't get the sqlcmd to run inside the container. I have it set up where the sqlcmd runs automatically when the container starts. This is the log message I get in the container:

Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2749.
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..
SQL Server 2019 will run as non-root by default.

This container is running as user mssql.
To learn more visit https://go.microsoft.com/fwlink/?linkid=2099216.

This is my terraform file:

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "2.16.0"
    }
  }
}

provider "docker" {
  host = "npipe:////.//pipe//docker_engine"
}

resource "docker_image" "mssql" {
  name         = "mcr.microsoft.com/mssql/server:2019-latest"
  keep_locally = true
}

resource "docker_container" "mssql" {
  image = docker_image.mssql.latest
  name  = "sqldatabase"
  attach = false
  rm = false
  restart = "no"
  command = [ "/opt/mssql-tools/bin/sqlcmd", "-S localhost", "-U mssql", "-P <StrongPassword>" ]
  env = [
    "ACCEPT_EULA=Y",
    "MSSQL_SA_PASSWORD=<StrongPassword>"
  ]
  ports {
    internal = 1433
    external = 1433
  }
}

I'm following this Microsoft tutorial about restoring a SQL Server database in a Docker container: https://learn.microsoft.com/en-us/sql/linux/tutorial-restore-backup-in-sql-server-container?view=sql-server-ver15

Right now I'm just trying to run sqlcmd in the container to see if it works then I'll try restoring an actual database backup. How do I get the sqlcmd to work? I'm brand new to Terraform.

UPDATE: As @Nick.McDermaid suggested, I ran the sqlcmd manually in the container and it worked. Also the username I had was wrong. It should be SA, not mssql. I got throw off because in the container log it says This container is running as user mssql. which must be different from a SQL Server username. As @AlwaysLearning suggested it is a timing issue, Terraform starts the Docker container and runs sqlcmd when SQL Server is not ready. I guess I need some kind of entrypoint.sh file that Terraform can run.

mdailey77
  • 1,673
  • 4
  • 26
  • 52
  • `sqlcmd` seems to be working fine; what isn't is the connection string you gave it. For what ever reason the docker container cannot connect to the (remote?) host that is running SQL Server – Thom A Feb 27 '22 at 21:37
  • Does this answer your question? [Cannot Connect to Server - A network-related or instance-specific error](https://stackoverflow.com/questions/18060667/cannot-connect-to-server-a-network-related-or-instance-specific-error) – Thom A Feb 27 '22 at 21:38
  • You'll need to do some further troubleshooting. I guess either the SQL Server is not running or `localhost` isn't resolving. If you follow this link by running manual commands can you connect? https://learn.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker?view=sql-server-ver15&pivots=cs1-bash#connect-to-sql-server Perhaps there is a timing issue – Nick.Mc Feb 27 '22 at 21:45
  • 1
    Aside... with `"-U mssql"` at what point do you create an SQL Login named `mssql` with the same password as the `sa` account? – AlwaysLearning Feb 27 '22 at 21:48
  • 2
    Be aware that the SQL Server instance doesn't have a server socket listening and accepting network connections at the exact microsecond a container starts. It goes through a process of verifying and upgrading system and user database files to the latest file version before it creates a server socket. Microsoft's [Configure and customize SQL Server Docker containers](https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-docker-container-configure) links to [twright-msft/mssql-node-docker-demo-app](https://github.com/twright-msft/mssql-node-docker-demo-app) which shows how to wait. – AlwaysLearning Feb 27 '22 at 21:58

1 Answers1

0

Please replace command = [ "/opt/mssql/bin/sqlservr","--accept-eula"]

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 04 '22 at 12:43