8

I have created a docker container that runs a command line tool. The container is supposed to be interactive. Am I somehow able to specify in the Dockerfile that the container is always started in interactive mode?

For reference this is the dockerfile:

FROM ubuntu:latest

RUN apt-get update && apt-get -y install curl

RUN mkdir adr-tools && \
    cd adr-tools && \
    curl -L https://github.com/npryce/adr-tools/archive/2.2.0.tar.gz --output adr-tools.tar.gz && \
    tar -xvzf adr-tools.tar.gz && \
    cp */src/* /usr/bin && \
    rm -rf adr-tools

CMD ["/bin/bash"]

EDIT: I know of the -it options for the run command. I'm explicitly asking for a way to do this in the docker file.

EDIT2: This is not a duplicate of Interactive command in Dockerfile since my question addresses an issue with how arguments specified to docker run can be avoided in favor of specifying them in the Dockerfile whereas the supposed duplicate addresses an issue of interactive input during the build of the image by docker itself.

SpaceTrucker
  • 13,377
  • 6
  • 60
  • 99
  • 1
    Dockerfile doesnt create container but it creates image. There is significant diffrence. docker-compose is orchestrator for docker containers and you can define what to do with running containers. In that case it might have been responded https://stackoverflow.com/questions/36249744/interactive-shell-using-docker-compose – Mazel Tov Jul 17 '18 at 13:28
  • Possible duplicate of [Interactive command in Dockerfile](https://stackoverflow.com/questions/40854482/interactive-command-in-dockerfile) – juanlumn Jul 18 '18 at 06:26
  • I too am struggling to find a solution for this. I'd prefer to have a single command that I can copy and paste rather than build and run. I guess the Docker developers don't think this is an important enough use-case. Thanks for posting. – Sridhar Sarnobat Sep 21 '22 at 04:50

4 Answers4

6

Many of the docker run options can only be specified at the command line or via higher-level wrappers (shell scripts, Docker Compose, Kubernetes, &c.). Along with port mappings and network settings, the “interactive” and “tty” options can only be set at run time, and you can’t force these in the Dockerfile.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • That makes sense, as much as I wish there was some syntactic sugar. It bothers me the same way OOP forces you to create an object before you call a method rather than just calling a non OOP function directly. – Sridhar Sarnobat Sep 21 '22 at 04:56
1

You can use the docker run command.

docker build -t curly .
docker run -it curly curl https://stackoverflow.com

The convention is:

docker run -it IMAGE_NAME [COMMAND] [ARG...]

Where [COMMAND] is curl and [ARG...] are the curl arguments, which is https://stackoverflow.com in my example.

-i enables interactive process mode. You can't specify this in the Dockerfile.
-t allocates a pseudo-TTY for the container.

Alexander Zeitler
  • 11,919
  • 11
  • 81
  • 124
0

Are you looking for the -it option?

From the Docker documentation:

For interactive processes (like a shell), you must use -i -t together in order to allocate a tty for the container process.

So, for example you can run it like:

docker run -it IMAGE_NAME [COMMAND] [ARG...]
juanlumn
  • 6,155
  • 2
  • 30
  • 39
0

Actually, in Ubuntu, I am running Apache Server in the background.

But for you, Try with below command and you should be able to go inside docker container.

docker exec -i -t your_container_name bash
Rohan J Mohite
  • 2,283
  • 10
  • 19