Keep container alive and linked using docker-compose

If you run with docker-compose up and there are no errors printed to the terminal then it suggests that the service is stopping because it is finished (as opposed to encountering some error).

One potential cause for error is that you are sending stuff to PHP (installing composer) which will kill the interactive php terminal that is fired by the php:5.6-cli image. To start the interactive php shell again add the following to the end of your Dockerfile:

CMD ["php", "-a"]

and try again with docker-compose up

Sidenote: when everything is running properly you can run docker-compose up -d to run in daemon mode, giving you control of your terminal again (all stdout + stderr will be logged within the docker container's respective log files).

You can then attach to the container to do your thing. I always found that docker exec -it <containerID> bash was quicker than docker attach <container id>

Hope this helps.


Join entrypoint so that the process does not exit

version: "3.7"
services:
    debug-srv:
        container_name: "debug-srv"
        image: "alpine:latest"
        entrypoint: ["tail", "-f", "/dev/null"]
        networks:
            debug-net:
networks:
    debug-net:
        name: "debug-net"

Had a similar issue with running a zsh shell on docker container running with compose, it would close with exit code 0 immediately after start.

@Spock's comment below the last answer is actually the key, at least for what I need.

Set your docker-compose command for the image to be:

command: tail -f /dev/null

This keeps the process alive but also allows it to shutdown gracefully.


So docker-compose is just a stand-in for the docker-engine client. It maintains feature parity with the client. For diagnosing problems like this, you should drop the use of docker-compose until you get it working with the regular ole client. Based on your comments here and on the other answer, it just sounds like you're not running a container with a daemon process in the foreground. If you want to run an interactive shell in Docker, you have to use the -it flags (-t allocates a tty and -i initiates an interactive session). If you don't run Docker with those switches, your container won't survive you starting an interactive shell, e.g. php -a.

It helps to think of Docker as a fancy way to run a process and not a virtual machine. It's not some "environment" that exists outside of the lifetime of whatever process (and its children) you are running. Normally, PHP is invoked by some server (e.g. Apache, Nginx, etc). What you're implying here is that you need a PHP process to run "permanently" so that you can drop into the container and test some things. Except for the interactive shell, that's not going to be possible, and you need specifically to use the -it switch to keep an interactive shell process alive in your container. The real answer here is that you can't do what you're trying to do here (keep a PHP container running) without some related daemon/server process listening in the foreground. The reason for that is because that's not how PHP works. If you really want to get into a container from your PHP image, just drop into a shell on it:

docker run -it apollo/php /bin/bash

... And you'll start a container from your PHP image, and get a shell on the container (which will die as soon as you exit the shell). But again, just reiterating from my first paragraph, docker-compose is not the way to go here.