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.
Step 3. Docker images -> to verify images get downloaded to the machine.
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.
Now Container for Powershell must start.
Step 5. Verify running containers.
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
Now, we are inside PowerShell containers.
Let's run a few commands of Poweshell.
Power Shell command inside containers.
- Check directory structure
LsĀ
- Using the LS command, we can see folders inside the container.
- Go to a specific folder.
- Create a folder inside the container.
- Read Write operation.
- Create a text file, set content into it and read.
- Here we are using simple PowerShell commands to read and write files.
- Copy files from the local machine to the docker container
- 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.
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\