1

Official Docker images like MySQL can be run like this:

docker run -d --name mysql_test mysql/mysql-server:8.0.13

And it can run indefinitely in the background.

I want to try to create an image which does the same, specifically a Flask development server (just for testing). But my container exit immediately. My Dockerfile is like this:

FROM debian:buster
ENV TERM xterm
RUN XXXX # some apt-get and Python installation stuffs
ENTRYPOINT [ "flask", "run", "--host", "0.0.0.0:5000" ]
EXPOSE 80
EXPOSE 5000
USER myuser
WORKDIR /home/myuser

However it exited immediately as soon as it is ran. I also tried "bash" as an entry point just so to make sure it isn't a Flask configuration issue and it also exited.

How do I make it so that it runs as THE process in the container?

EDIT

OK someone posted below (but later deleted), the command to test is to use tail -f /dev/null, and it does run indefinitely. I still don't understand why bash doesn't work as a process which doesn't exist (does it?). But my flask configuration is probably off.

EDIT 2

I see that running without the -d flag print out the stdout (or stderr) so I can diagnose the problem.

huggie
  • 17,587
  • 27
  • 82
  • 139
  • 2
    Possible duplicate of [How to keep Docker container running after starting services?](https://stackoverflow.com/questions/25775266/how-to-keep-docker-container-running-after-starting-services) – leopal Sep 03 '19 at 12:30

2 Answers2

2

Let's clear things out.

In general, a container exits as soon as its entrypoint is successfully executed.

In your case, without being a python expert this ENTRYPOINT [ "flask", "run", "--host", "0.0.0.0:5000" ] would be enough to keep the container alive. But I guess you have some configuration error and due to that error the container exited before running flask command. You can validate this by running docker ps -a and inspect the exit code(possibly 1).

Let's now discuss about the questions in your edits.

The key part of your misunderstanding derives from the -d flag.

You are right to think that setting bash as entrypoint would be enough to keep container alive but you need to attach to that shell.

When running in detach mode(-d), container will execute bash command but as soon as no one is attached to that shell, it will exit. In addition, using this flag will prevent you from viewing container logs lively(however you may use docker logs container_id to debug) which is very useful when you are in an early phase of setting thing up. So I recommend using this flag only when you are sure that everything works as intended.

To attach to bash shell and keep container alive, you should use the -it flag so that the bash shell will be attached to the current shell invoking the docker run command.

-t : Allocate a pseudo-tty

-i : Keep STDIN open even if not attached

Please also consult official documentation about foreground vs background mode.

Community
  • 1
  • 1
leopal
  • 4,711
  • 1
  • 25
  • 35
  • 1
    Thanks! My flask configuration did had some problem and this explains a whole lot about the confusion around bash and detached mode. – huggie Sep 04 '19 at 06:54
0

The answer to your edit is: when do docker run <container> bash it will literally call bash and exit 0, because the command (bash) was successful. Bash isn't a shell, it's a command.

If you ran docker run -it <container> tail -f /dev/null and then docker exec -it /bin/bash. You'd drop into the shell, because its the command you ran.

Your Dockerfile doesn't have a command to run in the background that is persistent, in mysqls case, it runs mysqld, which starts a server on PID 0.

When PID 0 exits, the container stops.

Your entrypoint is most likely failing to start, or starting and exiting because of how your command is running.

I would try changing your entrypoint to a

theoneandonlyak
  • 490
  • 4
  • 7