970

I am running the following command from my Jenkinsfile. However, I get the error "The input device is not a TTY".

docker run -v $PWD:/foobar -it cloudfoundry/cflinuxfs2 /foobar/script.sh

Is there a way to run the script from the Jenkinsfile without doing interactive mode?

I basically have a file called script.sh that I would like to run inside the Docker container.

2
  • 1
    For *nix, it looks like there is no solution here. 'docker exec -i' doesn't work, nor does '-t'.
    – rjurney
    Commented Jul 24, 2018 at 16:32
  • 2
    @rjurney Did you ever find a solution for docker exec ? I to have tried -i and -t with no success. docker exec -it mycontainer bash certbot --apache -d www.website.com --email *********@gmail.com --agree-tos -n
    – Hutch
    Commented Nov 1, 2019 at 8:41

18 Answers 18

1377
Answer recommended by CI/CD Collective

Remove the -it from your cli to make it non interactive and remove the TTY. If you don't need either, e.g. running your command inside of a Jenkins or cron script, you should do this.

Or you can change it to -i if you have input piped into the docker command that doesn't come from a TTY. If you have something like xyz | docker ... or docker ... <input in your command line, do this.

Or you can change it to -t if you want TTY support but don't have it available on the input device. Do this for apps that check for a TTY to enable color formatting of the output in your logs, or for when you later attach to the container with a proper terminal.

Or if you need an interactive terminal and aren't running in a terminal on Linux or MacOS, use a different command line interface. PowerShell is reported to include this support on Windows.


What is a TTY? It's a terminal interface that supports escape sequences, moving the cursor around, etc, that comes from the old days of dumb terminals attached to mainframes. Today it is provided by the Linux command terminals and ssh interfaces. See the wikipedia article for more details.

To see the difference of running a container with and without a TTY, run a container without one: docker run --rm -i ubuntu bash. From inside that container, install vim with apt-get update; apt-get install vim. Note the lack of a prompt. When running vim against a file, try to move the cursor around within the file.

6
  • 11
    I'm using this command in conjunction with mysql -p without specifiying a password. When just adding -i the password prompt never appears. With just adding -t the prompt appears but it seems to not read the input (which is printed literally instead of being hidden by the prompt) at all, not even when hitting return; only ctrl-c can end it. Is it somehow possible to use the mysql client with docker that way?
    – ohcibi
    Commented Jul 22, 2018 at 12:21
  • 25
    Thank you for this! For docker-compose users, I wanted to add that I had a similar command to run - I wanted to delete redis keys based on a pattern - and was able to do so with the docker-compose exec -T command. From the man page for docker-compose exec: Disable pseudo-tty allocation. By default docker-compose exec allocates a TTY.
    – Yacine B
    Commented Jul 21, 2020 at 21:13
  • Using PowerShell worked for me on Windows.
    – localhost
    Commented May 6, 2021 at 20:37
  • 1
    This completely fails to offer a solution to the original problem: how to SUCCEED rather than FAIL at running some script script.sh that runs OK when executed manually from a terminal but fails with the error given in the title when trying to execute it programmatically (not necessarily from a shell script). Commented Mar 18, 2022 at 10:54
  • @SzczepanHołyszewski there are at least 3 options provided here to do that, depending on the scenario. Perhaps you have a scenario not covered by any of these?
    – BMitch
    Commented Mar 18, 2022 at 12:18
406

For docker run DON'T USE -it flag

(as said BMitch)

And it's not exactly what you are asking, but would be also useful for others:

For docker-compose exec use -T flag!

The -T key would help people who are using docker-compose exec! (It disables pseudo-tty allocation)

For example:

docker-compose -f /srv/backend_bigdata/local.yml exec -T postgres backup

or

docker-compose exec -T mysql mysql -uuser_name -ppassword database_name < dir/to/db_backup.sql
6
  • 25
    Just what I needed too. According to the help: -T Disable pseudo-tty allocation. By default docker-compose exec allocates a TTY.
    – T.S.
    Commented Jan 26, 2020 at 16:26
  • 1
    with MySQL database running on mysql virtual machine (with the suggested -T above): docker-compose exec -T mysql mysql -uuser_name -ppassword database_name < dir/to/db_backup.sql Commented Jun 24, 2021 at 17:29
  • in my case, I'm trying to take the dump from a running container of my "production" env with authentication and passing with some arguments, then take the output of .bak file of my mssql database container. Remove -it from the command. If you want to keep it interactive then keep -i Commented Jul 18, 2022 at 8:08
  • I used -t instead of -T, I don't know what is the difference but -t worked Commented Mar 28 at 6:41
  • @ajaychawla It seems to me that you are using an undocumented option docs.docker.com/reference/cli/docker/compose/exec - what is your complete command? Commented Jul 2 at 15:23
