You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Docker容器中运行后台程序并实现容器重启自启?

Hey there, I’ve run into this exact issue with background daemons in Docker before—let’s break down how to fix it so your tinyproxy container stays up and auto-starts when the container restarts.

The Root Problem

Docker containers stop as soon as their main process exits. When you run tinyproxy directly in detached mode, it defaults to running as a background daemon, so the initial tinyproxy command exits immediately, and Docker kills the container right after. Your sleep trick didn’t work because that sleep command was running on your host machine, not inside the container—so it didn’t keep the container alive at all.

Solution 1: Run Tinyproxy in Foreground Mode (Best Practice)

The cleanest fix is to configure tinyproxy to run in the foreground instead of as a daemon. This makes it the container’s main process, so Docker will keep the container running as long as tinyproxy is active.

Here’s how to update your Dockerfile:

  1. Modify the tinyproxy config to disable daemon mode:
    # Assuming your base image is Debian/Ubuntu-based
    FROM ubuntu:latest
    
    RUN apt-get update && apt-get install -y tinyproxy
    # Edit the config to turn off daemon mode
    RUN sed -i 's/Daemon yes/Daemon no/' /etc/tinyproxy/tinyproxy.conf
    # Set the foreground-running tinyproxy as the container's entrypoint
    CMD ["tinyproxy", "-c", "/etc/tinyproxy/tinyproxy.conf"]
    
  2. Rebuild your image:
    docker build -t tinyproxy_local .
    
  3. Run the container with a restart policy to auto-start on reboot/container exit:
    docker run -d --restart=always tinyproxy_local
    

The --restart=always flag tells Docker to automatically restart the container if it stops (including when your host reboots). If you only want it to restart on failures, use --restart=on-failure:3 (restarts up to 3 times on error) or --restart=unless-stopped (restarts unless you manually stop it).

Solution 2: Use Supervisor for Process Management (For Multi-Process Scenarios)

If you ever need to run multiple services in the same container, a process manager like supervisord works great. It runs in the foreground and keeps track of your background services, restarting them if they crash.

  1. Update your Dockerfile to install supervisor:
    FROM ubuntu:latest
    
    RUN apt-get update && apt-get install -y tinyproxy supervisor
    # Create a directory for supervisor logs
    RUN mkdir -p /var/log/supervisor
    # Copy your supervisor config into the container
    COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
    # Start supervisor in foreground mode
    CMD ["/usr/bin/supervisord", "-n"]
    
  2. Create a supervisord.conf file in your build context:
    [supervisord]
    nodaemon=true  # Run supervisor in foreground
    
    [program:tinyproxy]
    command=tinyproxy  # Command to start tinyproxy
    autostart=true     # Start tinyproxy when supervisor starts
    autorestart=true   # Restart tinyproxy if it crashes
    stdout_logfile=/var/log/supervisor/tinyproxy.log
    stderr_logfile=/var/log/supervisor/tinyproxy.err.log
    
  3. Rebuild and run with the restart policy:
    docker build -t tinyproxy_local .
    docker run -d --restart=always tinyproxy_local
    

Why Your Sleep Trick Failed (And A Better Temporary Hack)

Your original command docker run -d tinyproxy_local tinyproxy && sleep 99999999999 ran the sleep on your host, not inside the container. To make it work (though I don’t recommend this long-term), you’d need to run both commands inside the container:

docker run -d tinyproxy_local sh -c "tinyproxy && sleep infinity"

But this is a hack—if tinyproxy crashes, the sleep will keep running, so Docker won’t restart tinyproxy or the container. Stick to the foreground or supervisor methods for reliability.

内容的提问来源于stack exchange,提问作者Wei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:09:04