Running PowerShell Inside Docker Container

Here, we will learn that instead of running the PowerShell command on a local machine. We will run PowerShell into a docker container and will run the PowerShell command inside it.

  • We can pull the PowerShell image and run it on the local machine.
  • Inside the host machine, we can have multiple containers that can run at the same time.
  • Like in the below case, we have a host machine and have 2 containers, MY SQL and Poweshell.

In this document, we will try to focus on the creation of a PowerShell container as per the screen below and will run a command inside it.

Step 1. Pull the Power shell image into local, Go to Docker Hub, and go to the below link to download.

Step 2. docker pull mcr.microsoft.com/powershell.

Docker Pull

Step 3. Docker images -> to verify images get downloaded to the machine.

Docker images

We can see images get downloaded to the machine now.

Step 4. Running images and starting the container.

docker run -it 085eb0dca1a5

Here –it is in interactive mode, and 085eb0dca1a5 is the image ID that we found above on the screen.

Image ID

Now Container for Powershell must start.

Step 5. Verify running containers.

Docker ps

Docker ps

We can see the container ID, which is for PowerShell, is running.

Step 6. Enter into PowerShell.

docker exec -it <containerID> <command>

Here command value is push-preview

Docker Exec

Now, we are inside PowerShell containers.

Let's run a few commands of Poweshell.

Power Shell command inside containers.

  1. Check directory structure
    LsĀ 
  2. Using the LS command, we can see folders inside the container.
    LS command
  3. Go to a specific folder.
    Specific folder
  4. Create a folder inside the container.
    Create a folder
  5. Read Write operation.
  6. Create a text file, set content into it and read.
  7. Here we are using simple PowerShell commands to read and write files.
    PowerShell commands
  8. Copy files from the local machine to the docker container
  9. We have the below files and we want to copy them to the container.

Command Syntax

docker cp src/. container_id:/target

Command

docker cp C:\Users\deves\OneDrive\Desktop\DockerSamples\Devesh_sampleFiles/. da1e0b50ef34:/home

Da1e0b50ef34 is the container ID.

Container ID

Docker

Verify files

Verify files

Copy files from the container to the local machine.

docker cp <containerID>:source destination
docker cp da1e0b50ef34:/home/test.txt C:\Users\deves\fromdocker\

Local machine