Docker Compose keep container running
Based on the comment of @aanand on GitHub Aug 26, 2015, one could use tail -f /dev/null
in docker-compose to keep the container running.
docker-compose.yml example
version: '3'
services:
some-app:
command: tail -f /dev/null
Why this command?
The only reason for choosing this option was that it received a lot of thumbs up on GitHub, but the highest voted answer does not mean that it is the best answer. The second reason was a pragmatic one as issues had to be solved as soon as possible due to deadlines.
To keep a container running when you start it with docker-compose
, use the following command
command: tail -F anything
In the above command the last part anything
should be included literally, and the assumption is that such a file is not present in the container, but with the -F
option (capital -F
not to be confused with -f
which in contrast will terminate immediateley if the file is not found) the tail
command will wait forever for the file anything
to appear. A forever waiting process is basically what we need.
So your docker-compose.yml becomes
version: '2'
services:
my-test:
image: ubuntu
command: tail -F anything
and you can run a shell to get into the container using the following command
docker exec -i -t composename_my-test_1 bash
where composename
is the name that docker-compose
prepends to your containers.
You can use tty
configuration option.
version: '3'
services:
app:
image: node:8
tty: true # <-- This option
Note: If you use Dockerfile for image and CMD
in Dockerfile, this option won't work; however, you can use the entrypoint
option in the compose file which clears the CMD
from the Dockerfile.