docker exec command not executing in sh file - docker

When running below command in command line(terminal) this gets executed fine:
$sudo docker exec -it 5570dc09b58 bash
But same results with :
FATA[0000] cannot enable tty mode on non tty input
Error when running in a shell script file.

Scripts may be forced to run in interactive mode with the -i option or with a #!/bin/bash -i header.
So adding shebang to the script with -i option should work:
#!/bin/bash -i
docker exec -it ed3d9e46b8ee date
Run the script as usual:
chmod +x run.sh
sudo ./run.sh
Output:
Thu Apr 2 14:06:00 UTC 2015

You are not running docker in a terminal, so you should remove -t from -it:
sudo docker exec -i 5570dc09b58 bash
See a more detailed answer here.

There are few images which do not support the interactive shell/bash.
Example - Docker image mockserver/mockserver
Docker Set Up

Related

Change selected directory at startup of docker container

Is it possible to save the selected directory in docker before exiting the container?
As a default, docker does not remember the selected directory before it exits.
In the example below, I changed the directory inside docker to home.
Example:
> docker exec -it loving_mccarthy /bin/bash
root#6bd70522dd17:/# cd /home
root#6bd70522dd17:/home# exit
exit
> docker exec -it loving_mccarthy /bin/bash
/#
You can achieve this behavior by setting up a bash function, which you can configure to run on terminal exit, that writes the current working directory to the .bashrc file, which you can then use the second time you run a bash terminal in the container with cd -.
Here's a full example:
Start the container:
$ docker run -it --name=example -d ubuntu /bin/bash
Run docker exec the first time, and configure the function for trap:
$ docker exec -it example /bin/bash
root#ecfc612fe6b8:/# set_workdir_on_exit() { echo "export OLDPWD=$PWD" >> $HOME/.bashrc ;}
root#ecfc612fe6b8:/# trap 'set_workdir_on_exit' EXIT
root#ecfc612fe6b8:/# cd /home
root#ecfc612fe6b8:/home# exit
Run docker exec the second time:
$ docker exec -it example /bin/bash
root#6a491ad1aee5:/# cd -
/home
root#6a491ad1aee5:/home#

how to run command in docker using a script outside thecontainer

in my .mk file I have done as following
.PHONY : Test
Test:
#echo Starting Docker container
docker container start XXX;
docker exec -it -w /home/TA/G/T/P XXX bash
docker exec /home/CO/CO_ANAlysis/bin/c-build --dir results--encoding UTF-8 make
and then I want to execute that it execute the first two and when docker container starts the command "docker exec /home/CO/CO_ANAlysis/bin/c-build --dir results--encoding UTF-8 make" doesn't execute
and if I run this out of docker:
docker exec -it -w /home/TA/G/T/P SDIO bash /home/CO/CO_ANAlysis/bin/c-build --dir results--encoding UTF-8 make
it shows me :
/home/CO/CO_ANAlysis/bin/c-build: /home/CO/CO_ANAlysis/bin/c-build: cannot execute binary filehow can I, in a mk file, run commands inside a docker?
Thanks
Problem Solved
docker container start XXX;
docker exec -it -w /home/TA/G/T/P XXX /home/CO/CO_ANAlysis/bin/c-build --dir results--encoding UTF-8 make

How to run commands in Docker container

Hello I m trying to follow the step by step guid to build jpeg xl (I m on windows and try to build a x64 version for linux)
after:
docker run -u root:root -it --rm -v C:\Users\fred\source\tools\jpegxl\jpeg-xl-master -w /jpeg-xl gcr.io/jpegxl/jpegxl-builder
I have the container running but I don't know how to run the command inside :
CC=clang-6.0 CXX=clang++-6.0 ./ci.sh opt
I tried CC=clang-6.0 CXX=clang++-6.0 ./ci.sh opt and I get ./ci.sh: No such file or directory no command seems to work when I do "ls" it display nothing
Does someone knows how to get this to build?
Make sure that you start a bash terminal inside the container:
docker run -it <image> /bin/bash
I believe /bin/bash is missing from your docker run command. As a result, you are executing the command for clang inside your own environment, not the container.
You can set the environment variables by using -e
Example
-e CC=clang-6.0 -e CXX=clang++-6.0
The full command to log in into your container:
docker run -u root:root -it --rm -e CC=clang-6.0 -e CXX=clang++-6.0 -v C:\Users\fred\source\tools\jpegxl\jpeg-xl-master -w /jpeg-xl gcr.io/jpegxl/jpegxl-builder /bin/bash
They have updated the image without updating the command so the command is
CC=clang-7 CXX=clang++-7 ./ci.sh opt
The discution is here:
Can't build from docker image "Unknown clang version"

the input device is not a TTY - when running docker-compose exec command on Jenkins

system("docker-compose -p #{ENV['COMPOSE_PROJECT_NAME']} exec #{ENV['BROWSER']} chmod 777 /home/seluser/Downloads")
Running this command from ruby test on Jenkins slave causes error
the input device is not a TTY
Locally (on Catalina OS) there's no error.
Analogical docker command works successfully on Jenkins:
system("docker exec \$(docker ps --filter name=#{ENV['BROWSER']}_1 --format {{.ID}}) sudo chmod 777 /home/seluser/Downloads")
What's wrong with docker-compose?
Found solution here https://github.com/docker/compose/issues/5696
Need to add -T' option and rundocker-compose exec -T
-T Disable pseudo-tty allocation. By defaultdocker-compose exec` allocates a TTY.

Executing a script inside a docker container gives no errors but does not work either

I have a docker container with the basic ubuntu image. I use the following command to start it.
docker container run -it -d -v c:\Git\ENGINE_LIB_DIR:/ENGINE_LIB_DIR:ro --name ibuntu ubuntu
inside the mounted volume is a Java JDK and a script which looks like this:
#!/bin/bash
echo "export JAVA_HOME=/ENGINE_LIB_DIR/jdk/" >> ~/.bashrc;
echo "export PATH=${PATH}:/ENGINE_LIB_DIR/jdk/bin/" >> ~/.bashrc;
exec bash
So it basically adds the mounted java to the path to make it useable. This script works, as long as I am executing it from the ubuntu bash inside the container. If I try to use
docker exec -it ibuntu sh -c "sh /ENGINE_LIB_DIR/action.sh"
from outside the container it does not give any error message, whereas
docker exec -it ibuntu sh -c "java -version"
Returns "java: not found". So I suspect the script is not executed properly. I tried absolute paths, just without "sh -c" and basically any other method I found by googeling.
My goal is to easily use a java jdk provided inside a docker container to build a project. I am gladful for any help.
Edit:
I tried the /bin/bash -ic approach from #itachi. It still says java: not found, while the shell call gives back that error:
bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell
sh: 0: Can't open ./ENGINE_LIB_DIR/action.sh
Edit 2:
I managed to narrow the behaviour down to the docker exec command. I setup the container with docker container run -it -d -v c:\Git\ENGINE_LIB_DIR:/ENGINE_LIB_DIR:ro --entrypoint /ENGINE_LIB_DIR/action.sh --name ibuntu ubuntu /bin/bash. The java path variable is functioning when attached to the container, but when i execute docker exec ibuntu sh -c "java -version" it still says sh: 1: java: not found. I would be grateful for any idea.

Resources