Skip to content

Commit

Permalink
Merge pull request #19 from janegilring/main
Browse files Browse the repository at this point in the history
Added session "Getting started with Dev Containers in VS Code"
  • Loading branch information
SQLDBAWithABeard authored Jun 23, 2022
2 parents 1094740 + a98355d commit c1140c7
Show file tree
Hide file tree
Showing 7 changed files with 360 additions and 0 deletions.
121 changes: 121 additions & 0 deletions JanEgilRing/VSCodeDevContainers/01 - basics.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<#
Prerequisites:
- A computer running Linux, MacOS or Windows
- VS Code
- Docker
Docker - The fastest way to containerize applications on your desktop
https://www.docker.com/docker-windows
https://desktop.docker.com/mac/stable/Docker.dmg
https://hub.docker.com/search?q=&type=edition&offering=community&operating_system=linux
#>

# Public Docker Hub
Start-Process https://hub.docker.com

# Run a container
# This example will run the latest version of the Azure PowerShell image
docker run --rm -it mcr.microsoft.com/azure-cli:latest
# -it run interactive
# --rm automatically remove container


docker pull mcr.microsoft.com/powershell

<#
Alpine Linux is a Linux distribution built around musl libc and BusyBox. The image is only 5 MB in size and has access
to a package repository that is much more complete than other BusyBox based images.
This makes Alpine Linux a great image base for utilities and even production applications.
#>

docker pull mcr.microsoft.com/powershell:preview-7.3-alpine-3.15

docker image list

docker run --rm -it mcr.microsoft.com/powershell:preview-7.3-alpine-3.15

<#
CBL-Mariner is an internal Linux distribution for Microsoft’s cloud infrastructure and edge products
and services. CBL-Mariner is designed to provide a consistent platform for these devices and
services and will enhance Microsoft’s ability to stay current on Linux updates.
https://github.com/microsoft/CBL-Mariner
#>

docker run --rm -it mcr.microsoft.com/powershell:lts-7.2-mariner-2.0

Start-Process https://hub.docker.com/_/microsoft-azure-powershell

# About 100-200 MB of the size is the PowerShell binaries (varies between versions)
docker image ls | Select-String powershell

# Inspect history for each layer
docker history mcr.microsoft.com/powershell:preview-7.3-alpine-3.15
docker history mcr.microsoft.com/powershell:lts-7.2-mariner-2.0

<# Key points:
- Containers in general are small and light weight
- Provides isolation
#>

# What about data? Containers are ephemeral and don't have a persistent storage

# Map local folder to container
docker run --rm -it -v "c:\git:/home/jan/git" mcr.microsoft.com/powershell

<#
For use in applications, for example on Azure Kubernetes Service, CSI drivers makes it
possible to mount Azure Disks (Managed Disks) and Azure Files (SMB shares) to containers
https://docs.microsoft.com/en-us/azure/aks/csi-storage-drivers
#>

#region Remote containers in VS Code

# Install extension and read documentation
Start-Process https://code.visualstudio.com/docs/remote/containers

# Windows
mkdir C:\temp\demo
code C:\temp\demo

# Linux/MacOS
mkdir /tmp/demo
code /tmp/demo

# Command pallette->Remote Containers->Add Development Container Configuration Files

# Clean-up
Remove-Item C:\temp\somerepo -Recurse -Force
Remove-Item /tmp/demo -Force -Recurse

#region Azure Cloud Shell image

Start-Process https://github.com/Azure/CloudShell

docker pull mcr.microsoft.com/azure-cloudshell:latest

docker run -it mcr.microsoft.com/azure-cloudshell pwsh

# Add to dev-container Dockerfile: FROM mcr.microsoft.com/azure-cloudshell:latest

# After rebuilding and restarting the container

# Ansible, git, chef and many others pre-installed
Get-Command -CommandType Application
Get-Command ansible, az, bicep, chef, kubectl, pwsh, terraform

apt list --installed


# Trade-off: Container image size of Azure CLoud Shell is rather huge: 9.3 GB
Start-Process https://github.com/Azure/CloudShell/issues/90

# For example, azure-functions-core-tools and chef-workstation alone are over 1.2 GB.


#endregion
101 changes: 101 additions & 0 deletions JanEgilRing/VSCodeDevContainers/02 - customizing the environment.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#region Dotfiles

