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

 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

当前回答

由于映像是linux,因此要检查的一件事是确保容器中使用的任何shell脚本都有unix行结束符。如果它们的末尾有^M,那么它们就是窗口行结束符。修复它们的一种方法是在/usr/local/start-all.sh上使用dos2unix将它们从windows转换为unix。以交互模式运行docker可以帮助解决其他问题。可能是文件名打错了。参见https://en.wikipedia.org/wiki/Newline

其他回答

我的实践是在Dockerfile中启动一个不会立即退出的shell CMD ["sh", "-c", "service ssh start;然后运行docker run -dit image_name。这样(ssh)服务和容器就开始运行了。

每当我想要一个容器在完成脚本执行后,我添加

&& tail -f /dev/null

在命令结束时。所以它应该是:

/usr/local/start-all.sh && tail -f /dev/null

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

来自副本,我在这里没有看到任何答案,解决了将主要工作负载作为后台作业运行的常见反模式,然后想知道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 fballiano / magento2-apache-php

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

现在,我建议你这样做

docker container ls --all | grep 127

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