tldnr: setting up docker-for-windows for the first time. mssql server. This is mere a personal braindump. Easy. Probably will use docker in future to replace local mssql server and for some other things.
Setup
Activate the Containers
and Hyper-V
Windows features.
I use choclatey to install all my windows programs. Therefore, installation of docker is like:
cinst docker -y
cinst docker-for-windows -y
Switch from linux to windows containers when using windows containers ;) e.g. MSSQL.
Download
Decide which image you need and download it. This will take time - depending on the size of the image. Fortunately, this has to be done only once.
docker pull microsoft/mssql-server-windows-developer
Start
docker run -d -p 1433:1433 -e sa_password=<SA PASSWORD> -e ACCEPT_EULA=Y --name sql microsoft/mssql-server-windows-developer
! The <SA PASSWORD>
has to match the mssql server password policy. Otherwise, the setup will fail and you have no idea why.
[edit 20.10.2017] ! Those settings will be stored as environment variables on the container. On every startup of the container a script (c:\start.ps1
) will be executed that resets the sa password to this initial password. The initial password, stored as environment variable, can be promted with SET sa_password
.
When no password is passed in the docker run
command the environment varibale is initialized to “_”, the sa user will be disabled and the sa password will not be resetted at startup.
Enable sa user and set password - via sqlcmd (docker exec -it sql sqlcmd
):
1> USE [master]
2> GO
1> ALTER LOGIN [sa] WITH PASSWORD=N'<SA PASSWORD>'
2> GO
1> ALTER LOGIN [sa] ENABLE
2> GO
Use
Connect to localhost.
Since we mapped the container mssql server default port to the host system mssql server default port (-p 1433:1433
) we can access the container via localhost
.
Example connections string:
"Data Source=.;Initial Catalog=MyAppDb;User Id=sa;Password=<SA PASSWORD>;MultipleActiveResultSets=true;"
If needed you can get the container IP with this command:
docker inspect --format '{{.NetworkSettings.Networks.nat.IPAddress}}' sql
Now you can just connect to the mssql server using management studio, visual studio code, …
Useful commands
start the sql container: docker start sql
stop the sql container: docker stop sql
list running containers: docker ps
list all containers: docker ps -a
open a cmd.exe on the container: docker exec -it sql cmd
open sqlcmd on the container: docker exec -it sql sqlcmd
Conclusion
Cool first impression. Will use it much more in the future and challenge it against the real world of software development.