pre-req to this blog:
install docker, I use docker desktop for windows. You can find everything you need to get started with this on the docker home page:
install visual studio code. I find this is really useful when writing custom YAML scripts that we will be doing as part of this blog. You can find out more info on how to download here:
Ok.. so we have docker installed- the great thing about docker desktop is that it already installs docker-compose as part of that- which is what we'll be using to install SQL Server!
Don't worry... I hear you... "What on earth is docker-compose?" well with docker compose you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration. Pretty cool stuff!
Ready to get started?
Creating your directories
Open up power shell and run the following command
mkdir mssql
then in the same command window change to this directory
cd mssql
we can then open visual studio code within this directory. In the command window type in
code .
Visual studio code will then magically open!
Create your docker compose file
In visual studio code you should see on the left hand side "MSSQL" expand the ">" arrow which should give you an add file icon.
Click on the add file icon and type in the below for the file name "docker-compose.yml" this will then open a window where you can begin to type your yaml code
![](https://static.wixstatic.com/media/7a082c_f23b4cb7eaaf423787fbfc1d0cf90130~mv2.png/v1/fill/w_770,h_232,al_c,q_85,enc_avif,quality_auto/7a082c_f23b4cb7eaaf423787fbfc1d0cf90130~mv2.png)
Constructing your YAML code
One thing to note with YAML is that it's really important to get your indentations right. If you copy the below in to visual studio code and save it this should do the trick- I've added comments to hopefully give you insight into what each line does!
version: "3.7"
services: #Servuces is for example your database
sql-server-db: #indentation is important here as this falls under services- service name name is placed here
container_name: sql-server-db #indentation is required as this falls under the Sql service above. Enter a container name
image: mcr.microsoft.com/mssql/server:2019-latest #Docker image you require this will grab the latest version for SQL 2019
ports:
- "1433:1433" #Ports for SQL server- we will keep these as the default 1433
environment: #Environment variables required to configure SQL Service
SA_PASSWORD: "St0ngP@55w0rd" #SA Admin password is a pre-req and is required for it to be installed
ACCEPT_EULA: "Y" #Accept end user license agreement- again a pre-req to be installed
Please note that indentation is extremely important in YAML. Without it you'll get all kinds of funky errors and will be scratching your head wondering where it all went wrong!
Make sure your indentation is set similar to the screenshot below
![](https://static.wixstatic.com/media/7a082c_e7fc3f4eabf64f8fa87dbd616f1b17f6~mv2.jpg/v1/fill/w_980,h_212,al_c,q_80,usm_0.66_1.00_0.01,enc_avif,quality_auto/7a082c_e7fc3f4eabf64f8fa87dbd616f1b17f6~mv2.jpg)
the way i like to remember it is:
Services define what services you'll configure within your template.
sql-server-db is a service which falls under services (therefore an indentation should be used)
under sql-server-db you then have your configuration for that particular service- again because the container_name, image and ports fall under the sql-server-db an indentation should be used.
Once your YAML code looks like the above screenshot remember to click save.
We are ready to run!
in visual studio code click on terminal on the top menu > new terminal (alternatively if your a big fan of short cuts do the following: CTL + SHIFT + ' ) this will open up a windows powershell window at the bottom. Run the following
docker-compose up -d
once this is run this should magically create your sql-server-db. To verify if this is running run the following within the visual studio terminal
docker ps
![](https://static.wixstatic.com/media/7a082c_a3011fb965774c47a2cacbb4d968af4e~mv2.png/v1/fill/w_980,h_48,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/7a082c_a3011fb965774c47a2cacbb4d968af4e~mv2.png)
you should be able to see an entry for sql-server-db and that the status is "Up"
You can then use SQL Server Management studio to connect to your new SQL instance using the password details that were set up for the SA account in your docker compose file.
![](https://static.wixstatic.com/media/7a082c_6fd786ebd6f1491784f8323cb9fdb227~mv2.png/v1/fill/w_415,h_215,al_c,q_85,enc_avif,quality_auto/7a082c_6fd786ebd6f1491784f8323cb9fdb227~mv2.png)
And there you have it.. super simple to begin with but we've just managed to install SQL Server using docker!
Please note you don't always have to use the "latest" version of SQL. You can see more details on the below link https://hub.docker.com/_/microsoft-mssql-server which will give you more examples should you wish to customise this slightly!
Yorumlar