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.