docker on windows ignoring ctrl-c [duplicate] - windows

This question already has answers here:
Unable to stop my Docker container with Ctrl-C
(2 answers)
Closed 1 year ago.
If i run
docker run alpine sleep 120
on a linux docker host, it is possible to to press CTRL-C to stop the container.
The same does not work if i try the same on Docker Desktop for Windows. I tested WSL1, GitBash, CMD and powershell. The shell returns immediately, but if you do a docker ps you'll find the container still running...
I learned, that using
docker run --init -it alpine sleep 120
almost solves this problem for me. Unfortunately i need to redirect output, which conflicts with usage of -t (Surprisingly only on Windows. On Linux, docker is happy with that too, but this time really needs the --init, otherwise it would block ctrl-c)
docker run --init -it alpine sleep 120 > somefile.log
Even in GitBash, usage of winpty does not work.
EDIT: I just realized, that the last command also seems to work for CMD and PowerShell. Only WSL and GitBash are misbehaving...
How do i make the container stop using CTRL-C on windows in WSL and GitBash?

TL;DR
Historically you CAN'T.
I doubt this will change anymore, since, after all these years, this is probably no longer considered a bug but a feature :S. It's bound to be in many shell scripts/batch files that would break.
Perhaps run docker in the new Linux subsystem for Windows. Enable the checkbox in Docker for Windows if you use that:
https://docs.docker.com/docker-for-windows/wsl/
Just use an explicit docker container stop ... on Windows.
You can consider this an automatic '--detached' or '-d' as compared to the Linux way, which you see in many online examples/tutorials anyway.

Related

Ctrl-C In VSCode Terminal Does Not Interrupt Docker On Windows

I'm working on a Docker container that, under normal circumstances, does not exit, so has no graceful exit logic. I've installed Docker Desktop on my Windows 11 system so I can build and test the container locally without the overhead of pushing to Git and waiting for CI to build.
If I run docker run --rm -p 80:80 my_container_image within the VSCode terminal, it runs, as expected. When I press Ctrl-C to send a keyboard interrupt to the process afterward so I can rebuild the image, nothing happens. I have to use the kill terminal function. Why can't I interrupt docker run from VSCode?
I'm using Powershell 7.2.6. Docker is configured to use the WSL 2 backend.
What I tried:
Pressing Ctrl-C in the terminal
What I expected:
The program to be exited
Possibly ^C to be printed in terminal
What actually happened:
Nothing.
Add -it to make the session interactive: docker run -it --rm -p 80:80 my_container_image.
As stated here:
The -it instructs Docker to allocate a pseudo-TTY connected to the
container’s stdin; creating an interactive bash shell in the
container.

How come "docker ps" has a different behaviour in PowerShell versus inside VS Code?

If I open a PowerShell, and type docker ps, this is what happens:
PS C:\Users\myself\dev> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5372832c4500 demo_flask "python ./server.py" 8 minutes ago Up 8 minutes 0.0.0.0:1234->1234/tcp, :::1234->1234/tcp intel_chatelet
Whereas if I open a new terminal inside VS Code and do the same, this error happens:
PS C:\Users\myself\dev> docker ps
error during connect: This error may indicate that the docker daemon is not running.:
Get "http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.24/containers/json":
open //./pipe/docker_engine: The system cannot find the file specified.
This is weird since these two shells are PowerShells.
How can I succesfully run "docker ps" in a VS Code terminal?
PS: I read this thread and this one but they do not solve the problem I'm facing. One shell can run docker while another cannot.

Is there a way to reintroduce yourself to a docker container running bash?

Let's say I have one docker container that when run it finishes with bash. So after docker run I have a bash terminal and I can put commands.
If for some reason I lost contact to this terminal (it can happen) and then in another terminal, I do docker ps, I can see the container running. However I am not "inside" the bash of this docker
Right now what I do is to delete this and then run docker run again but is there a way I can rejoin the terminal of this running container?
You can "detach" from a container without closing the program by Ctrl-P + Ctrl-Q
docker run -it -d --entrypoint sh busybox
docker attach $container_id
/ # #inside container, use Ctrl-p Ctrl-q to detach

Running one script in different environment

I am just wondering is that possible to run one script (e.g. shell script, python script, etc.) in different environments?
For example, I want to run my script from Linux shell to docker container shell (which the container is created by the script)? In other words, keep the script executing the rest of commands on container (after into the container).
run.sh (#shell script)
sudo docker exec -it some_containers bash #this command will lead me to docker container environment
apt-get install curl # I want to also execute this command inside the docker container after I enter the docker container environment
# this is just one script
Your question is not very clear, but it sounds like this is a job requiring two scripts - the first script runs in your "Linux shell", and needs to cause the second script to be placed into the container (perhaps by way of the dockerfile), at which point you can have the first script use docker exec.
Please see the answers on this question for more information.

docker run ubuntu on mac and nothing happens

i'm on mac running:
docker is configured to use the default machine with IP 192.168.99.100
For help getting started, check out the docs at https://docs.docker.com
my-mac:mydir$ docker run ubuntu /bin/bash
my-mac:mydir$
am i doing something wrong? shouldn't I get into the ubuntu shell?
By running docker run ubuntu /bin/bash, docker create a randomly-named container from the image ubuntu and runs a bash without stdin, stdout nor stderr then bash exits (right after being started).
Try at least to set a tty and interactive mode (aka foreground mode):
docker ps -a
# if not exited, stop it first
docker stop <container_id>
# remove the container which cannot be used
docker rm <container_id>
# let's try again
docker run -it --rm --name=test ubuntu bash
As commented by physincubus:
'-it' is the bit that makes it interactive,
'--rm' removes the container when you exit (so if you want to be able to exit for detach and reattach later, do not do this), and
'--name' allows you to name the container more explicitly in case you want to run multiple instances of the same container
Run it with following command
docker run -it ubuntu /bin/bash
Then you will get bash prompt of ubuntu container

Resources