Create the following Dockerfile (example for SQL Server 2019 image, most content from here):
FROM mcr.microsoft.com/mssql/server:2019-latest AS base
# This is required since the instance uses mssql user and the following command require root or sudo
# This is used to avoid installing sudo
USER root
# Download the Microsoft repository GPG keys
RUN wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb
# Register the Microsoft repository GPG keys
RUN dpkg -i packages-microsoft-prod.deb
# Update the list of products
RUN apt-get update
# Install PowerShell
RUN apt-get install -y powershell
# Revert back to default user used by SQL instance
USER mssql
To interactively work with the docker instance, explicitly use the root
user (source):
docker exec -u 0 -it mssqlcontainer pwsh
PowerShell 7.0.0
Copyright (c) Microsoft Corporation. All rights reserved.
https://aka.ms/powershell
Type 'help' to get help.
PS /> $PSVersionTable.PSVersion
Major Minor Patch PreReleaseLabel BuildLabel
----- ----- ----- --------------- ----------
7 0 0