在Alpine Docker容器中运行OpenSSH

时间:2016-02-29 02:22:43

标签: linux shell docker openssh alpine

我已installed OpenSSH,现在我希望按照documentation中的说明运行/etc/init.d/sshd start来运行它。但它没有开始:

 / # /etc/init.d/sshd start
 /bin/ash: /etc/init.d/sshd: not found

思想?

P.S。

/ # ls -la /etc/init.d/sshd 
-rwxr-xr-x    1 root     root          2622 Jan 14 20:48 /etc/init.d/sshd

/etc/init.d/sshd的内容:

    #!/sbin/openrc-run
    # Copyright 1999-2015 Gentoo Foundation
    # Distributed under the terms of the GNU General Public License v2
    # $Header: /var/cvsroot/gentoo-x86/net-misc/openssh/files/sshd.rc6.4,v 1.5 2015/05/04 02:56:25 vapier Exp $

    description="OpenBSD Secure Shell server"
    description_checkconfig="Verify configuration file"
    description_reload="Reload configuration"

    extra_commands="checkconfig"
    extra_started_commands="reload"

    : ${SSHD_CONFDIR:=/etc/ssh}
    : ${SSHD_CONFIG:=${SSHD_CONFDIR}/sshd_config}
    : ${SSHD_PIDFILE:=/var/run/${SVCNAME}.pid}
    : ${SSHD_BINARY:=/usr/sbin/sshd}

    depend() {
        use logger dns
        if [ "${rc_need+set}" = "set" ] ; then
            : # Do nothing, the user has explicitly set rc_need
        else
            local x warn_addr
            for x in $(awk '/^ListenAddress/{ print $2 }' "$SSHD_CONFIG" 2>/dev/null) ; do
                case "${x}" in
                    0.0.0.0|0.0.0.0:*) ;;
                    ::|\[::\]*) ;;
                    *) warn_addr="${warn_addr} ${x}" ;;
                esac
            done
            if [ -n "${warn_addr}" ] ; then
                need net
                ewarn "You are binding an interface in ListenAddress statement in your sshd_config!"
                ewarn "You must add rc_need=\"net.FOO\" to your /etc/conf.d/sshd"
                ewarn "where FOO is the interface(s) providing the following address(es):"
                ewarn "${warn_addr}"
            fi
        fi
    }

    checkconfig() {
        if [ ! -d /var/empty ] ; then
            mkdir -p /var/empty || return 1
        fi

        if [ ! -e "${SSHD_CONFIG}" ] ; then
            eerror "You need an ${SSHD_CONFIG} file to run sshd"
            eerror "There is a sample file in /usr/share/doc/openssh"
            return 1
        fi

        if ! yesno "${SSHD_DISABLE_KEYGEN}"; then
            ssh-keygen -A || return 1
        fi

        [ "${SSHD_PIDFILE}" != "/var/run/sshd.pid" ] \
            && SSHD_OPTS="${SSHD_OPTS} -o PidFile=${SSHD_PIDFILE}"
        [ "${SSHD_CONFIG}" != "/etc/ssh/sshd_config" ] \
            && SSHD_OPTS="${SSHD_OPTS} -f ${SSHD_CONFIG}"

        "${SSHD_BINARY}" -t ${SSHD_OPTS} || return 1
    }

    start() {
        checkconfig || return 1

        ebegin "Starting ${SVCNAME}"
        start-stop-daemon --start --exec "${SSHD_BINARY}" \
            --pidfile "${SSHD_PIDFILE}" \
            -- ${SSHD_OPTS}
        eend $?
    }

    stop() {
        if [ "${RC_CMD}" = "restart" ] ; then
            checkconfig || return 1
        fi

        ebegin "Stopping ${SVCNAME}"
        start-stop-daemon --stop --exec "${SSHD_BINARY}" \
            --pidfile "${SSHD_PIDFILE}" --quiet
        eend $?

        if [ "$RC_RUNLEVEL" = "shutdown" ]; then
            _sshd_pids=$(pgrep "${SSHD_BINARY##*/}")
            if [ -n "$_sshd_pids" ]; then
                ebegin "Shutting down ssh connections"
                kill -TERM $_sshd_pids >/dev/null 2>&1
                eend 0
            fi
        fi
    }

    reload() {
        checkconfig || return 1
        ebegin "Reloading ${SVCNAME}"
        start-stop-daemon --signal HUP \
            --exec "${SSHD_BINARY}" --pidfile "${SSHD_PIDFILE}"
        eend $?
    }

5 个答案:

答案 0 :(得分:9)

容器不是完整安装的环境。 官方文件是针对某些机器安装的高山。 打开电源,启动服务等容器没有的服务。

