|
@@ -5,38 +5,38 @@ author: Vin Yu
|
|
|
---
|
|
|
# SQL Server Containers Lab
|
|
|
|
|
|
+This lab use the **Putty** program, but you can use any ssh program you want.
|
|
|
+
|
|
|
## Pre Lab
|
|
|
1. Install docker engine by running the following:
|
|
|
|
|
|
-```
|
|
|
-sudo yum install -y yum-utils device-mapper-persistent-data lvm2
|
|
|
|
|
|
-sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
|
|
|
+`sudo yum install -y yum-utils device-mapper-persistent-data lvm2`
|
|
|
|
|
|
-sudo yum install http://mirror.centos.org/centos/7/extras/x86_64/Packages/pigz-2.3.3-1.el7.centos.x86_64.rpm
|
|
|
+`sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo`
|
|
|
|
|
|
-sudo yum install docker-ce
|
|
|
-```
|
|
|
+`sudo yum install http://mirror.centos.org/centos/7/extras/x86_64/Packages/pigz-2.3.3-1.el7.centos.x86_64.rpm`
|
|
|
+
|
|
|
+`sudo yum install docker-ce`
|
|
|
|
|
|
check status of docker engine:
|
|
|
-```
|
|
|
-sudo systemctl status docker
|
|
|
- ```
|
|
|
+
|
|
|
+`sudo systemctl status docker`
|
|
|
|
|
|
if is not running, start it by running:
|
|
|
-```
|
|
|
-sudo systemctl start docker
|
|
|
-```
|
|
|
+
|
|
|
+`sudo systemctl start docker`
|
|
|
+
|
|
|
>Note: for this lab, we are installing docker for CentOS, this will work on CentOS or RHEL due to the similarity of the OS’s. For production usage on RHEL, install Docker EE for RHEL: https://docs.docker.com/install/linux/docker-ee/rhel/.
|
|
|
|
|
|
2. clone this repo by running the following:
|
|
|
|
|
|
- Note: If you have already done this in the prelab you can skip this step
|
|
|
+`sudo yum install git`
|
|
|
+
|
|
|
+`git clone https://github.com/Microsoft/tigertoolbox.git`
|
|
|
+
|
|
|
+`unzip tigertoolbox/Sessions/Winter-Ready-2019/Labs/Lab-Containers.zip`
|
|
|
|
|
|
-```
|
|
|
-sudo yum install git
|
|
|
-git clone https://github.com/Microsoft/sqllinuxlabs.git
|
|
|
-```
|
|
|
---
|
|
|
|
|
|
## Lab
|
|
@@ -46,12 +46,9 @@ git clone https://github.com/Microsoft/sqllinuxlabs.git
|
|
|
In this section you will run SQL Server in a container and connect to it with SSMS/SQL Operations Studio. This is the easiest way to get started with SQL Server in containers.
|
|
|
|
|
|
#### Steps
|
|
|
-1. Change the `SA_PASSWORD` in the command below and run it in your terminal:
|
|
|
-```
|
|
|
-sudo docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=YourStrong!Passw0rd' \
|
|
|
- -p 1500:1433 --name sql1 \
|
|
|
- -d microsoft/mssql-server-linux:2017-latest
|
|
|
- ```
|
|
|
+1. Change the **SA_PASSWORD** in the command below and run it in your terminal:
|
|
|
+
|
|
|
+ `sudo docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=YourStrong!Passw0rd' -p 1500:1433 --name sql1 -d microsoft/mssql-server-linux:2017-latest`
|
|
|
|
|
|
> Tip: edit commands in a text editor prior to pasting in the terminal to easily edit the commands.
|
|
|
>
|
|
@@ -59,9 +56,8 @@ sudo docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=YourStrong!Passw0rd' \
|
|
|
|
|
|
|
|
|
2. Check that SQL Server is running:
|
|
|
-```
|
|
|
-sudo docker ps
|
|
|
-```
|
|
|
+
|
|
|
+ `sudo docker ps`
|
|
|
|
|
|

