DEV Community

Henrique Holtz
Henrique Holtz

Posted on

[extra-basic] How to create a SQL Server instance with docker container (linux based)

Hello dears, in this simple article we'll see how to create a SQL Server instance into a docker container (linux based). We'll use the mcr.microsoft.com/mssql/server:2022-latest image. Let's go!

  1. Create the Dockerfile
FROM mcr.microsoft.com/mssql/server:2022-latest

ENV ACCEPT_EULA=Y
ENV MSSQL_SA_PASSWORD=MyStrong@Passw0rd
ENV MSSQL_PID=Developer
ENV MSSQL_TCP_PORT=1433

WORKDIR /src

RUN (/opt/mssql/bin/sqlservr --accept-eula & ) | grep -q "Service Broker manager has started" &&  /opt/mssql-tools/bin/sqlcmd -S127.0.0.1 -Usa -P MyStrong@Passw0rd
Enter fullscreen mode Exit fullscreen mode
  1. Build our docker image
docker build -t henriqueholtz/sql-linux-2022 .
Enter fullscreen mode Exit fullscreen mode
  1. Run a container with our own docker image
docker run -d -p 1433:1433 --name sqlgen henriqueholtz/sql-linux-2022
Enter fullscreen mode Exit fullscreen mode
  1. Access our container and see make a simple SELECT.
docker exec -it sqlgen bash
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "MyStrong@Passw0rd"
> SELECT name FROM master.dbo.sysdatabases
> GO
Enter fullscreen mode Exit fullscreen mode

If you are using a Windows with WSL2 (because the docker must be running linux containers), you can also access using the SQL Server Management Studio using the server name 127.0.0.1 for example.

Image description

Now, with the SQL instance running, you can create your own databases and/or restore from a backup (.bak) file.

Crédits: [Basic] Docker image 🐳 Dockerfile — SQL Server with custom prefill DB scripts

Top comments (0)