因此,Section "Uninstall" ${nsProcess::FindProcess} "${name}.exe" $R0 ${If} $R0 == 0 ${nsProcess::KillProcess} "${name}.exe" $R0 ${EndIf} ${nsProcess::Unload} DeleteRegKey HKLM "${uninstkey}" DeleteRegKey HKLM "${regkey}" Delete "$SMPROGRAMS\${name}.lnk" Delete "$DESKTOP\${name}.lnk" ; Remove whole directory from Program Files RMDir /r "$INSTDIR" ; Remove also database files generated by your app if user checked this option ${If} $RemoveAppDataCheckbox_State == ${BST_CHECKED} SetShellVarContext all RMDir /r "${dbFolder}" ${EndIf} SectionEnd 中的任何内容都不能直接用于启动服务使用的容器(如systemd或alpine的rc *)。这就是您收到错误消息的原因,因为容器中未安装/etc/init.d/

你需要做的是开始sshd manuanlly。 您可以查看以下示例:

https://hub.docker.com/r/danielguerra/alpine-sshd/~/dockerfile/

答案 1 :(得分:2)

尽管我仍然不清楚一些细节,但让我在讨论中发言。以下配置指定的解决方案对我有效。这是艰巨实验的结果。

首先, dockerfile

FROM alpine
RUN apk update && \
apk add --no-cache sudo bash openrc openssh
RUN mkdir -p /run/openrc && \
    touch /run/openrc/softlevel && \
    rc-update add sshd default
RUN adduser --disabled-password regusr && \
    sh -c 'echo "regusr:<encoded_passwd>"' | chpasswd -e > /dev/null 2>&1 && \
    sh -c 'echo "regusr ALL=NOPASSWD: ALL"' >> /etc/sudoers
VOLUME ["/home/reguser/solution/entrypoint-init.d","/sys/fs/cgroup"]
USER reguser
WORKDIR /home/reguser
RUN mkdir -p $HOME/solution && sudo chown reguser:reguser $HOME/solution
ADD ./entrypoint.sh /home/reguser/solution/
EXPOSE 22
ENTRYPOINT ["./solution/entrypoint.sh"]
CMD ["/bin/bash"]

接下来, /home/reguser/solution/entrypoint.sh

#!/bin/bash
for f in ./solution/entrypoint-init.d/*; do
    case "$f" in
       *.sh)     echo "$0: running $f"; . "$f" ;;
       *)        echo "$0: ignoring $f" ;;
    esac
    echo
done

exec "$@"

接下来, /home/reguser/solution/entrypoint-init.d/10-ssh-up.sh

#!/bin/bash
sudo sed --in-place --expression='/^#[[:space:]]*Port[[:space:]]\+22$/ s/^#//i' -- /etc/ssh/sshd_config
sudo sed --in-place --expression='/^#[[:space:]]*AddressFamily[[:space:]]\+any$/ s/^#//i' -- /etc/ssh/sshd_config
sudo sed --in-place --expression='/^#[[:space:]]*HostKey[[:space:]]\+\/etc\/ssh\/ssh_host_rsa_key$/ s/^#//i' -- /etc/ssh/sshd_config

sudo sed --in-place --expression='/^#[[:space:]]*HostbasedAuthentication[[:space:]].*/ s/^#//i' -- /etc/ssh/sshd_config
sudo sed --in-place --expression='/^[[:space:]]*HostbasedAuthentication[[:space:]].*/ s/^[[:space:]]*\(HostbasedAuthentication\)[[:space:]]\(.*\)/\1 no/i' -- /etc/ssh/sshd_config
sudo sed --in-place --expression='/^[[:space:]]*HostbasedAuthentication[[:space:]]\+yes.*/ s/^/#/i' -- /etc/ssh/sshd_config

sudo sed --in-place --expression='/^#[[:space:]]*IgnoreRhosts[[:space:]].*/ s/^#//i' -- /etc/ssh/sshd_config
sudo sed --in-place --expression='/^[[:space:]]*IgnoreRhosts[[:space:]].*/ s/^[[:space:]]*\(IgnoreRhosts\)[[:space:]]\(.*\)/\1 yes/i' -- /etc/ssh/sshd_config
sudo sed --in-place --expression='/^[[:space:]]*IgnoreRhosts[[:space:]]\+no.*/ s/^/#/i' -- /etc/ssh/sshd_config

sudo sed --in-place --expression='/^#[[:space:]]*PasswordAuthentication[[:space:]].*/ s/^#//i' -- /etc/ssh/sshd_config
sudo sed --in-place --expression='/^[[:space:]]*PasswordAuthentication[[:space:]].*/ s/^[[:space:]]*\(PasswordAuthentication\)[[:space:]]\(.*\)/\1 yes/i' -- /etc/ssh/sshd_config
sudo sed --in-place --expression='/^[[:space:]]*PasswordAuthentication[[:space:]]\+no.*/ s/^/#/i' -- /etc/ssh/sshd_config