168

For those who struggle with this error and git bash on Windows, just use PowerShell where -it works perfectly.

7
  • 31
    This does not answer the question. The question is about docker in Jenkins, not git bash on Windows.
    – user1544337
    Commented Feb 12, 2018 at 16:40
  • 104
    Well. true, and it was never intended to. The question pops up in google when you search for this specific error message. I figured, better to have the answer somewhere than not to have it at all. Clearly some people found it useful :) Commented Feb 12, 2018 at 19:11
  • 3
    The problem with Powershell as TTY for shell operations is that it does not properly pass the arrow keys, like the up arrow for cycling command history. Works great other than that shortcoming.
    – Corgalore
    Commented Feb 28, 2018 at 22:49
  • 3
    If you want to continue to use Git Bash, see this answer on another question or the winpty answer below
    – tessafyi
    Commented Jun 18, 2018 at 20:49
  • 2
    It helped me so thank you and one thumbs up from me.
    – Marin
    Commented Jun 1, 2021 at 8:01
102

If you are using git bash on windows, you just need to put

winpty

before your 'docker line' :

winpty docker exec -it some_container bash
4
  • how do you download winpty? Commented Dec 3, 2018 at 1:53
  • 12
    Did you try before asking ? I think it comes with Git (mine is inside .../Git/usr/bin)
    – Gremi64
    Commented Dec 3, 2018 at 10:23
  • You're right, it's under C:\Program Files\Git\usr\bin\winpty.exe Commented Dec 3, 2018 at 20:51
  • Same Case Here, I am running the following command throw .sh script(bash) and python.py However, I get the same error "The input device is not a TTY". Remove -it from the command. If you want to keep it interactive then keep -i Commented Jul 18, 2022 at 8:09
71

In order for docker to allocate a TTY (the -t option) you already need to be in a TTY when docker run is called. Jenkins executes its jobs not in a TTY.

Having said that, the script you are running within Jenkins you may also want to run locally. In that case it can be really convenient to have a TTY allocated so you can send signals like ctrl+c when running it locally.

To fix this make your script optionally use the -t option, like so:

test -t 1 && USE_TTY="-t" 
docker run ${USE_TTY} ...
3
  • This error happen to me when running docker run… command form a makefile task triggered by a git hook Commented Oct 17, 2018 at 18:03
  • 5
    this should be the accepted answer. it actually adresses the problem in a universally applicable manner
    – sgohl
    Commented May 6, 2020 at 16:12
  • I find it weird that docker says "the input device is not a TTY" but we actually need to check if the output device is a tty --"
    – Jaffa
    Commented Jun 3, 2022 at 4:27
16

when using 'git bash',

1) I execute the command:

docker exec -it 726fe4999627 /bin/bash

I have the error:

the input device is not a TTY.  If you are using mintty, try prefixing the command with 'winpty'

2) then, I execute the command:

winpty docker exec -it 726fe4999627 /bin/bash

I have another error:

OCI runtime exec failed: exec failed: container_linux.go:344: starting container process caused "exec: \"D:/Git/usr/bin/
bash.exe\": stat D:/Git/usr/bin/bash.exe: no such file or directory": unknown

3) third, I execute the:

winpty docker exec -it 726fe4999627 bash

it worked.

when I using 'powershell', all worked well.

3
  • Banged my head against the wall, too, for a couple hours with these issues using bash. Switched to Powershell and all works now! Commented Oct 22, 2019 at 18:23
  • (2) fails because winpty converts unix-filepath-like arguments into Windows-speak. To see this clearly: winpty echo "/foo/bar" prints C:/Program Files/Git/foo/bar. Commented Mar 10, 2021 at 17:06
  • You can turn this behavior off with MSYS_NO_PATHCONV=1 Commented Mar 10, 2021 at 17:12
11

Using docker-compose exec -T fixed the problem for me via Jenkins

docker-compose exec -T containerName php script.php
1
  • 1
    This work also on github action with docker compose. Thanks Commented Nov 20, 2020 at 14:43
8

Same Case Here, I am running the following command throw .sh script(bash) and python .py However, I get the same error "The input device is not a TTY".

in my case, I'm trying to take the dump from a running container of my "production" env with authentication and passing with some arguments, then take the output of .bak file of my mssql database container.

Remove -it from the command. If you want to keep it interactive then keep -i.

you can check my .sh file and a long command taking dump. enter image description here

3

if using windows, try with cmd , for me it works. check if docker is started.

2

My Jenkins pipeline step shown below failed with the same error.

       steps {
            echo 'Building ...' 
            sh 'sh ./Tools/build.sh'
        }

In my "build.sh" script file "docker run" command output this error when it was executed by Jenkins job. However it was working OK when the script ran in the shell terminal.The error happened because of -t option passed to docker run command that as I know tries to allocate terminal and fails if there is no terminal to allocate.

In my case I have changed the script to pass -t option only if a terminal could be detected. Here is the code after changes:

DOCKER_RUN_OPTIONS="-i --rm"

# Only allocate tty if we detect one
if [ -t 0 ] && [ -t 1 ]; then
    DOCKER_RUN_OPTIONS="$DOCKER_RUN_OPTIONS -t"
fi

docker run $DOCKER_RUN_OPTIONS --name my-container-name my-image-tag
1
  • I like your answer but I would default to DOCKER_RUN_OPTIONS="--rm" if no TTY detected, since the -i is not needed in Jenkins.
    – ceztko
    Commented Oct 8, 2021 at 18:34
1

Instead of using -it use --tty

So your docker run should look like this:

docker run -v $PWD:/foobar --tty cloudfoundry/cflinuxfs2 /foobar/script.sh
1
  • 1
    by the way, -t and --tty are the same flag, so part of this post is suggesting to both disable and enable the same flag, check out the Liar Paradox for more on this. also, with out -i the input device will still not be a tty Commented Mar 17, 2023 at 0:05
0

In Jenkins, I'm using docker-compose exec -T

eg:-

docker-compose exec -T app php artisan migrate
0

winpty works as long as you don't specify volumes to be mounted such as .:/mountpoint or ${pwd}:/mountpoint

The best workaround I have found is to use the git-bash plugin inside Visual Code Studio and use the terminal to start and stop containers or docker-compose.

0

I know this is not directly answering the question at hand but for anyone that comes upon this question who is using WSL running Docker for windows and cmder or conemu.

The trick is not to use Docker which is installed on windows at /mnt/c/Program Files/Docker/Docker/resources/bin/docker.exe but rather to install the ubuntu/linux Docker. It's worth pointing out that you can't run Docker itself from within WSL but you can connect to Docker for windows from the linux Docker client.

Install Docker on Linux

sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
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

Connect to Docker for windows on the port 2375 which needs to be enabled from the settings in docker for windows.

docker -H localhost:2375 run -it -v /mnt/c/code:/var/app -w "/var/app" centos:7

Or set the docker_host variable which will allow you to omit the -H switch

export DOCKER_HOST=tcp://localhost:2375

You should now be able to connect interactively with a tty terminal session.

0
0

For my case, I use -d instead of -it, and it works like a charm

f"""docker run --rm --name {DOCKERNAME} -d {DOCKERTAG}"""
-1

For those using Pyinvoke see this documentation which I'll syndicate here in case the link dies:

99% of the time, adding pty=True to your run call will make things work as you were expecting. Read on for why this is (and why pty=True is not the default).

Command-line programs often change behavior depending on whether a controlling terminal is present; a common example is the use or disuse of colored output. When the recipient of your output is a human at a terminal, you may want to use color, tailor line length to match terminal width, etc.

Conversely, when your output is being sent to another program (shell pipe, CI server, file, etc) color escape codes and other terminal-specific behaviors can result in unwanted garbage.

Invoke’s use cases span both of the above - sometimes you only want data displayed directly, sometimes you only want to capture it as a string; often you want both. Because of this, there is no “correct” default behavior re: use of a pseudo-terminal - some large chunk of use cases will be inconvenienced either way.

For use cases which don’t care, direct invocation without a pseudo-terminal is faster & cleaner, so it is the default.

-1

Other answer is useful, and script -c 'docker exec -it ubuntu echo 1' /dev/null will be another way, if you're unable to change the shell script which contain the -it switch.

script -c 'docker exec -it ubuntu echo 1' /dev/null # don't record
script -c 'docker exec -it ubuntu echo 1' a.txt # record in a.txt
script -c './some-script.sh' /dev/null # contains lots of docker -it command

The script command provides a pty environment, which solve this error.

The script command may be in coreutils, or you can install busybox, it provides this.

Also in my blog post: https://kkocdko.site/post/202307030023

1
  • Got unvote is quiet depressing for me...
    – kkocdko
    Commented Apr 29 at 18:09
-3

Use only -i flag than -it flag. which can help you to see what going on inside container.

docker exec -i $USER bash <<EOF
 apt install nano -y 
EOF

you might see the warning but it shows you output on the terminal inside docker.

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Nov 26, 2022 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.