<#
Dotfiles are used to customize your system. The "dotfiles" name is derived
from the configuration files in Unix-like systems that start with a dot
(e.g. .bash_profile and .gitconfig). For normal users, this indicates these
are not regular documents, and by default are hidden in directory listings.
For power users, however, they are a core tool belt.
#>

Start-Process 'https://dotfiles.github.io'

# Many applications store their configuration files in a ".config" file.
Get-ChildItem ~ -Force -Filter .* | Format-Table name

# PowerShell does too, for example for the CurrentUser-profiles
$profile | Format-List -Force

# And for the current user`s modules
$env:PSModulePath -split ':'

Start-Process 'https://code.visualstudio.com/docs/remote/containers#_personalizing-with-dotfile-repositories'

# Gotcha - executable bit must be set on scripts to be run
chmod 444 ./install.sh

<# Errors might happen during bootstrapping:
2022-06-18 20:08:07.984Z: New-Item: /workspaces/.codespaces/.persistedshare/dotfiles/install.ps1:5
2022-06-18 20:08:07.999Z: Line |
2022-06-18 20:08:08.013Z: 5 | New-Item -Path (Join-Path '~/.config' $_.Name) -ItemType Symbolic …
2022-06-18 20:08:08.028Z: | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2022-06-18 20:08:08.042Z: | No such file or directory
#>

# Hence, it is useful to log actions and errors to a log file
~\dotfiles.log

# Example dotfiles
Start-Process https://github.com/janegilring/dotfiles

# Tip: Look into using Stow for managing dotfiles
# https://www.youtube.com/watch?v=FHuwzbpTTo0

#endregion

#region Contoso management

code "~/git/2022psconfeu/JanEgilRing/VSCodeDevContainers/contoso-management"

#endregion

#region Git credentials & SSH keys

Start-Process 'https://code.visualstudio.com/docs/remote/containers#_sharing-git-credentials-with-your-container'

<#
If you do not have your user name or email address set up locally, you
may be prompted to do so. You can do this on your local machine by
running the following commands:
git config --global user.name "Your Name"
git config --global user.email "your.email@address"
The extension will automatically copy your local .gitconfig file into
the container on startup so you should not need to do this in the container itself.
#>


<#
You can add your local SSH keys to the agent if it is running by using the ssh-add command. For example, run this from a terminal or PowerShell:
ssh-add $HOME/.ssh/github_rsa
#>

# Windows
Set-Service ssh-agent -StartupType Automatic
Start-Service ssh-agent
Get-Service ssh-agent

# Linux

<#
Then add these lines to your ~/.bash_profile or ~/.zprofile (for Zsh) so it starts on login:
if [ -z "$SSH_AUTH_SOCK" ]; then
# Check for a currently running instance of the agent
RUNNING_AGENT="`ps -ax | grep 'ssh-agent -s' | grep -v grep | wc -l | tr -d '[:space:]'`"
if [ "$RUNNING_AGENT" = "0" ]; then
# Launch a new instance of the agent
ssh-agent -s &> $HOME/.ssh/ssh-agent
fi
eval `cat $HOME/.ssh/ssh-agent`
fi
#>


#endregion

30 changes: 30 additions & 0 deletions JanEgilRing/VSCodeDevContainers/03 - github-codespaces.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<#
Codespaces is an online development environment,
hosted by GitHub and powered by Visual Studio Code.
Makes it possible to develop and test your code in the cloud,
from low-end hardware and you can connect from anywhere -
via web or VS Code.
#>

Start-Process https://docs.github.com/en/github/developing-online-with-codespaces/about-codespaces

Start-Process https://docs.github.com/en/github/developing-online-with-codespaces/personalizing-codespaces-for-your-account

# VS Code extension
Start-Process https://code.visualstudio.com/docs/remote/codespaces

# Example container definitions
Start-Process https://github.com/Microsoft/vscode-dev-containers

Start-Process https://github.com/codespaces

<# If you have configured a Dotfiles repository, it will be used for Codespaces as well
Use Cmd/Ctrl + Shift + P -> View Creation Log to see full logs
✔ Finishing up...
⠏ Installing Dotfiles
#>

Start-Process "https://docs.github.com/en/codespaces/troubleshooting/troubleshooting-dotfiles-for-codespaces"

# Demo: Connect to Codespace both from web and VS Code
18 changes: 18 additions & 0 deletions JanEgilRing/VSCodeDevContainers/04 - misc.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#region Windows Subsystem for Linux (WSL)

# The Visual Studio Code Remote - WSL extension lets you use the Windows Subsystem for Linux (WSL) as your full-time development environment right from VS Code.
Start-Process https://code.visualstudio.com/docs/remote/wsl

# Tip: Create multiple users in WSL to separate environments

#endregion

#region Remote SSH

# Example usage (mainly for IT Pros): Remote into jumpbox/management server in locked down environments rather than working locally from the jumpbox

Start-Process https://code.visualstudio.com/docs/remote/ssh

# Demo (if time allows): Remote SSH against a Windows Server 2022 management server with OpenSSH Server installed and running

#endregion
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
FROM mcr.microsoft.com/dotnet/runtime:6.0

RUN apt-get update && apt-get -y upgrade

RUN apt-get update && apt-get install -y \
wget \
unzip \
ca-certificates \
apt-transport-https \
lsb-release \
gnupg \
curl \
libgdiplus \
libc6-dev \
&& rm -rf /var/lib/apt/lists/*

# Prerequisite for .NET Interactive Notebooks: https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.dotnet-interactive-vscode
# https://docs.microsoft.com/en-gb/dotnet/core/install/linux-ubuntu#2004-

# Download the powershell '.tar.gz' archive
RUN curl -L -o /tmp/powershell.tar.gz https://github.com/PowerShell/PowerShell/releases/download/v7.2.4/powershell-7.2.4-linux-x64.tar.gz

# Create the target folder where powershell will be placed
RUN mkdir -p /opt/microsoft/powershell/7

# Expand powershell to the target folder
RUN tar zxf /tmp/powershell.tar.gz -C /opt/microsoft/powershell/7

# Set execute permissions
RUN chmod +x /opt/microsoft/powershell/7/pwsh

# Create the symbolic link that points to pwsh
RUN ln -s /opt/microsoft/powershell/7/pwsh /usr/bin/pwsh

RUN useradd --create-home vscode

RUN wget https://github.com/JanDeDobbeleer/oh-my-posh/releases/download/v8.5.2/posh-linux-amd64 -O /usr/local/bin/oh-my-posh
RUN chmod +x /usr/local/bin/oh-my-posh

RUN apt-get update;apt-get install openssh-client -y

# ImportExcel prerequsites
RUN apt-get install -y --no-install-recommends libgdiplus libc6-dev

# Switch to non-root user:
WORKDIR /home/vscode
USER vscode

# Not needed when using dotfiles
# SHELL ["pwsh","-NoProfile", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
# RUN mkdir /home/vscode/.config;mkdir /home/vscode/.config/powershell
# RUN Set-Content -Path /home/vscode/.config/powershell/Microsoft.VSCode_profile.ps1 -Value ". /workspaces/contoso/.devcontainer/powershell/profile.ps1"
# RUN Install-Module -Name PSReadLine -Force -AllowPreRelease
# RUN Install-Module PSDepend -Force
# RUN Invoke-PSDepend /workspace/powershell/requirements.psd1 -Force
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "contoso-management",
"build": {
"dockerfile": "Dockerfile",
},

// Set *default* container specific settings.json values on container create.
// 2022-06: This is deprecated, the new recommended way to configure your default shell is by creating a terminal profile in `#terminal.integrated.profiles.linux
"settings": {
"terminal.integrated.shell.linux": "/opt/microsoft/powershell/7/pwsh"
},

// Add the IDs of extensions you want installed when the container is created.
// Note that some extensions may not work in Alpine Linux. See https://aka.ms/vscode-remote/linux.
"extensions": ["ms-vscode.powershell","ms-vscode.azure-account","ms-dotnettools.dotnet-interactive-vscode","hashicorp.terraform","takumii.markdowntable","ms-kubernetes-tools.vscode-kubernetes-tools","ms-kubernetes-tools.vscode-aks-tools","mutantdino.resourcemonitor"],

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
//"postCreateCommand": "uname -a",

"mounts": ["source=contoso-data,target=/contoso-data,type=volume"],

// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode",

"features": {
"azure-cli": "latest",
"github-cli": "latest",
"kubectl": "latest",
"terraform": "latest"
}

}

0 comments on commit c1140c7

Please sign in to comment.