0

I'm developing a docker image based on nvidia/cuda:10.1-base-ubuntu18.04. At build time a custom web server is installed and I have a docker-entrypoint.sh which runs the server depending if an env var is set at run time.

This all works fine if I run the container in interactive mode:

docker run -it -p 5000:5000 -e RUN_SERVER=1 myImage/17.5.360/all /bin/bash

Or use the old "hack" to keep the image alive:

docker run -d -p 5000:5000  -e RUN_SERVER=1 myImage/17.5.360/all /bin/bash -c "tail -f /dev/null"

But when I follow the most recent answer from here:

docker run -t -d -p 5000:5000 -e RUN_SERVER=1 myImage/17.5.360/all

The image appears to load successfully and is kept alive, but the server is not active.

Here's the abridged version of docker-entrypoint.sh:

#! /bin/bash
if [ $RUN_SERVER = "1" ]; then
   runMyServer
fi
exec "$@"

And Dockerfile:

FROM nvidia/cuda:10.1-base-ubuntu18.04 AS HInstaller  
ENV RUN_SERVER=0  # Defaults to not run the server

# ...

COPY "./docker-entrypoint.sh" /usr/local/bin/
ENTRYPOINT ["docker-entrypoint.sh"]

How can I debug what's happening since interactive mode works?

4

1 回答 1

0

Your entrypoint script ends with the line

exec "$@"

which means to replace the current process with whatever got passed as command-line arguments. When there is both an entrypoint and a command, the command gets passed as arguments to the entrypoint.

In your first two examples, you are giving some command on the docker run command line, and the container will keep running as long as that stays running. In your last two examples, absent anything on either the command line or a Dockerfile CMD instruction, the command will be empty. This exec line won't do anything, and the entrypoint script will complete successfully.

It's not really good practice to have a container that runs an interactive shell or a meaningless tail command, and as a side effect runs some service. You want to start your service as a foreground process, and make it be the main container process. It's extra work for a service to start itself in the background so usually there will be some sort of "foreground" option to bypass it. If you put something like

CMD ["runMyServer", "--foreground"]

this will run in that exec "$@" line. If your entrypoint script gets reduced to just that line, you can remove it.

于 2019-10-24T10:45:59.023 回答