Home  >  Q&A  >  body text

container - 为了让docker容器一直运行难道要写个死循环的程序?

为了让docker容器一直运行难道要写个死循环的程序?
我想让容器一直运行

ringa_leeringa_lee2707 days ago1518

reply all(9)I'll reply

  • 天蓬老师

    天蓬老师2017-04-24 09:11:36

    $ docker run -d --name test_sleep_infinity your_image sleep infinity
    # enter the container
    $ docker exec -it test_sleep_infinity /bin/bash

    reply
    0
  • PHPz

    PHPz2017-04-24 09:11:36

    docker run -d .....

    -d parameter is to keep docker running in the background,

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-24 09:11:36

    If your run command can run continuously and cannot be stopped after running like ls, then just add -d

    reply
    0
  • 高洛峰

    高洛峰2017-04-24 09:11:36

    If you want to use -d to run in the background, you can currently only use the command that can run continuously, otherwise it will stop when the operation is completed.

    reply
    0
  • 迷茫

    迷茫2017-04-24 09:11:36

    I don’t particularly understand your scenario. I see that you have asked several docker-related questions. I will give you a few commands for your reference. I hope they are useful to you.

    创建启动容器   docker run -itd <image-di> /bin/bash
    单纯启动容器   docker start <container-id>
    连接容器      docker attach <container-id>
    回到宿主机    ctrl+p,ctrl+q      

    reply
    0
  • 黄舟

    黄舟2017-04-24 09:11:36

    You can just run a program in the foreground, or use supervisor to manage running programs

    reply
    0
  • PHP中文网

    PHP中文网2017-04-24 09:11:36

    You can do this

     CMD tail -f /var/log/cron.log 
    

    Run a continuous output task in DockerFile
    Okay, this is almost an infinite loop

     docker ps -a
    


    Here you can see the running status of docker, up means it keeps running, and exit means it exits after it finishes running. Docker will exit when it finishes running the task you want it to perform. So you have to write a continuous Command

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-24 09:11:36

    Even if -d is added, as long as the command is finished, docker will think that it has nothing to do, so it will automatically exit. To make the container run in the background, it is necessary to have a foreground process.
    You can add top, tail, etc. after the command to always be in the foreground. Running process

    reply
    0
  • 黄舟

    黄舟2017-04-24 09:11:36

    Let me go, everyone upstairs did not understand what the original poster meant. . . What he means is that there is no continuous running inside docker (i.e. running in the background).
    If you want docker to keep running without exiting, you can use supervisord

    reply
    0
  • Cancelreply