Creating a Docker Container for Background Processes
Q: Develop a Docker container that runs a background process, such as a worker or a task scheduler, using tools such as supervisord or systemd.
- Docker
- Mid level question
Explore all the latest Docker interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create Docker interview for FREE!
To develop a Docker container that runs a background process, such as a worker or a task scheduler, you can use tools such as supervisord or systemd. Here is an example Dockerfile that uses supervisord to run a background process:
FROM ubuntu:latest # Update the system and install necessary packages RUN apt-get update && \ apt-get install -y supervisor && \ apt-get clean # Create the necessary directories for supervisor RUN mkdir -p /var/log/supervisor && \ mkdir -p /etc/supervisor/conf.d # Copy the supervisor configuration file COPY supervisord.conf /etc/supervisor/supervisord.conf # Add your worker or task scheduler script COPY worker.py /usr/local/bin/ # Define the command to run the supervisor daemon CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]
In this example, the Dockerfile starts with a base image of Ubuntu and installs the necessary packages for supervisor. It then creates the necessary directories for supervisor and copies the supervisor configuration file into place. Finally, it adds your worker or task scheduler script and defines the command to run the supervisor daemon.
Here is an example supervisord.conf file that defines a program named "worker" that runs your worker script:
[supervisord] nodaemon=true [program:worker] command=/usr/bin/python /usr/local/bin/worker.py autostart=true autorestart=true stdout_logfile=/var/log/supervisor/worker.log stderr_logfile=/var/log/supervisor/worker.err
In this example, the supervisord.conf file defines a program named "worker" that runs your worker script. The autostart and autorestart options ensure that the worker is started automatically and restarted if it fails. The stdout_logfile and stderr_logfile options define the log files for the worker's output.
To build the Docker image, run the following command from the directory containing the Dockerfile:
docker build -t my-worker .
To run the Docker container, use the following command:
docker run -d my-worker


