我运行一个容器在后台使用

 docker run -d --name hadoop h_Service

它很快就会消失。但如果我在前台运行,它工作得很好。我用

docker logs hadoop

没有错误。什么好主意吗?

DOCKERFILE

 FROM java_ubuntu_new
 RUN wget http://archive.cloudera.com/cdh4/one-click-install/precise/amd64/cdh4-repository_1.0_all.deb
 RUN dpkg -i cdh4-repository_1.0_all.deb
 RUN curl -s http://archive.cloudera.com/cdh4/ubuntu/precise/amd64/cdh/archive.key | apt-key add -
 RUN  apt-get update
 RUN apt-get install -y hadoop-0.20-conf-pseudo
 RUN dpkg -L hadoop-0.20-conf-pseudo
 USER hdfs
 RUN hdfs namenode -format
 USER root
 RUN apt-get install -y sudo
 ADD . /usr/local/
 RUN chmod 777 /usr/local/start-all.sh
 CMD ["/usr/local/start-all.sh"]

start-all.sh

 #!/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
 /bin/bash

当前回答

来自副本,我在这里没有看到任何答案,解决了将主要工作负载作为后台作业运行的常见反模式,然后想知道Docker为什么退出。

简单来说,如果你有

my-main-thing &

然后取出&在前台运行作业,或者添加

wait

在脚本的末尾,使其等待所有后台作业。

如果主工作负载退出,它仍然会退出,所以可以在while true循环中运行这个命令来强制它永远重新启动:

while true; do
    my-main-thing &
    other things which need to happen while the main workload runs in the background
    maybe if you have such things
    wait
done

(还要注意当为true时如何写作。经常会看到一些愚蠢的东西,比如while [true]或while[1],它们碰巧是有用的,但它们的意思可能不是作者想象的那样。)

其他回答

把这个添加到Dockerfile的末尾:

CMD tail -f /dev/null

示例Docker文件:

FROM ubuntu:16.04

# other commands

CMD tail -f /dev/null

参考

我在最后添加了read shell语句。这将保持容器的主进程——启动shell脚本——运行。

您需要使用-d标志来运行它,以使它在后台作为守护进程运行。

Docker运行-d -it ubuntu bash

例如,如果您从容器中检查Dockerfile fballiano / magento2-apache-php

你会看到他在文件末尾添加了以下命令: 而真正的;做睡眠1;完成

现在,我建议你这样做

docker container ls --all | grep 127

然后,您将看到您的docker映像是否有一个错误,如果它以0退出,那么它可能需要这些将永远休眠的命令之一。

我想扩展或者我敢说,改进camposer提到的答案

当你奔跑

docker run -dit ubuntu

您基本上是以交互模式在后台运行容器。

当你通过CTRL+D(最常用的方法)附加并退出容器时,你停止了容器,因为你刚刚杀死了用上面的命令启动容器的主进程。

利用一个已经运行的容器,我只需要fork bash的另一个进程,并通过运行来获得一个伪TTY:

docker exec -it <container ID> /bin/bash