Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Compose keep container running

People also ask

How do I run a docker container without stopping?

Detaching Without StoppingPress Ctrl-P, followed by Ctrl-Q, to detach from your connection. You'll be dropped back into your shell but the previously attached process will remain alive, keeping your container running. You can check this by using docker ps to get a list of running containers.

Does docker compose automatically restart container?

Like the restart Docker command, Docker Compose includes the restart property to restart containers automatically.

Do docker containers stay running?

A Docker container runs a process (the "command" or "entrypoint") that keeps it alive. The container will continue to run as long as the command continues to run.


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.


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.


  • Create a file called docker-compose.yml
  • Add the following to the file
version: "3"

services:
  ubuntu:
    image: ubuntu:latest
    tty: true
  • Staying in the same directory, run docker-compose up -d from the terminal
  • Run docker ps to get the container id or name
  • You can run docker inspect $container_id
  • You can enter the container and get a bash shell running docker-compose exec ubuntu /bin/bash or docker-compose exec ubuntu /bin/sh
  • When done, make sure you are outside the container and run docker-compose down

Here's a small bash script (my-docker-shell.sh) to create the docker compose file, run the container, login to the container and then finally cleanup the docker container and the docker compose file when you log out.

#!/bin/bash

cat << 'EOF' > ./docker-compose.yml
---

version: "3"

services:
  ubuntu:
    image: ubuntu:latest
    command: /bin/bash
    # tty: true

...
EOF

printf "Now entering the container...\n"
docker-compose run ubuntu bash
docker-compose down

rm -v ./docker-compose.yml

In the Dockerfile you can use the command:

{CMD sleep infinity}

Some people here write about overwriting the entrypoint so that the command can also have its effect. But no one gives an example. I then:

docker-compose.yml:

version: '3'

services:

    etfwebapp:
        # For messed up volumes and `sudo docker cp`:
        command: "-f /dev/null"
        entrypoint: /usr/bin/tail
        tty: true

# ...

I am not sure if tty is needed at this point. Is it better to do it twice? In my case it did no harm and worked perfectly. Without entrypoint it didn't work for me because then command had no effect. So I guess for this solution tty is optional.

To understand which command is executed at start-up, simply read the entrypoint before the command (concat with space): /usr/bin/tail -f /dev/null.