
1. Overview
In this article, we will learn to fix the issue “Docker EOFError: EOF when reading a line”.
2. Docker attached mode
When starting a Docker container using the docker run
command, it runs in the default “attached” mode.
For example, the following command runs the container in the foreground attached mode.
> docker run -p 8081:8081 9f356a6e96f4 output logs of the container rolls here
The attached mode (interactive session) will attach your terminal to a running container. This allows you to view the container’s ongoing output or to control it interactively, as though the commands were running directly in your terminal.
It simply means you will see the output of the container in your terminal. However, you could not enter any input.
For example, assume your python application requires user input:
number = int(input('Please enter the number: ')) print(number)
FROM python WORKDIR /app COPY . /app/ CMD [ "python", "sample.py" ]
When you run the docker image after building it using the above Dockerfile
, it throws “Docker EOFError: EOF when reading a line” error.
Please enter the min number: Traceback (most recent call last): File "/app/sample.py", line 1, in <module> number = int(input('Please enter the number: ')) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EOFError: EOF when reading a line
3. Docker EOFError: EOF when reading a line
When using Docker run
command, it attaches your terminal to the container so we can listen to output printed by the container but we can’t input anything into the container or into the application running into the container.
You can fix this by using any of the following configuration options:
--interactive
or-i
: Launches the container in interactive mode and keeps STDIN open even if not attached--tty
or-t
: Allocate a pseudo-TTY. This option tells the Docker to allocate a virtual terminal session within the container.
If you combine both options, we can input something so the container will listen to the input and we will also get a terminal exposed by the container which is actually the device where we enter the input.
> docker run -i -t sha256:8500cda89e73276cff391ecb6fba1f28d83ca0aafdd5e47d6e66c857d0f68a5f Please enter the number: 10 10
Alternatively, you can combine -i
and -t
together as -it
as below:
> docker run -i -t sha256:8500cda89e73276cff391ecb6fba1f28d83ca0aafdd5e47d6e66c857d0f68a5f Please enter the number: 10 10
While restarting the container using the docker start
command, you must use -i
to enter input. There is no need to use the -tty
option as docker remembers it if we ran the container originally with that flag.
The flag -a
attaches the terminal to the container.
> docker start -a -i dazzling_dart
4. Conclusion
To sum up, we have learned to fix Docker EOFError.