简体   繁体   中英

How do avoid a docker container stop after the application is stopped

There is a docker container with Postgres server. Ones postgres is stopped or crashed (doesn't matter) I need to check some environment variables and the state of a few files.

By default, the container stops after an application is finished. I know there is an option to change the default behavior in dockerfile but I no longer to find it (( If somebody knows that please give me an Dockerfile example like this :

FROM something

RUN something ...

ENTRYPOINT [something]

You can simply run non exiting process in the end of entrypoint to keep the container alive, even if the main process exits. For example use

tail -f 'some log file'

There isn't an "option" to keep a container running when the main process has stopped or died. You can run something different in the container while debugging the actual startup scripts. Sometimes you need to override an entrypoint to do this.

docker run -ti $IMAGE /bin/sh
docker run -ti --entrypoint=/bin/sh $IMAGE

If the main process will not stay running when you docker start the existing container then you won't be able to use that container interactively, otherwise you could:

docker start $CID
docker exec -ti $CID sh 

For getting files from an existing container, you can docker cp anything you need from the stopped container.

docker cp $CID:/a/path /some/local/path

You can also docker export a tar archive of the complete container.

docker export $CID -o $CID.tar
tar -tvf $CID.tar | grep afile

The environment Docker injects can be seen with docker inspect , but this won't give you anything the process has added to the environment.

docker inspect $CID --format '{{ json .Config.Env }}'

In general, Docker requires a process to keep running in the foreground. Otherwise, it assumes that the application is stopped and the container is shut down. Below, I outline a few ways, that I'm aware of, which can prevent a container from stopping:

Use a process manager such as runit or systemd to run a process inside a container :

As an example, here you can find a Redhat article about running systemd within a docker container.

A few possible approaches for debugging purposes:

a) Add an artificial sleep or pause to the entrypoint:

For example, in bash, you can use this to create an infinite pause:

while true; do sleep 1; done

b) For a fast workaround, one can run the tail command in the container:

As an example, with the command below, we start a new container in detached/background mode (-d) and executing the tail -f /dev/null command inside the container. As a result, this will force the container to run forever.

docker run -d ubuntu:18.04 tail -f /dev/null

And if the main process crashed/exited, you may still look up the ENV variable or check out files with exec and the basic commands like cd, ls. A few relevant commands for that:

docker inspect -f \
   '{{range $index, $value := .Config.Env}}{{$value}} {{end}}' name-of-container

docker exec -it name-of-container bash

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM