Cannot run ruby commands while running Docker - ruby

I have an image I have built with Ruby in it. I am able to run Ruby commands, irb and install gems in the image while running by:
docker run -it jikkujose/apple
I can also do this to list the files in the container:
docker run -it jikkujose/apple ls
But when I try to run Ruby commands, it fails:
docker run -it jikkujose/apple ruby -e "puts 'Hello'"
Error:
Error response from daemon: Cannot start container c888aa8d2c7510a672608744a69f00c5feda4509742d54ea2896b7ebce76c16d: [8] System error: exec: "ruby": executable file not found in $PATH

That is probably because the ruby executable is not in the path of the user running the container process (i.e. root or the user specified with the USER command in the Dockerfile). The following two options might help you with your problem.
Specify the full path to the ruby binary when running the container. docker run -it jikkujose/apple /usr/bin/ruby -e "puts 'Hello'"
Add /usr/bin to the path in the Dockerfile ENV PATH /usr/bin:$PATH I'm not 100% sure this works, but the ENV operator in the Dockerfile should add this environment variable to the container. Source docker.com.
Alternatively you can specify /usr/bin/ruby as the ENTRYPOINT in your Dockerfile. That is: ENTRYPOINT ["/usr/bin/ruby"]. Then you can run docker run -it jikkujose/apple -e "puts 'Hello'" Note that this causes the container to run /usr/bin/ruby as default, and that you need to override this entrypoint if you want to run ls or other commands.
Edit:
Minimal viable Dockerfile solution is given below. Let us assume that /usr/bin is not already in the $PATH environment variable, which it is in the Ubuntu image.
FROM ubuntu:latest
RUN apt-get install ruby -y
ENV PATH /usr/bin:$PATH
CMD ["bash"]
Running docker run --rm -it pathtest ruby -e "puts 'Hello'" now outputs Hello in the terminal.
Edit 2:
Ah, you built the image with Docker commit. You can send in environment variables when running the docker run command. To do this simply run docker like so:
docker run --rm -e "PATH=/usr/bin" -it pathtest ruby -e "puts 'Hello'"
The -e option for docker run lets you specify or override an environment variable inside the container. Note that you will have to provide all paths you want $PATH to equal with this method.
You may also want to simply edit the PATH variable inside the container and then recommit the container so that /usr/bin is present in the $PATH environment variable stored in the container.

Its possible PATH is not set correctly, therefore try
docker run -it jikkujose/apple /usr/bin/ruby -e "puts 'Hello'"
or
docker run -it jikkujose/apple /bin/sh -c "/usr/bin/ruby -e "puts
'Hello'"

Related

Command Not Found with Dockerfile CMD

