15

I want to use a Dockerfile to build an image. However, commands will need user input as they run. Currently, the build is not successful because docker exits on user input. I know I can use the -i -t options on docker run command but I want to do that on a Dockerfile. How is that possible?

Keeto
  • 4,074
  • 9
  • 35
  • 58
  • It depends on what userinput, if it's userinput for apt-get update as example, then you can add -y to always say yes. Otherwise, run something that takes userinput, save it in an environment variable and then pull it from the environment variables. – Jan Vladimir Mostert Jun 20 '14 at 20:25
  • I am running 'rvmsudo passenger-install-nginx-module'. I am not sure your recommendation applies here. – Keeto Jun 20 '14 at 20:28
  • 1
    In that case I would build a base image with those modules already installed (manually entering the needed data), then create Dockerfiles using that new image. I'm curious to see if there's a generic way to solve it if the script you're running doesn't support unattended setup ... – Jan Vladimir Mostert Jun 20 '14 at 20:34
  • Depending on what command needs user input they are ways to get around it, for instance to set the password 'f00' for user 'bar' you could add to the Dockerfile: RUN echo 'bar:f00' | chpasswd – Thomasleveil Jun 21 '14 at 12:54

1 Answers1

7

You can try with expect or a similar tool.

The easiest way to configure it is using the autoexpect tool, which lets you run the commands interactively and creates an expect script for you.

I couldn't get the rvmsudo stuff working (I haven't used it and didn't want to spend too much time with it) so I decided to use vi instead. First run autoexpect

$ autoexpect vi test

This will open vi and you can create or edit the file and save it. After exiting the vi you'll see your file test as well as an expect script script.exp.

You can then remove the test file and execute script.exp. It will recreate the same file using the same steps.

The autoexpect tool is great, but you may have to create a script from scratch if you need to have more control over what happens. E.g. if you don't want the script to work with the exact expected input.

ivant
  • 3,909
  • 1
  • 25
  • 39
  • 5
    Gotta love downvoters, who don't comment on what's wrong with he answer. Cheers to you! – ivant Jul 14 '15 at 14:02