Blog Post

Connecting a powershell session to a container

,

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: –

docker ps

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: –

get-psdrive

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!

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating