I have some script I need to run during a Docker build which requires a tty (which Docker does not provide during a build). Under the hood the script uses the read
command. With a tty, I can do things like (echo yes; echo no) | myscript.sh
.
Without it I get strange errors I don't completely understand. So is there any way to use this script during the build (given that its not mine to modify?)
EDIT: Here's a more definite example of the error:
FROM ubuntu:14.04
RUN echo yes | read
which fails with:
Step 0 : FROM ubuntu:14.04
---> 826544226fdc
Step 1 : RUN echo yes | read
---> Running in 4d49fd03b38b
/bin/sh: 1: read: arg count
The command '/bin/sh -c echo yes | read' returned a non-zero code: 2
RUN <command>
inDockerfile
reference:let's see what exactly
/bin/sh
is in ubuntu:14.04:/bin/sh is a symbolic link of
dash
, seeread
function indash
:read
function indash
:let's see
read
function inbash
:So I guess your script
myscript.sh
is start with#!/bin/bash
or something else but not/bin/sh
.Also, you can change your
Dockerfile
like below:Links:
Most likely you don't need a tty. As the comment on the question shows, even the example provided is a situation where the
read
command was not properly called. A tty would turn the build into an interactive terminal process, which doesn't translate well to automated builds that may be run from tools without terminals.If you need a tty, then there's the C library call to
openpty
that you would use when forking a process that includes a pseudo tty. You may be able to solve your problem with a tool likeexpect
, but it's been so long that I don't remember if it creates a ptty or not. Alternatively, if your application can't be built automatically, you can manually perform the steps in a running container, and thendocker commit
the resulting container to make an image.I'd recommend against any of those and to work out the procedure to build your application and install it in a non-interactive fashion. Depending on the application, it may be easier to modify the installer itself.
You don't need a tty for feeding your data to your script . just doing something like
(echo yes; echo no) | myscript.sh
as you suggested will do. also please make sure you copy your file first before trying to execute it . something likeCOPY myscript.sh myscript.sh