Skip to content

DevContainer Support

Shawn Melton edited this page Dec 8, 2020 · 5 revisions

Introduction

PR sqlcollaborative/dbatools PR 6997 introduced the addition of DevContainers support for our repository. This works with both VS Code (remote container development) and GitHub Codespaces.

Note: If you are using Devcontainers for the first time on your machine with VS Code you should be prompted to install the required extensions and dependencies the first time you open our repository.

Prerequisites

Docker Desktop must be installed and running for the Visual Studio Code use; whether you are using stable or insiders build.

Use of GitHub Codespaces is tied to your GitHub account being added to the beta program at this time (2020). It is unknown when/how this will be managed once it becomes generally available by GitHub. Once it does then you just ensure you have enabled/signed up for that feature under your GitHub account.

General environment info

The dockerfile and docker-compose files within the .devcontainer provide exact details on what containers are built and how they are configured. This section is to provide a brief summary and call out a few particular details.

The base image used: mcr.microsoft.com/mssql/server:2019-latest. (You can see the full list of tags here for convenience: https://mcr.microsoft.com/v2/mssql/server/tags/list).

There are three containers of SQL Server created. You can modify the base image if you would like to use a different version, but be aware that some older versions may not play nice with the configuration/modification to the image done in the dockerfile (miles may vary):

  1. dbatools1
  2. dbatools2
  3. dbatools3

Environment Variables

Environment variables have been added to provide an easier reference to the instances. MY_SERVERS list all three instances above. You can reference this in bash shell using %MY_SERVERS% or in PowerShell $env:MY_SERVERS to grab the names. Another variable also exists for the primary instance, dbatools1, that VS Code will join or be loaded into: MY_SERVER.

image

PS /workspace> $servers = $env:MY_SERVERS.Split(',')
PS /workspace> $servers
dbatools1
dbatools2
dbatools3
PS /workspace> 

Login as

All the images have the same sa password based on the environment variable value SA_PASSWORD so you can create one credential to use against all instances:

image

PS /workspace> Import-Module ./dbatools.psd1
PS /workspace> $cred = [pscredential]::new('sa',(ConvertTo-SecureString $env:SA_PASSWORD -AsPlainText -Force))
PS /workspace> $serversCn = Connect-DbaInstance -SqlInstance $servers -SqlCredential $cred
PS /workspace> $serversCn.NetName
ef4266d0e0a7
5dae5ae59158
519bccb6e0fc
PS /workspace> $serverCn | ft

ComputerName Name      Product              Version   HostPlatform IsAzure IsClustered ConnectedAs
------------ ----      -------              -------   ------------ ------- ----------- -----------
dbatools1    dbatools1 Microsoft SQL Server 15.0.4073 Linux        False   False       sa
dbatools2    dbatools2 Microsoft SQL Server 15.0.4073 Linux        False   False       sa
dbatools3    dbatools3 Microsoft SQL Server 15.0.4073 Linux        False   False       sa

PS /workspace> 

ms-mssql.mssql extension

The SQL Server extension for VS Code can be utilized for a more ADS type access to the instance objects (via explorer panel). If you use the VS Code or GitHub Codespaces, utilizing the Sync feature in VS Code will ensure these exists between environments.

image

Test Framework

An update has been made to our constants.ps1 file via PR 7022 that will allow you to run our tests framework against the SQL Server instances. We utilize the constants.ps1 file to set some session variables (i.e., $script:instance1), and these are utilized within each Pester test. This will allow you to run our tests for a given command within the secluded environment of a container.

VS Code Remote Container Development

  1. Clone our repository
  2. Reload VS Code (close, reopen; or utilize Command Palette option)
  3. Once the extensions initialize you should receive a prompt to reopen in a container

image

Being that VS Code reopens the workspace into the container you can either just close the VS Code window or utilize the command palette option to reopen in local:

image

GitHub Codespaces

Note this is in beta, so be aware miles may vary on experience

To test for GitHub Codespaces (requires your account has been added to the beta):

  1. Navigate to the repo on GitHub
  2. Switch to the devcontainer branch
  3. Click the drop-down for Code
  4. Click Open with Codespaces
  5. Click + New codespace

When you first create the codespace,e it will initialize the environment for Codespaces itself, and then begin building the container.

image

If all goes well VS Code (web) will open and the workspace loaded:

image

You can also access this via a PR (for maintainers):

image

If you utilize VS Code sync feature and tied it to your GitHub user all your preferences and extensions will be installed; in addition to the ones set to install by default by the devcontainer definition.