Vasya Petrov
Vasya Petrov

Reputation: 145

Any commands hang inside docker container

Any commands hang terminal inside docker container.

I login in container with docker exec -t php-zts /bin/bash And then print any elementary command (date, ls, cd /, etc.)

Command hang

When I press ctrl+c I going back to host machine. But, if I run any command without container - it's work normally

docker exec -t php-zts date
Wed Jan 26 00:04:38 UTC 2022

tty is enabled in docker-compose.yml docker system prune and all cleanups can not help me.

I can't identify the problem and smashed my brain. Please help :(

Upvotes: 0

Views: 2296

Answers (2)

Affes Salem
Affes Salem

Reputation: 1651

You can try to run your container using -i for interactive and -t for tty which will allow you to navigate and execute commands inside the container

docker run -it --rm  alpine

In the other hand you can run the container with docker run then execute commands inside that container like so:

  • tail -f /dev/null will keep your container running.
  • -d will run the command in the background.
docker run --rm -d --name container1 alpine tail -f /dev/null 
or
docker run --rm -itd --name container1 alpine sh # You can use -id or -td or -itd
  • This will allow you to run commands from inside the container.
  • you can choose sh, bash, or any other shell you prefer.
docker exec -it container1 alpine sh

Upvotes: 3

jkr
jkr

Reputation: 19250

The solution is to use the flag -i/--interactive with docker run. Here is a relevant section of the documentation:

--interactive , -i Keep STDIN open even if not attached

Upvotes: 5

Related Questions