Windows - Docker CMD does not execute - powershell

For the life of me, I cannot seem to get my provisioning script to execute when I run my container. Down the road, I will need to pass in arguments to the docker run command to replace 'hiiii' and '123' for multiple container deployments.
This is my docker file
FROM microsoft/aspnet:3.5-windowsservercore-10.0.14393.1198
SHELL [“powershell”, “-Command”, “$ErrorActionPreference = ‘Stop’; $ProgressPreference = ‘SilentlyContinue’;”]
COPY *.ps1 /Container/
COPY [“wwwroot”, “/inetpub/wwwroot”]
COPY [“Admin”, “/Program Files (x86)Admin”]
COPY [“Admin/web.config”, “/Program Files (x86)/Admin/web_default.config”]
#ENTRYPOINT [“powershell”]
CMD [“powershell.exe”, -NoProfile, -File, C:\Container\Start-Admin.Docker.Cmd.ps1 -Parm1 ‘Hiiii’ -parm2 ‘123’]
I have also tried the shell version of CMD as follows
CMD powershell -NoProfile -File C:\Container\Start-Admin.Docker.Cmd.ps1 -Parm1 ‘Hiiii’ -Parm2 ‘123’
This is the command I am using.
docker image build -t image:v1:v1 .
docker run --name container -p 8001:80 -d image:v1
After I create and run the container I see that the script did not run, or failed. However, I can exec into powershell on the container and run the script manually and it works fine and I see all the changes that I need.
docker exec --interactive --tty container powershell
C:\Container\Start-Admin.Docker.Cmd.ps1 -Parm1 ‘Hiiii’ -Parm2 ‘123’
I am just at a loss as to what I am missing regarding CMD.
Thanks!

I was able to get it working the way I had hoped. Though I am still working out some details in the provisioning script, this is how I ended up getting the result I wanted from the docker side of things.
FROM microsoft/aspnet:3.5-windowsservercore-10.0.14393.1198
#The shell line needs to be removed and any RUN commands need to be immediately proceeded by 'powershell' eg RUN powershell ping google.com
#SHELL [“powershell”, “-Command”, “$ErrorActionPreference = ‘Stop’; $ProgressPreference = ‘SilentlyContinue’;”]
COPY *.ps1 /Container/
COPY [“wwwroot”, “/inetpub/wwwroot”]
COPY [“Admin”, “/Program Files (x86)Admin”]
COPY [“Admin/web.config”, “/Program Files (x86)/Admin/web_default.config”]
ENV myParm1 Hiiii
ENV myParm2 123
ENTRYPOINT ["powershell", "-NoProfile", "-Command", "C:\\Container\\Start-Admin.Docker.Cmd.ps1"]
CMD ["-parm1 $Env:myParm1 -parm2 $Env:myParm2"]
The docker run command looks like this
docker run -d -p 8001:80 -e "myParm1=byeeeee" --name=container image:v1
I hope this helps someone else that is in my boat. Thanks for all the answers!

You can give this a try. ARG passes the variable to shell accessible via PS $env: variable.
ARG parmOne=Hiiii
ARG parmTwo=123
# Run a native PowerShell session and pass the script as a command.
RUN ["powershell", "-NoProfile", "-Command", "C:\\Container\\Start-Admin.Docker.Cmd.ps1 -Parm1 $env:parmOne -parm2 $env:parmTwo"]

Related

Windows terminal: open multiple panes and execute specified command

I recently downloaded the new Windows Terminal. I have created the shortcut for opening the multiple panes(which is working fine). However, I am trying to execute a command for the respective pane.
wt -d <path> -c "cls && php artisan serve" ;
split-pane -p "Command Prompt" -H -d <path> -c "npm run watch"
I googled for the solution but no luck.
Is this even possible..?
I got a similar setup working.
Im running Windows Terminal version 1.8.1444.0
My goal was to setup a dotnet core app running in left pane and a react-app running in right pane:
wt -d "C:\path\to\dotnetcoreapp" -p "Command Promt" cmd /k dotnet watch run ; split-pane -d "C:\path\to\reactapp" cmd /k yarn start
Also tried to start an interactive elixir session: wt -d "C:\dev\elixir" cmd /k IEx which also worked fine...
The short answer is: Yes it is possible but it is a workaround.
The Challenges
wt.exe does not currently have a command line option to execute a
command from a split-pane
wsl.exe (which runs your default shell such as bash) does not currently support opening a shell with a command without exiting the shell right after the command is run.
The workaround
To get around the first challenge we can launch a custom profile that executes the command via wsl.exe in the key value pair (in settings json) "commandline": "wsl.exe 'commands go here"
To get around the second challenge we need to execute the wsl.exe 'commands go here' via powershell.exe because Powershell has a -NoExit option which will keep the shell open after the command is executed. So for example if you wanted to open a shell that runs wsl.exe (your linux shell) with the command watch ps then the line in the custom profile would look like this:
"commandline": "powershell.exe -NoExit -Command wsl.exe watch ps"
The Solution:
Create a profile in Windows Terminal settings.json for each command you want to run. Each profile should have a unique guid that you can generate in Powershell by running the command [guid]::NewGuid(). So the profile to run the command watch ps would look something like this:
{
"guid": "{b7041a85-5613-43c0-be35-92d19002404f}",
"name": "watch_ps",
"commandline": "powershell.exe -NoExit -Command wsl.exe watch ps",
"hidden": false,
"colorScheme": "One Half Dark"
},
Now you can open a tab in windows terminal with two panes, the pane on the right will run the command watch ps and the shell will stay open. Put something like the below line of code in your shortcut (or from the command line) where the value of the option -p is equal to the value of the profile you created. Each additional pane you open will need a profile that has the command you want to run in it.
wt split-pane -p "watch_ps"

