docker集装箱不能使用“服务sshd重启”

我正在尝试构build一个hadoop Dockerfile

在构build过程中,我补充道:

  && apt install -y openssh-client \ && apt install -y openssh-server \ && ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa \ && cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys \ && chmod 0600 ~/.ssh/authorized_keys && sed -i '/\#AuthorizedKeysFile/ d' /etc/ssh/sshd_config \ && echo "AuthorizedKeysFile ~/.ssh/authorized_keys" >> /etc/ssh/sshd_config \ && /etc/init.d/ssh restart 

我假设当我运行这个容器时:

 docker run -it --rm hadoop/tag bash 

我将能够:

 ssh localhost 

但是我得到一个错误:

ssh:连接到主机localhost端口22:连接被拒绝

如果我在容器内手动运行:

 /etc/init.d/ssh restart # or this service ssh restart 

然后我可以连接。 我想这意味着sshd重新启动不起作用

我在Dockerfile使用FROM java

构build过程只构build一个图像。 当前运行的进程(使用RUN )在构build之后不再运行,并且在使用映像启动容器时不会再次启动。

你需要做的是让sshd 在容器运行时启动。 最简单的方法是使用入口点脚本。

Dockerfile:

 COPY entrypoint.sh /entrypoint.sh RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] CMD ["whatever", "your", "command", "is"] 

entrypoint.sh:

 #!/bin/sh # Start the ssh server /etc/init.d/ssh restart # Execute the CMD exec "$@" 

使用上面的重build图像,当你使用它来启动一个容器时,它应该在运行你的CMD之前启动sshd。

如果您愿意,也可以将开始的基本图像更改为像Phusion baseimage之类的东西。 它可以很容易地启动一些服务,如syslogd,sshd,你可能希望容器运行。