Docker api exitcode 0

I want to run a API on my rasberry pi 5 with Kali Linux, but the docker instantly exits with code 0 after creating. The API should connect a locally hosted Minecraft server with a docker running the get.homepage image. The code for the API and everything you need to run it is here: MC/DOCKER-API · GitHub
I‘d be happy if someone could help.

The Homepage docker runs on port 3000 and the rcon port of the server is port 25575

So if I understand it correctly, it is basically a Pyhon question, because I don’t see you returning an exit code in Python: https://www.codecademy.com/article/python-exit-commands-quit-exit-sys-exit-os-exit-and-keyboard-shortcuts

Once it returns the right exit code, the Docker container will ass well.

It is a Docker container. Not a Docker. When you say “a Docker”, people will think of the Docker daemon that runs the containers.

Also could you please, share the code snippets here in the topic? You can insert the code in a code block like this:

```
this is a code
```

Otherwise the topic will not be understandable later if the snippets disappear on GitHub for any reason in the future. Thank you!

When I execute docker ps -a it gives me 0cbf705c1f6a mc-api "python api.py" 36 minutes ago Exited (0) 34 minutes ago mc-api the code is (its a bit bigger than you‘d think)

from datetime import datetime, timezone
import threading
import time
from flask import Flask
from mcrcon import MCRcon  # pip install mcrcon

app = Flask(__name__)

RCON_HOST = "127.0.0.1"
RCON_PORT = 25575
RCON_PASSWORD = "YourStrongPassword"
POLL_INTERVAL = 5

state = {
    "online": False,
    "players": None,
    "error": "Not checked yet",
    "checked_at": None,
}

def poll_minecraft():
    global state
    while True:
        try:
            with MCRcon(RCON_HOST, RCON_PASSWORD, port=RCON_PORT, timeout=3) as mcr:
                resp = mcr.command("list")`

                parts = resp.split()
                online = int(parts[2])
                maxp = int(parts[7])
    state = {
                    "online": True,
                    "players": {"online": online, "max": maxp},
                    "checked_at": datetime.now(timezone.utc).isoformat()
                }
        except Exception as e:
            state = {
                "online": False,
                "error": str(e),
                "checked_at": datetime.now(timezone.utc).isoformat()
            }

        time.sleep(POLL_INTERVAL)

@app.get("/status")
def status():
    return state`

I cant get it as one block.this is the best i could do.

Please, edit your post and add the code block as I described in my previous post. If you need more help, you can find our formatting guide: How to format your forum posts

But you just need to add the three backtick characters before and after the code in a new line. (ALTGR+7 usually)

If you are using the WYSIWYG message composer, then use the </> button to add the code block. Thank you!

It seems your python code is missing to start the services. So the script will just exit. I ran your script through a chatbot and simply asked to add debug information, and the bot silently added:

if __name__ == "__main__":
    print("[DEBUG] Starting poll thread")
    t = threading.Thread(target=poll_minecraft, daemon=True)
    t.start()

    print("[DEBUG] Starting Flask app")
    app.run(host="0.0.0.0", port=5000)

Did you ever try to run your script without a Docker container? It should have exited, too.

2 Likes