鉴于:
apt-get install ssh
)问题:每次启动容器时我都必须手动运行sshd service ssh start
尝试:update-rc.d ssh defaults
,但没有帮助。
问题:如何设置容器以在容器启动期间自动启动sshd服务?
答案 0 :(得分:10)
试试吧:
ENTRYPOINT service ssh restart && bash
在您的dockerfile中,它对我来说很有趣!
更多详细信息:How to automatically start a service when running a docker container?
答案 1 :(得分:7)
这里是Dockerfile
,它安装ssh服务器并运行它:
# Build Ubuntu image with base functionality.
FROM ubuntu:focal AS ubuntu-base
ENV DEBIAN_FRONTEND noninteractive
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
# Setup the default user.
RUN useradd -rm -d /home/ubuntu -s /bin/bash -g root -G sudo ubuntu
RUN echo 'ubuntu:ubuntu' | chpasswd
USER ubuntu
WORKDIR /home/ubuntu
# Build image with Python and SSHD.
FROM ubuntu-base AS ubuntu-with-sshd
USER root
# Install required tools.
RUN apt-get -qq update \
&& apt-get -qq --no-install-recommends install vim-tiny=2:8.1.* \
&& apt-get -qq --no-install-recommends install sudo=1.8.* \
&& apt-get -qq --no-install-recommends install python3-pip=20.0.* \
&& apt-get -qq --no-install-recommends install openssh-server=1:8.* \
&& apt-get -qq clean \
&& rm -rf /var/lib/apt/lists/*
# Configure SSHD.
# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
RUN mkdir /var/run/sshd
RUN bash -c 'install -m755 <(printf "#!/bin/sh\nexit 0") /usr/sbin/policy-rc.d'
RUN ex +'%s/^#\zeListenAddress/\1/g' -scwq /etc/ssh/sshd_config
RUN ex +'%s/^#\zeHostKey .*ssh_host_.*_key/\1/g' -scwq /etc/ssh/sshd_config
RUN RUNLEVEL=1 dpkg-reconfigure openssh-server
RUN ssh-keygen -A -v
RUN update-rc.d ssh defaults
# Configure sudo.
RUN ex +"%s/^%sudo.*$/%sudo ALL=(ALL:ALL) NOPASSWD:ALL/g" -scwq! /etc/sudoers
# Generate and configure user keys.
USER ubuntu
RUN ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519
#COPY --chown=ubuntu:root "./files/authorized_keys" /home/ubuntu/.ssh/authorized_keys
# Setup default command and/or parameters.
EXPOSE 22
CMD ["/usr/bin/sudo", "/usr/sbin/sshd", "-D", "-o", "ListenAddress=0.0.0.0"]
使用以下命令进行构建:
docker build --target ubuntu-with-sshd -t ubuntu-with-sshd .
然后运行:
docker run -p 2222:22 ubuntu-with-sshd
要通过本地端口连接到容器,请运行:ssh -v localhost -p 2222
。
要检查容器IP地址,请使用docker ps
和docker inspect
。
以下是docker-compose.yml
文件的示例:
---
version: '3.4'
services:
ubuntu-with-sshd:
image: "ubuntu-with-sshd:latest"
build:
context: "."
target: "ubuntu-with-sshd"
networks:
mynet:
ipv4_address: 172.16.128.2
ports:
- "2222:22"
privileged: true # Required for /usr/sbin/init
networks:
mynet:
ipam:
config:
- subnet: 172.16.128.0/24
要运行,请输入:
docker-compose up --build
答案 2 :(得分:2)
这是一个很老的问题,但是为了让其他人发现它,我认为正确的处理方法将遵循docker的指令对ssh服务进行docker化。
与特定问题相关,在dockerfile末尾添加的以下几行将实现您正在寻找的内容:
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
答案 3 :(得分:1)
答案 4 :(得分:1)
这就是我所做的:
FROM nginx
# install gosu
# seealso:
# https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
# https://github.com/tianon/gosu/blob/master/INSTALL.md
# https://github.com/tianon/gosu
RUN set -eux; \
apt-get update; \
apt-get install -y gosu; \
rm -rf /var/lib/apt/lists/*; \
# verify that the binary works
gosu nobody true
ENV myenv='default'
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
COPY entrypoint.sh /entrypoint.sh
ENV AIRFLOW_HOME=/usr/local/airflow
RUN mkdir $AIRFLOW_HOME
RUN groupadd --gid 8080 airflow
RUN useradd --uid 8080 --gid 8080 -ms /bin/bash -d $AIRFLOW_HOME airflow
RUN echo 'airflow:mypass' | chpasswd
EXPOSE 22
CMD ["/entrypoint.sh"]
内部entrypoint.sh:
echo "starting ssh as root"
gosu root service ssh start &
#gosu root /usr/sbin/sshd -D &
echo "starting tail user"
exec gosu airflow tail -f /dev/null
答案 5 :(得分:0)
好吧,我使用以下命令来解决
{data1 data2}
答案 6 :(得分:0)
首先登录您的容器并编写如下初始化脚本/bin/init
:
# execute in the container
cat <<EOT >> /bin/init
#!/bin/bash
service ssh start
while true; do sleep 1; done
EOT
然后允许 root
用户通过 ssh 登录:
# execute in the container
echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
退出容器后将容器提交到一个新的镜像:
# execute in the server
docker commit <YOUR_CONTAINER> <ANY_REPO>:<ANY_TAG>
从现在开始,只要你用下面的命令运行你的容器,ssh服务就会自动启动。
# execute in the server
docker run -it -d --name <NAME> <REPO>:<TAG> /bin/init
docker exec -it <NAME> /bin/bash
完成。
答案 7 :(得分:-4)
您可以使用phusion / baseimage-docker
尝试更优雅的方式