|
|
|
|
|
@@ -69,28 +65,23 @@ sudo docker ps
|
|
|
|
|
|
Open SSMS or Ops studio and connect to the SQL Server in container instance by connecting host:
|
|
|
|
|
|
-```
|
|
|
-<host IP>, 1500
|
|
|
-```
|
|
|
->Note: If you are running this in an Azure VM, the host IP is the public Azure VM IP. You will also need to open port 1500 external traffic. [go here to learn how to open ports in Azure VMs](/open_azure_vm_port)
|
|
|
+`<host IP or name>, 1500`
|
|
|
|
|
|

|
|
|
|
|
|
3. Run SQLCMD inside the container. First run bash interactively in the container with docker execute 'bash' inside 'sql1' container.
|
|
|
|
|
|
-```
|
|
|
-sudo docker exec -it sql1 bash
|
|
|
-```
|
|
|
+`sudo docker exec -it sql1 bash`
|
|
|
+
|
|
|
Use SQLCMD within the container to connect to SQL Server:
|
|
|
-```
|
|
|
-/opt/mssql-tools/bin/sqlcmd -U SA -P 'YourStrong!Passw0rd'
|
|
|
-```
|
|
|
+
|
|
|
+`/opt/mssql-tools/bin/sqlcmd -U SA -P 'YourStrong!Passw0rd'`
|
|
|
+
|
|
|

|
|
|
|
|
|
Exit SQLCMD and the container with exit:
|
|
|
-```
|
|
|
-exit
|
|
|
-```
|
|
|
+
|
|
|
+`exit`
|
|
|
|
|
|
|
|
|
> **Key Takeaway**
|
|
@@ -107,29 +98,29 @@ exit
|
|
|
Enter the following commands in your terminal.
|
|
|
|
|
|
See the active container instances:
|
|
|
-```
|
|
|
-sudo docker ps
|
|
|
-```
|
|
|
+
|
|
|
+`sudo docker ps`
|
|
|
+
|
|
|
List all container images:
|
|
|
-```
|
|
|
-sudo docker image ls
|
|
|
-```
|
|
|
+
|
|
|
+`sudo docker image ls`
|
|
|
+
|
|
|
Stop the SQL Server container:
|
|
|
-```
|
|
|
-sudo docker stop sql1
|
|
|
-```
|
|
|
-See that `sql1` is no longer running by listing all containers:
|
|
|
-```
|
|
|
-sudo docker ps -a
|
|
|
-```
|
|
|
+
|
|
|
+`sudo docker stop sql1`
|
|
|
+
|
|
|
+See that **sql1** is no longer running by listing all containers:
|
|
|
+
|
|
|
+`sudo docker ps -a`
|
|
|
+
|
|
|
Delete the container:
|
|
|
-```
|
|
|
-sudo docker rm sql1
|
|
|
-```
|
|
|
+
|
|
|
+`sudo docker rm sql1`
|
|
|
+
|
|
|
See that the container no longer exists:
|
|
|
-```
|
|
|
-sudo docker container ls
|
|
|
-```
|
|
|
+
|
|
|
+`sudo docker container ls`
|
|
|
+
|
|
|

