One of the questions that I was asked at SQL Saturday Iceland was “how can I view the filesystem within a container?”.
This is a great question as one of the things that people find off-putting about containers is their opaqueness. It’s not obvious where everything lives within the container or how we can view the files within it.
Thankfully there’s a simple docker command that allows us to open a powershell session within a container, that command is docker exec.
Let’s run through an example. Firstly I’m going to spin up a new container running an instance of SQL Server: –
docker run -d -p 15777:1433 --env ACCEPT_EULA=Y --env sa_password=Testing11@@ --name testcontainer microsoft/mssql-server-windows
Then verify that the container is running: –
Now, to open up the powershell session inside run: –
docker exec -it testcontainer powershell
N.B. – the -i flag keeps the powershell session open, otherwise it will immediately exit. The -t flag creates a pseudo terminal, you don’t have to include it tbh but the examples I’ve seen online always include it.
Ok, we know have a powershell session within the container and we can execute commands as normal, for example: –
This will allow us to explore what the filesystem is within the container and run powershell commands as needed. In the screenshot above it’s interesting to see the space available/used on the C: drive. Have a dig around within your containers and let me know if you find anything interesting 🙂
Thanks for reading!
4 thoughts on “Connecting a powershell session to a container”
I know this post is old, but can’t seem to find a more recent one. Is this method deprecated? Getting an OCI runtime exec failed error about PowerShell not found.
Powershell may have been removed from the newer SQL Windows container images. One way to check would be to jump in with: –
docker exec -it testcontainer cmd
and then try to run powershell.