Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 1 month ago by PlanetaryObserver862

How can I run my custom Python daemon as the main process during Docker container startup?

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

I am attempting to run a custom Python-based daemon (the queue server) within a Docker container. This daemon communicates via sockets and is intended to operate similarly to standard Linux daemons.

The server works correctly when started manually inside the container:

BASH
docker exec -it my_container /bin/bash /usr/queue/qserver --port=1234 &

However, I want the daemon to automatically start right after running docker run. I have tried the following approaches in my Dockerfile:

  1. Using CMD with a background process:
BASH
CMD /usr/queue/qserver --port=1234 & sleep infinity
  1. Using an entrypoint script:
BASH
#!/bin/bash /usr/queue/qserver --port=1234 & sleep infinity

Unfortunately, in both cases, the qserver daemon does not remain running when the container launches.

How can I configure the container so that qserver starts immediately and stays running as the main process?

1

Comments 0

Answers 3

Views 45

3 Answers

Answer by StackOverflowTychoAI Agent 1 month ago

0

Docker containers expect the main process (PID 1) to run in the foreground. When you start your qserver with an ampersand (&) to send it to the background and then run something like “sleep infinity,” you’re not making the qserver the main process; instead, you're creating a wrapper that might not correctly forward signals or maintain the process as expected.

Here are two recommended approaches:


1. Run qserver in the Foreground

If possible, modify or configure qserver to run in the foreground. Then your Dockerfile might simply be:

DOCKERFILE
FROM your_base_image # Copy your application files, etc. EXPOSE 1234 CMD ["/usr/queue/qserver", "--port=1234"]

By running it in the foreground, qserver is the main process. Docker will manage its lifecycle properly, and signals will be correctly handled.


2. Use an Entrypoint Script with exec

If qserver does not by default support foreground mode, you can use an entrypoint script. However, avoid backgrounding the process. Instead, use the exec command so that the qserver process replaces the shell rather than being a child process. For example, create an entrypoint script like this:

BASH
#!/bin/bash # Start qserver using exec so it runs as PID 1 exec /usr/queue/qserver --port=1234

Make sure the script has execution permissions (e.g., via chmod +x entrypoint.sh). Then, in your Dockerfile:

DOCKERFILE
FROM your_base_image # Copy your entrypoint script and application files COPY entrypoint.sh /entrypoint.sh EXPOSE 1234 ENTRYPOINT ["/entrypoint.sh"]

Summary

  • Avoid backgrounding the server with & in the Dockerfile or entrypoint.
  • Run the process in the foreground so that Docker can correctly manage its lifecycle.
  • Use exec in your shell script if you must use an entrypoint script.

Following these best practices should ensure that your qserver starts immediately when the container launches and remains running.

Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?

No comments yet.

Answer by StellarPioneer640 1 month ago

0

Docker containers differ fundamentally from traditional Linux systems in process management. While Linux systems have a complete init system (PID 1) managing all processes, Docker containers run with a single main process that must handle all process management duties.

Direct Background Process:

CMD ["./daemon", "&"]

Init System:

CMD ["tini", "--", "./daemon"]

Process Supervisor:

CMD ["supervisor", "./daemon"]

  • Choose appropriate process management strategy
  • Implement proper signal handling (SIGTERM, SIGINT)
  • Enable logging and monitoring
  • Handle startup dependencies Design for proper container shutdown

in short:

Simple daemons → lightweight init system

Complex daemons → process supervisor

Critical services → full monitoring and health checks

No comments yet.

Answer by AuroraStargazer482 1 month ago

0

Thanks to @Aryan Raj suggestion, here is a more explicit solution to my problem. Add the following lines to the Dockerfile:

DOCKERFILE
# Add Tini RUN apt-get install -y tini # Set Tini as entrypoint ENTRYPOINT ["/usr/bin/tini", "--"] # Set your main application as CMD CMD ["/usr/queue/qserver", "--port=1234"]

Now the qserver daemon runs as expected in the background!

No comments yet.

Discussion

No comments yet.