简体   繁体   中英

Is it possible to let docker container keep running when no foreground process is attached

My DockerFile is of the following logic:

1. Start the server in the background (with `&` at the end of the command)
2. Run some queries against the server, these are foreground processes

My goal is to keep the container running when all logics in the DockerFile has been executed. However, when I do docker run -d , the container keeps exiting with error code 0 , after running the queries in the step 2 above.

From this post I learnt that

Exit code 0 indicates that the specific container does not have a foreground process attached.

Which is true in my case -- there's only the background server running. Why the server has to run in the background? Because if not, the runtime would be "blocked" by starting the server, and will not proceed to run the queries in step 2.

I wonder if there's a workaround for this, eg any magic flags that enables running the docker container without a foreground process, or maybe is there a recommended way to spin up a no-op foreground process at the end of the dockerFile, so that the container can attach to it and continue running?

Note that this question is different from Docker container will automatically stop after "docker run -d" --

I have tried docker run -t , docker run -t -d , docker run -it , docker run -it -d , and none of them work!

You should a script to run by the CMD instruction inside you docker file which looks like the following pseudo script:

#!/bin/bash

# Invoke your server in background
your_server_process&

# Do some invokes on your service...

# Wait for server termination (by container gracefully killing)
wait

Look at this document for further details on the wait instruction.

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