Why docker container exits immediately

后端 未结 15 2233
温柔的废话
温柔的废话 2020-11-22 13:42

I run a container in the background using

 docker run -d --name hadoop h_Service

it exits quickly. But if I run in the foreground, it works

相关标签:
15条回答
  • 2020-11-22 14:27

    I added read shell statement at the end. This keeps the main process of the container - startup shell script - running.

    0 讨论(0)
  • 2020-11-22 14:29

    This did the trick for me:

    docker run -dit ubuntu
    

    After it, I checked for the processes running using:

    docker ps -a
    

    For attaching again the container

    docker attach CONTAINER_NAME
    

    TIP: For exiting without stopping the container type: ^P^Q

    0 讨论(0)
  • 2020-11-22 14:29

    You need to run it with -d flag to leave it running as daemon in the background.

    docker run -d -it ubuntu bash

    0 讨论(0)
  • 2020-11-22 14:31

    A nice approach would be to start up your processes and services running them in the background and use the wait [n ...] command at the end of your script. In bash, the wait command forces the current process to:

    Wait for each specified process and return its termination status. If n is not given, all currently active child processes are waited for, and the return status is zero.

    I got this idea from Sébastien Pujadas' start script for his elk build.

    Taking from the original question, your start-all.sh would look something like this...

     #!/usr/bin/env bash
     /etc/init.d/hadoop-hdfs-namenode start &
     /etc/init.d/hadoop-hdfs-datanode start &
     /etc/init.d/hadoop-hdfs-secondarynamenode start &
     /etc/init.d/hadoop-0.20-mapreduce-tasktracker start &
     sudo -u hdfs hadoop fs -chmod 777 /
     /etc/init.d/hadoop-0.20-mapreduce-jobtracker start &
     wait
    
    0 讨论(0)
  • 2020-11-22 14:38

    I would like to extend or dare I say, improve answer mentioned by camposer

    When you run

    docker run -dit ubuntu
    

    you are basically running the container in background in interactive mode.

    When you attach and exit the container by CTRL+D (most common way to do it), you stop the container because you just killed the main process which you started your container with the above command.

    Making advantage of an already running container, I would just fork another process of bash and get a pseudo TTY by running:

    docker exec -it <container ID> /bin/bash
    
    0 讨论(0)
  • 2020-11-22 14:39

    Adding

    exec "$@"
    

    at the end of my shell script was my fix!

    0 讨论(0)
提交回复
热议问题