Run PowerShell script when container starts

How to run a simple PowerShell script after Docker container starts?
FROM ...
ENTRYPOINT ["powershell", "C:\scripts\remotetools.ps1"]
or
FROM ...
CMD ["powershell", "C:\scripts\remotetools.ps1"]
didn't work
Take the ENTRYPOINT/CMD out of the dockerfile and then build the image again, and run it. find the container ID with
docker container ls
Now run your command but passed through the exec function so you can see if it works and get some better debugs:
docker exec <HEX_CONTAINER_ID> powershell C:\scripts\remotetools.ps1
you may also require the --privileged flag if the script doesn't run, then you may be looking at a permissions issue

Certain Docker Commands not working in Windows Powershell ISE but working in other Command Line tools

I am exploring docker and I've been using Windows Powershell ISE to run docker commands. I have come across situation where I run certain commands in Powershell but they appear to be stuck, but when I run the same docker command on Command Prompt they work fine. I am using the same machine but different command line tools. Why do certain docker command work in powershell and some don't? The below are just some examples. While most work, some don't appear to be working.
Example 1) docker system prune
In powershell I expected a message like I do in the command prompt below, but it appears to be hanging.
Example 2) docker exec -ti containerName bash
In CMD Prompt, I am able to jump into my running container but not in Powershell
Docker does not support Powershell-ISE:
https://docs.docker.com/docker-for-windows/

How do I specify a PowerShell script as a Docker container entry point?

I need to run a [Windows] Docker container as an executable, running a rather involved PowerShell script (invoking Java and .NET applications) and exiting. Docker documentation suggests using ENTRYPOINT for this purpose. So I went ahead and created a Dockerfile with the following contents:
FROM microsoft/dotnet-framework
COPY run.ps1 /
ENTRYPOINT [ "powershell.exe", "C:\\run.ps1" ]
The contents of run.ps1 (uber-simplified for this question):
gci
write-host "looks like everything is good!"
Then, I ran the following commands:
# Build the Docker image
docker build --rm -t testdockerps .
# Create/run container using above image
docker run -it testdockerps
The container ran successfully, displaying the contents of C:\ followed by the message - looks like everything is good!.
I have a couple of questions based on what my observations:
What is the default shell for a Windows based Docker container? Is there any way to set it to PowerShell, so I don't have to specify "powershell" as the first element of the ENTRYPOINT JSON array? Should I be using the SHELL command in the Dockerfile?
Creating and running the container takes about 3-4 seconds which is somewhat understandable, but after the PS1 script completes, it takes nearly a questionable 10 seconds for the container to exit and return to the command prompt. What may be the cause of this delay?
Yes you can specify powershell as default shell like below on top of DOCKERFILE
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'Continue'; $verbosePreference='Continue';"]
I'm not sure you can do anything about the time it takes to spin down your VM

Import-Module in the Docker PowerShell image

I'm building a Docker image based from microsoft/powershell:ubuntu16.04. It builds properly without any error, however when I go to use it it doesn't have the imported module that I stated in the Dockerfile (which is a psd1 file from a GitHub repo cloned in the image.
I tried with a simple import of Microsoft's powershell management as well (seen below), and it also doesn't get imported. I ran the image interactively and the commands one by one and everything imported fine, so I don't know why it doesn't work through the Dockerfile build itself.
FROM microsoft/powershell:ubuntu16.04
WORKDIR /workdir
RUN pwsh -command "Import-Module Microsoft.PowerShell.Management"
CMD [ "pwsh", "-Command", "Get-Module" ]
Example psd1 file
FROM microsoft/powershell:ubuntu16.04
RUN apt-get update && apt-get install git -y
RUN git clone https://github.com/DTW-DanWard/PowerShell-Beautifier.git
RUN cp -a PowerShell-Beautifier/src/. /opt/microsoft/powershell/6.0.0-rc/Modules
ENTRYPOINT [ "pwsh", "-c" ]
CMD [ "Get-Help", "Edit-DTWBeautifyScript" ]
Because it happens in a wrong "context". For this to work the way you want it to work you need to use both these commands in 1 powershell session:
pwsh -command "Import-Module Microsoft.PowerShell.Management; Get-Module"
else it creates a layer where it ran that command, but when powershell stops all the imports are gone (and when the layer is done, it shuts down the container, so it doesnt preserve session state, only os state).
My dockerfile (working example):
FROM microsoft/powershell:ubuntu16.04
RUN pwsh -c "Install-Module AzureRM.netcore -Force"
CMD [ "pwsh" ]