sudo sed --in-place --expression='/^#[[:space:]]*PubkeyAuthentication[[:space:]].*/ s/^#//i' -- /etc/ssh/sshd_config
sudo sed --in-place --expression='/^[[:space:]]*PubkeyAuthentication[[:space:]].*/ s/^[[:space:]]*\(PubkeyAuthentication\)[[:space:]]\(.*\)/\1 yes/i' -- /etc/ssh/sshd_config
sudo sed --in-place --expression='/^[[:space:]]*PubkeyAuthentication[[:space:]]\+no.*/ s/^/#/i' -- /etc/ssh/sshd_config

sudo sed --in-place --expression='/^#[[:space:]]*PrintMotd[[:space:]].*/ s/^#//i' -- /etc/ssh/sshd_config
sudo sed --in-place --expression='/^[[:space:]]*PrintMotd[[:space:]].*/ s/^[[:space:]]*\(PrintMOTD\)[[:space:]]\(.*\)/\1 no/i' -- /etc/ssh/sshd_config
sudo sed --in-place --expression='/^[[:space:]]*PrintMotd[[:space:]]\+yes.*/ s/^/#/i' -- /etc/ssh/sshd_config

sudo sed --in-place --expression='$ a\' --expression='\nAcceptEnv LANG LC_\*' -- /etc/ssh/sshd_config

sudo /etc/init.d/sshd --dry-run start
sudo /etc/init.d/sshd start

最后两行是技巧的核心。特别是sudo /etc/init.d/sshd --dry-run start使解决方案有效。

最后,命令行控件

docker build --tag='dockerRegUser/sshdImg:0.0.1' --file='./dockerfile' .
docker container create --tty \
       --volume $(pwd)/dock/entrypoint-init.d:/home/reguser/solution/entrypoint-init.d:ro \
       --name sshdCnt 'dockerRegUser/sshdImg:0.0.1' tail -f /dev/null
docker start sshdCnt && \
ssh-keygen -f "/home/user/.ssh/known_hosts" -R "$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' sshdCnt)" && \
sleep 5 && \
ssh-copy-id -i ~/.ssh/sshkey reguser@$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' sshdCnt)

我知道,我知道,有很多不必要的构造。该示例还违反了单一服务docker容器原则。但是,在解决方案开发和交付生命周期中有一些阶段和情况可以证明(或至少是诱使)考虑使用sshd或其他openrc控制的服务来扩展容器。

答案 2 :(得分:1)

  

/etc/init.d/sshd:找不到

尝试先运行这些:

apk add --no-cache openrc
rc-update add sshd

答案 3 :(得分:0)

首先检查/usr/bin/usr/sbin中是否存在sshd。

然后,init.d只有在你将其设置为自动启动时才应该使用sshd:

rc-update add sshd
rc-status

答案 4 :(得分:0)

出于特殊原因,我需要sshd。我必须在CI服务器上运行前端(cypress)和后端(django)最终测试。在一个容器中运行它们至少很难,所以我决定使用2个容器。另外,必须有一个入口点将在两个容器中运行测试。因此,我们的想法是,一个容器将运行其测试,而不是通过ssh在另一个容器中运行测试。

在您的情况下,您可能不想完全像我一样做,例如设置空的root密码,空密码。

最好在单独的目录中运行它,因为它会创建文件(id_rsa.pub)。

server.sh

#!/bin/sh -eux
apk add openssh-server
ssh-keygen -A
passwd -d root
mkdir ~/.ssh
while ! [ -e id_rsa.pub ]; do sleep 1; done
cp id_rsa.pub ~/.ssh/authorized_keys
/usr/sbin/sshd -De

client.sh

#!/bin/sh -eux
apk add openssh-client wait4ports
ssh-keygen -f ~/.ssh/id_rsa -N ''
cp ~/.ssh/id_rsa.pub .
wait4ports -s 1 tcp://c1:22
ssh-keyscan -t rsa c1 > ~/.ssh/known_hosts
ssh c1 echo DO SOMETHING
echo done

docker-compose.yml

version: '3'
services:
    server:
        image: alpine:3.12
        command: sh -c 'cd app && ./server.sh'
        volumes:
            - .:/app
    client:
        image: alpine:3.12
        command: sh -c 'cd app && ./client.sh'
        volumes:
            - .:/app
$ docker-compose up -d && docker-compose logs -f

如果您决定再次运行它:

$ rm -f id_rsa.pub && docker-compose down && docker-compose up -d && docker-compose logs -f