I have a Dockerfile that uses
CMD ['/usr/local/bin/gunicorn', '-b 0.0.0.0:8000', 'myapp.wsgi']
But when I run the container using docker run --rm myimage:latest I get an error:
/bin/sh: 1: [/usr/local/bin/gunicorn,: not found
Yet, when I run docker run --rm -it myimage:latest /bin/bash to go into the container, I can see that gunicorn runs, and running which gunicorn returns the correct path for gunicorn. Why is it failing to run?
Similarly, I planned on adding
ENTRYPOINT ['/entrypoint.sh']
to my Dockerfile, but when I run that, I get the error
/bin/sh: 1: /bin/sh: [/entrypoint.sh]: not found
The entrypoint.sh file contains:
#! /bin/bash
echo 'Starting app...'
cd /app || exit;
python manage.py migrate;
So why does it keep saying command not found when all the commands are there?
The issue here is the quotes. Use double " quotes.
From Docker Documentation:
The exec form is parsed as a JSON array, which means that you must use
double-quotes (“) around words not single-quotes (‘).
This is applicable for other instructions such as RUN, LABEL, ENV, ENTRYPOINT and VOLUME.

Docker run fail with ruby

I have a little problem, when i run my container this:
docker run -it emails_request cucumber -t #teste_inserindo_email
It's ok.
But, when i run this:
docker run it emails_request
Where my #teste_inserindo_emails, is on my dockerfile
WORKDIR /app
COPY Gemfile .
RUN bundle install && bundle clean
COPY . /app
EXPOSE 80
RUN cucumber -t #teste_inserindo_email
#CMD ["cucumber", "-t", " #teste_inserindo_email"]
Not found, return:
$ docker run -t emails_request
irb(main):001:0>
Or:
$ docker run emails_request
Switch to inspect mode.
what's your question exactly? you can just run it manually by firing up a container with an interactive terminal from your image, and then run the commands you want, or have a script in the image (or mounted as a volume) and then pass the script as the entry command instead.
docker run -it IMAGE_ID bash (for running manual commands)
if you want to use a script instead, put an ENTRYPOINT script in your Dockerfile instead

docker error on windows : the input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty' [duplicate]

This question already has answers here:
Error "The input device is not a TTY"
(16 answers)
Closed 2 years ago.
After I run this
docker run --rm -v "/c/users/vipul rao/documents/github/wappalyzer:/opt/wappalyzer" -it wappalyzer/dev
I am getting the following error
the input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty'
What should I use here? I am running Docker on Windows 8 in MINGW64.
As suggested by the error message you obtain, you should try to use winpty (which is installed by default with Git-Bash) and thus run:
winpty docker run --rm -v "/c/users/vipul rao/documents/github/wappalyzer:/opt/wappalyzer" -it wappalyzer/dev
If this works, you may want to set a Bash alias to avoid manually prepending winpty all the time:
echo "alias docker='winpty docker'" >> ~/.bashrc
or
echo "alias docker='winpty docker'" >> ~/.bash_profile
If you are using Git Bash you can try like this
winpty docker run -it ubuntu
This problem occurs when running with -it option using bash terminal on windows. You can use Powershell to resolve this issue.
This works for me.
I am using git bash on windows
winpty docker-compose exec app ls -l
Remove -it from the command. If you want to keep it interactive then keep -i
Don't use alias docker="winpty docker". It solves your problem but break pipes.
$ winpty docker run -ti ubuntu
root#e85cff7d1670:/# exit
$ wintpy docker run ubuntu bash HELLO
HELLO
$ wintpy docker run ubuntu bash HELLO | cat
stdout is not a tty
Copy this to your .bashrc. This script uses winpty docker only if -ti is used.
function docker(){
for param; do if [[ "$param" == "-ti" ]] || [[ "$param" == "-it" ]]; then
winpty docker "$#"; return
fi; done;
command docker "$#"
}
docker run -ti ubuntu becomes winpty docker run -ti ubuntu avoids error: the input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty'"
docker run ubuntu echo "what's up" | cat becomes command docker run echo "what'up" | cat avoids error: stdout is not a tty
The script only looks if there is a '-it' parameter without checking if it is inside a 'docker run' sentence... but it does the trick for my uses.
Did you start "Docker Quickstart Terminal"? I was trying to run
$ docker run -i -t redcricket/react-tutorial:latest /bin/bash
on windows from a Cygwin bash shell and got the same error:
the input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty'
Then I remembered that when I installed docker on my windows 10 system something called "Docker Quickstart Terminal" got installed. You need to start that first from that dumb windows 'Type here to search' thing on the task bar:
That launches this …
… you can run your docker commands there without getting that error or running winpty.
The problem is with gitbash however with the powershell it is working fine ..
Happened to me. From Git Bash, on Windows 8 running Docker Toolbox. There are two things happening. From git bash, we do not seem to have complete escalated privilege to the docker daemon (even though i'm running git bash with administrative privileges).
Thus:
Try running the command from your docker terminal. (gives you privilege).
To compensate for errors from Window's folder naming formats, don't forget to quote the path.. (to escape spaces and/or capitalization errors) say
From:
docker run -v $(pwd):/data image_ref
To:
docker run -v "$(pwd):/data" image_ref
(notice the enclosing quotes in the latter around $(pwd):/data).
Just add 'winpty' in start of your cmd ,Try below:
$ winpty docker.exe run --rm -v "/c/users/vipul rao/documents/github/wappalyzer:/opt/wappalyzer" -it wappalyzer/dev
Why this happens? More details here:
http://willi.am/blog/2016/08/08/docker-for-windows-interactive-sessions-in-mintty-git-bash/
Got this error for running docker-compose exec workspace bash
So just prefix with winpty winpty docker-compose exec workspace bash
It may be that you're not running your commands within the Docker terminal. If you don't, you may not be properly connected to the Docker daemon and won't be able to interact correctly.
Make sure you're running commands in the actual Docker Terminal.
For those using WSL and running Docker for windows inside of cmder or conemu I would recommend to not to use Docker which is installed on windows in 'Program Files' but instead install Docker inside WSL on ubuntu/linux. Do remember though that you can't run Docker itself from within WSL, you must connect to Docker running on windows from the linux Docker client installed in WSL.
To install Docker on WSL
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install docker-ce
Your options for running actual Docker commands are to either:
Connect to Docker using the switch -H
docker -H localhost:2375 run -it -v /mnt/c/code:/var/app -w "/var/app" centos:7
Or set the environment variable docker_host
export DOCKER_HOST=tcp://localhost:2375
Either way you are now be able to interactively connect to a running Docker container
you can try with Cmder tool it will work. Its not working with Gitbash
In addition to above mentioned solutions.
In case you are getting this error for docker attach
example: docker attach alpine1
error: the input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty'
Solution: Adding winpty before docker command i.e. winpty docker attach should work.
example: winpty docker attach alpine1
Note: I was getting this error while using base on windows and this solution worked for me.
I had the same error when trying to run the docker-compose exec command.
In the help documentation docker-compose exec --help it shows how you can disable the pseudo-tty allocation by adding -T to your command options in the following way:
docker-compose exec -T
From the help documentation:
-T Disable pseudo-tty allocation. By default docker-compose exec allocates a TTY.
If you are using gitbash the problem is when setting the terminal emulator for for using with Git bash.setting the emurator
Instead you can change the emulator to the first options or use the
winpty command before your docker run command

Running shell script using Docker image

Input:
- There is Windows machine with Docker Toolbox installed.
- There is a shell script file baz.sh which calls py2dsc-deb.
Problem: py2dsc-deb is not available on Windows.
As I understand correctly, I can pull some Linux distro image from Docker repository, create a container and then execute shell-script file and it will run py2dsc-deb and do its job.
I have pulled:
debian - stretch-slim - 3ad21 - 3 weeks ago - 55.3MB
Now
How do I run my script using debian, something like: docker exec mycontainer /path/to/test.sh?
Running docker --rm debian:stretch-slim does nothing. Doesn't it suppose to run Debian distro at docker-machine ip?
I have tried to keep the container up using docker run -it debian:stretch-slim /bin/bash, then run the script using docker exec 1ef5b ./build.sh, but getting
$ docker exec 745 ./build.sh
rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:247: starting container process caused "exec: \"./build.sh\": stat ./build.sh: no such file or directory"
Does it mean I can't run external script and has to always pass it inside the Docker?
You can execute bash command inside your container by typing
docker exec -ti -u `username` `container_name` bash -c "cd /path/to/ && ./test.sh"
lets say your container name is test_buildbox, you are root and your script stays inside /bin/test.sh You can call this script by typing
docker exec -ti -u root test_buildbox bash -c "cd /bin/ && ./test.sh
Please check if you have correct line endings in your .sh scripts (<LF>) when you built Docker image on Windows.

Use container environment variable in docker run without using bash -c

I have a WP-CLI container where I have to run the following command:
wp --allow-root core config --dbname=$MYSQL_DATABASE --dbuser=$MYSQL_USER --dbpass=$MYSQL_PASSWORD --dbhost=$WP_CLI_MYSQL_HOST --debug
When I run in bash inside container, I have no problem, but when I try to do:
docker-compose run --rm wordpress-cli --rm core config --dbname=$MYSQL_DATABASE --dbuser=$MYSQL_USER --dbpass=$MYSQL_PASSWORD --dbhost=$WP_CLI_MYSQL_HOST --allow-root --debug
All environment variables are evaluated in the host instead of the container, so they are passed empty to container.
I found in this question, that using bash -c 'my command' will do the trick, but my ENTRYPOINT is the WP command, so I want to just run without using the bash command.
Just escape the $ so they get passed through to the container:
docker-compose run --rm wordpress-cli --rm core config --dbname=\$MYSQL_DATABASE --dbuser=\$MYSQL_USER ...

Resources