Run PowerShell script when container starts - powershell

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

Related

Docker Container for Azure with Poweshell ExchangeOnlineManagement module

I am very new to containers and trying out Docker for the first time
I want to create a Docker Image which has PowerShell 5.1 installed along with PowershellGet and ExchangeOnlineManagement modules.
I am really lost as how to use a base image and update it with the modules needed above.
I would suggest to first pull and run the image. https://hub.docker.com/_/microsoft-powershell?tab=description
Start container interactively and see if it fulfills your needs:
docker run -it mcr.microsoft.com/powershell
If you want to create a customized Docker Image you need to create a Dockerfile.
And you can use it as base image and built you image on top of https://hub.docker.com/_/microsoft-powershell?tab=description:
# Indicates image that will be used as the base image.
FROM mcr.microsoft.com/powershell
...
# add your docker instructions e.g:
RUN powershell New-Item c:\test
...
Afterwards in the same directory in which you have your Dockerfile: docker build -t <username>/<image_name>:<tag_name> .

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

Windows - Docker CMD does not execute

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"]

Run a perl script at startup in Ubuntu

I have a perl script that I need to run once at startup with an argument under my user account.
So when I boot the system up it needs to execute a command like this,
./path/to/script.pl start
Any ideas?
You could use a line in your crontab (crontab -e)
To run a command at startup:
edit /etc/crontab
Add the following line:
#reboot root perl ./path/to/script.pl start
^^^ Runs as root. Change "root" to "BlackCow" to run as BlackCow
Or, you could use upstart (add a .conf file to /etc/init/). Here's a copy and paste from my notes:
Use upstart to run a daemon at reboot/start
e.g. /etc/init/prestocab.conf:
#!upstart
description "node.js server"
author "BlackCow"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
script
export HOME="/root"
exec sudo -u root /usr/local/bin/node /home/prestocab/prestocab.com/www/socket.io/server.js 2>&1 >> /var/log/prestocab.log
end script
To use:
start prestocab
stop prestocab
restart prestocab
#
You might want to use some sort of process monitor to restart the daemon if it crashes
Depends on what init you are using, if your version of Ubuntu is using upstart
you have to configure the appropriate Upstart start scripts, if not
the rc scripts based on your runlevel. Check update-rc.d.
On Ubuntu, the simplest way is to add this line to your /etc/rc.local file (before the exit 0 line, substituting username with your own user name):
su -c "./path/to/script.pl start" username &