|
|
|
|
|
|
> **Key Takeaway**
|
|
@@ -156,13 +147,13 @@ Scenario: Let's say for testing purposes you want to start the container with th
|
|
|
|
|
|
#### Steps:
|
|
|
|
|
|
-1. Change directory to the `mssql-custom-image-example folder`.
|
|
|
-```
|
|
|
-cd sqllinuxlabs/containers/mssql-custom-image-example/
|
|
|
-```
|
|
|
+1. Change directory to the *mssql-custom-image-example folder*.
|
|
|
+
|
|
|
+ `cd containers/mssql-custom-image-example/`
|
|
|
|
|
|
|
|
|
2. Create a Dockerfile with the following contents
|
|
|
+
|
|
|
```
|
|
|
cat <<EOF>> Dockerfile
|
|
|
FROM microsoft/mssql-server-linux:latest
|
|
@@ -173,25 +164,25 @@ EOF
|
|
|
|
|
|
3. View the contents of the Dockerfile
|
|
|
|
|
|
-```
|
|
|
-cat Dockerfile
|
|
|
-```
|
|
|
+`cat Dockerfile`
|
|
|
+
|
|
|

|
|
|
|
|
|
4. Run the following to build your container
|
|
|
-```
|
|
|
-sudo docker build . -t mssql-with-backup-example
|
|
|
-```
|
|
|
+
|
|
|
+`sudo docker build . -t mssql-with-backup-example`
|
|
|
+
|
|
|

|
|
|
|
|
|
-5. Start the container by running the following command after replacing `SA_PASSWORD` with your password
|
|
|
+5. Start the container by running the following command after replacing **SA_PASSWORD** with your password
|
|
|
+
|
|
|
```
|
|
|
sudo docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=YourStrong!Passw0rd' \
|
|
|
-p 1500:1433 --name sql2 \
|
|
|
-d mssql-with-backup-example
|
|
|
```
|
|
|
|
|
|
-6. Edit the `-P` with the value used for `SA_PASSWORD` used in the previous command and view the contents of the backup file built in the image:
|
|
|
+6. Edit the **-P** with the value used for **SA_PASSWORD** used in the previous command and view the contents of the backup file built in the image:
|
|
|
|
|
|
```
|
|
|
sudo docker exec -it sql2 /opt/mssql-tools/bin/sqlcmd -S localhost \
|
|
@@ -202,18 +193,21 @@ sudo docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=YourStrong!Passw0rd' \
|
|
|
```
|
|
|
|
|
|
the output of this command should be similar to this:
|
|
|
+
|
|
|
>LogicalName PhysicalName
|
|
|
>----------- ------------
|
|
|
>ProductCatalog /var/opt/mssql/data/ProductCatalog.mdf
|
|
|
>ProductCatalog_log /var/opt/mssql/data/ProductCatalog_log.ldf
|
|
|
|
|
|
-7. Edit the `-P` with the value of `SA_PASSWORD` used to start the container and restore the database in the container:
|
|
|
+7. Edit the **-P** with the value of **SA_PASSWORD** used to start the container and restore the database in the container:
|
|
|
+
|
|
|
```
|
|
|
sudo docker exec -it sql2 /opt/mssql-tools/bin/sqlcmd \
|
|
|
-S localhost -U SA -P YourStrong!Passw0rd \
|
|
|
-Q 'RESTORE DATABASE ProductCatalog FROM DISK = "/var/opt/mssql/data/SampleDB.bak" WITH MOVE "ProductCatalog" TO "/var/opt/mssql/data/ProductCatalog.mdf", MOVE "ProductCatalog_log" TO "/var/opt/mssql/data/ProductCatalog.ldf"'
|
|
|
|
|
|
```
|
|
|
+
|
|
|
the output of this command should be similar to
|
|
|
|
|
|
>Processed 384 pages for database 'ProductCatalog', file 'ProductCatalog' on file 1.
|
|
@@ -227,10 +221,10 @@ If you connect to the instance, you should see that the database was restored.
|
|
|

|
|
|
|
|
|
7. Clean up the container
|
|
|
-```
|
|
|
-sudo docker stop sql2
|
|
|
-sudo docker container rm sql2
|
|
|
-```
|
|
|
+
|
|
|
+`sudo docker stop sql2`
|
|
|
+
|
|
|
+`sudo docker container rm sql2`
|
|
|
|
|
|
|
|
|
> **Key Takeaway**
|
|
@@ -240,79 +234,71 @@ sudo docker container rm sql2
|
|
|
> -- https://docs.docker.com/get-started/part2/#your-new-development-environment
|
|
|
---
|
|
|
|
|
|
- ### 4. Run a Containerized Application with SQL Server
|
|
|
+### 4. Run a Containerized Application with SQL Server
|
|
|
|
|
|
- #### Introduction
|
|
|
+#### Introduction
|
|
|
|
|
|
Most applications involve multiple containers.
|
|
|
|
|
|
#### Steps
|
|
|
|
|
|
1. Install docker-compose:
|
|
|
-```
|
|
|
-sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
|
|
|
|
|
|
-sudo chmod +x /usr/local/bin/docker-compose
|
|
|
+ `sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose`
|
|
|
+
|
|
|
+ `sudo chmod +x /usr/local/bin/docker-compose`
|
|
|
+
|
|
|
+ `sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose`
|
|
|
|
|
|
-sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
|
|
|
-```
|
|
|
|
|
|
|
|
|
1. Change directory to the mssql-aspcore-example.
|
|
|
|
|
|
-```
|
|
|
-cd sqllinuxlabs/containers/mssql-aspcore-example
|
|
|
-```
|
|
|
+`cd containers/mssql-aspcore-example`
|
|
|
|
|
|
>note: if you just finished the **Build your own container** lab, you can navigate to this folder with the following command:
|
|
|
>
|
|
|
-> `cd ../../containers/mssql-aspcore-example `
|
|
|
+> `cd ../mssql-aspcore-example `
|
|
|
|
|
|
2. Open the docker-compose.yml file
|
|
|
-```
|
|
|
-nano docker-compose.yml
|
|
|
-```
|
|
|
|
|
|
-3. Edit the `SA_PASSWORD` SQL Server environment variables then save the file with `ctrl + x`
|
|
|
+`nano docker-compose.yml`
|
|
|
+
|
|
|
+3. Edit the **SA_PASSWORD** SQL Server environment variables then save the file with **ctrl + x**
|
|
|
|
|
|

|
|
|
|
|
|
-4. Edit the `-P` parameter in the `./mssql-aspcore-example-db/db-init.sh` file with the `SA_PASSWORD` that you used in the previous step
|
|
|
-```
|
|
|
-nano ./mssql-aspcore-example-db/db-init.sh
|
|
|
-```
|
|
|
+4. Edit the **-P** parameter in the **./mssql-aspcore-example-db/db-init.sh** file with the **SA_PASSWORD** that you used in the previous step
|
|
|
+
|
|
|
+ `nano ./mssql-aspcore-example-db/db-init.sh`
|
|
|
|
|
|

|
|
|
|
|
|
4. Run the containers with docker-compose:
|
|
|
-```
|
|
|
-sudo docker-compose up
|
|
|
-```
|
|
|
+
|
|
|
+ `sudo docker-compose up`
|
|
|
+
|
|
|
>note: this will take approx. 15 seconds
|
|
|
|
|
|
|
|
|
5. At this point, you will have two containers up and running: an application container that is able to query the database container. Connect to the
|
|
|
|
|
|
-```
|
|
|
-http:<host IP>:5000
|
|
|
-```
|
|
|
->Note: If you are running this in an Azure VM, the host IP is the Azure VM Public IP. You will also need to open port 5000 external traffic. [go here to learn how to open ports in Azure VMs](/open_azure_vm_port) (be sure to open port 5000!)
|
|
|
+ `http:<host IP/name>:5000`
|
|
|
+
|
|
|
|
|
|

|
|
|
|
|
|
-To stop the docker compose application, press `ctrl + c` in the terminal.
|
|
|
+To stop the docker compose application, press **ctrl + c** in the terminal.
|
|
|
To remove the containers run the following command:
|
|
|
|
|
|
-```
|
|
|
-sudo docker-compose down
|
|
|
-```
|
|
|
+`sudo docker-compose down`
|
|
|
|
|
|
|
|
|
### Start-up Explanation
|
|
|
|
|
|
-1. Running `docker-compose up` builds/pulls containers and run them with parameters defined in docker-compose.yml
|
|
|
+1. Running **docker-compose up** builds/pulls containers and run them with parameters defined in docker-compose.yml
|
|
|
2. The .Net Core application container starts up
|
|
|
-3. The SQL Server container starts up with `entrypoint.sh`
|
|
|
+3. The SQL Server container starts up with **entrypoint.sh**
|
|
|
|
|
|
a. The sqlservr process starts
|
|
|
|