Running interactive container with power shell - powershell

I'm trying to setup an automated build container in Windows(host and guest). Right now I'm having problems executing a simple powershell inside the container. I've done the following:
Created this DockerFile:
# escape=`
FROM microsoft/windowsservercore
SHELL ["cmd", "/S", "/C"]
CMD ["powershell.exe", "-NoLogo", "-ExecutionPolicy", "Bypass"]
Executed this build command:
docker build -t test:latest .
Started the docker with this command:
docker run test
The PowerShell prints this and the container exits:
PS C:\>
D:\repo\docker\Teste
Tried again with this command:
docker start d05ee -ai
The PowerShell prints the same output:
PS C:\>
D:\repo\docker\Teste
I wish to use the container interactively in a first moment to validate the tools I will install on it, but I'm not able to do that. I don't now which error is blocking me to do it and that is my question.
Obs1: The powershell in a windows cmd with the same parameters work fine.
Obs2: I've based my DockerFile on the one in this tutorial.
Obs3: Running this works fine:
docker run -it microsoft/windowsservercore powershell -NoLogo -ExecutionPolicy Bypass
Therefore I presume the problem is on the image generation.

you need to run your container with the -it switch. this will make you container interactive, so you can poke around
docker run -it test

Related

how to get the console output of docker-compose exec of a detached container

I've run in detached mode docker-compose up.
I have then run docker-compose exec container_name command
and i had nothing, so I have then run docker-compose exec -it container_name bash to get the shell and i got nothing either.
I have tried these command but I have no output from the first command and i dont have access to the shell with the second command. Do you know how i can have output or access the shell?
I am under macos x catalina 10.15.7. I have tried to reboot but it's the same.
docker-compose build and docker-compose up are running correctly
note docker ps -a gives me the same container id as docker-compose without the #1 at the end (react-wagtail-blog_web_cont with docker ps -a and react-wagtail-blog_web_cont_1 with docker-compose ps).
I can access my react-wagtail-blog_web_cont container with docker exec -it react-wagtail-blog_web_cont bash...
Thank you
If you just want to see the normal log output you can use docker logs <container> where container is the container name or hash. To find that, just run docker ps
Also if you want to see the logs realtime as they come use `docker logs -f
See docs

How to execute docker compose run command with ansible?

Is there a way to execute "docker compose run" command with ansible? Here is my command that I want to transform into ansible task:
docker compose --env-file configs/.env.dev -f docker-compose.yml -f docker-compose.dev.yml run --rm artisan migrate:refresh
I know how to use ansible-shell module for that. Also I've used ansible's docker-compose module to execute compose up command, but I haven't found any module parameters to specify the specific command for execution.

Running an exe file in windows container using docker

I want to run an exe file on window container using Docker. My exe accepts parameters. New file gets created in predefined directory
ie:- Test.exe -f1=file1.txt -f2=file2.txt
**Output** :
Writing file file1.txt
Successfully created file file1.txt
Writing file2 file2.txt
Successfully created file file2.txt
Enjoy!!
My docker file looks like below
FROM microsoft/aspnet:3.5-windowsservercore-10.0.14393.1715
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
Copy Test.exe ./TestFolder/
WORKDIR /TestFolder
ENTRYPOINT ["Test.exe"]
CMD ["f1=file1.txt","f2=file2.txt"]
I build the image and run the container
docker build -t image1 .
docker run -it image1
Once the container runs I get the exact above output but when I login to container machine using scripts, I don't see any file created in the predefined path.
Am I missing out anything ? Is this the correct way to run exe file on windows base image?
Any advice is appreciated. Thanks
Found out that the exe was running fine and file was created.
The way I logged in to container was wrong
Use
docker exec -it containername powershell
docker run -it --entrypoint powershell imagename --> Runs new instance of image not existing

How do avoid a docker container stop after the application is stopped

There is a docker container with Postgres server. Ones postgres is stopped or crashed (doesn't matter) I need to check some environment variables and the state of a few files.
By default, the container stops after an application is finished.
I know there is an option to change the default behavior in dockerfile but I no longer to find it ((
If somebody knows that please give me an Dockerfile example like this :
FROM something
RUN something ...
ENTRYPOINT [something]
You can simply run non exiting process in the end of entrypoint to keep the container alive, even if the main process exits.
For example use
tail -f 'some log file'
There isn't an "option" to keep a container running when the main process has stopped or died. You can run something different in the container while debugging the actual startup scripts. Sometimes you need to override an entrypoint to do this.
docker run -ti $IMAGE /bin/sh
docker run -ti --entrypoint=/bin/sh $IMAGE
If the main process will not stay running when you docker start the existing container then you won't be able to use that container interactively, otherwise you could:
docker start $CID
docker exec -ti $CID sh
For getting files from an existing container, you can docker cp anything you need from the stopped container.
docker cp $CID:/a/path /some/local/path
You can also docker export a tar archive of the complete container.
docker export $CID -o $CID.tar
tar -tvf $CID.tar | grep afile
The environment Docker injects can be seen with docker inspect, but this won't give you anything the process has added to the environment.
docker inspect $CID --format '{{ json .Config.Env }}'
In general, Docker requires a process to keep running in the foreground. Otherwise, it assumes that the application is stopped and the container is shut down. Below, I outline a few ways, that I'm aware of, which can prevent a container from stopping:
Use a process manager such as runit or systemd to run a process inside a container:
As an example, here you can find a Redhat article about running systemd within a docker container.
A few possible approaches for debugging purposes:
a) Add an artificial sleep or pause to the entrypoint:
For example, in bash, you can use this to create an infinite pause:
while true; do sleep 1; done
b) For a fast workaround, one can run the tail command in the container:
As an example, with the command below, we start a new container in detached/background mode (-d) and executing the tail -f /dev/null command inside the container. As a result, this will force the container to run forever.
docker run -d ubuntu:18.04 tail -f /dev/null
And if the main process crashed/exited, you may still look up the ENV variable or check out files with exec and the basic commands like cd, ls. A few relevant commands for that:
docker inspect -f \
'{{range $index, $value := .Config.Env}}{{$value}} {{end}}' name-of-container
docker exec -it name-of-container bash

RUN Powershell Script in Docker Container From Host Powershell Script

I have a powershell script in host which copy some files and starts the container.
#Copy File
docker cp "D:\addApplication.ps1" website:/inetpub/wwwroot/
#Start Container
docker start website
Write-Host 'Process has started'
#Execute Container
docker exec -ti website powershell
#Run Script
Invoke-Expression "C:\inetpub\wwwroot\addApplication.ps1"
Second last command executes fine but last command will only execute when I exit the container session and returns error(File Not Found which is because it finds that file on host)
Question: Is there anyway I can execute the command in container session from the script. Or execute any command from script in any process(confused)
Any help is appreciated.
Thanks
Do not use the -ti flags to start an interactive session, just execute the script directly via the docker exec command
docker exec website powershell -command "C:\inetpub\wwwroot\addApplication.ps1"