我有一台安装了Ubuntu系统的机器,它托管着一个Docker容器。 在这个Docker容器中,我正在运行一个Web服务,该服务必须使用Docker主机的/etc/password验证用户密码。
我的想法是从Docker容器中通过SSH登录到Docker主机。 然而,当我在Docker容器中运行ssh命令时,它会提示ssh未找到。 所以,基本上容器中没有安装ssh。 有什么方法可以在容器中安装ssh吗? 是否有任何方式可以实现这种情况?
我有一台安装了Ubuntu系统的机器,它托管着一个Docker容器。 在这个Docker容器中,我正在运行一个Web服务,该服务必须使用Docker主机的/etc/password验证用户密码。
我的想法是从Docker容器中通过SSH登录到Docker主机。 然而,当我在Docker容器中运行ssh命令时,它会提示ssh未找到。 所以,基本上容器中没有安装ssh。 有什么方法可以在容器中安装ssh吗? 是否有任何方式可以实现这种情况?
好的,作为镜像文件的一部分,您只需要安装openssh-server:
sudo apt-get install openssh-server
问题在于传统上,正在运行的 Docker 容器只会运行单个命令。您可以通过使用 supervisord 等工具来解决此问题。Docker 文档中有一个示例:https://docs.docker.com/engine/admin/using_supervisord/
您的 Dockerfile 可能类似于以下内容:
FROM ubuntu:16.04
MAINTAINER examples@docker.com
RUN apt-get update && apt-get install -y openssh-server apache2 supervisor
RUN mkdir -p /var/lock/apache2 /var/run/apache2 /var/run/sshd /var/log/supervisor
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
EXPOSE 22 80
CMD ["/usr/bin/supervisord"]
您的supervisord.conf文件可能类似于以下内容:
[supervisord]
nodaemon=true
[program:sshd]
command=/usr/sbin/sshd -D
[program:apache2]
command=/bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND"