Start sshd automatically with docker container

前端 未结 7 1175
抹茶落季
抹茶落季 2020-12-10 10:33

Given:

  • container based on ubuntu:13.10
  • installed ssh (via apt-get install ssh)

Problem: each when I start container I hav

相关标签:
7条回答
  • 2020-12-10 11:07

    Here is a Dockerfile which installs ssh server and runs it:

    # 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"]
    

    Build with the following command:

    docker build --target ubuntu-with-sshd -t ubuntu-with-sshd .
    

    Then run with:

    docker run -p 2222:22 ubuntu-with-sshd
    

    To connect to container via local port, run: ssh -v localhost -p 2222.

    To check for container IP address, use docker ps and docker inspect.


    Here is example of docker-compose.yml file:

    ---
    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
    

    To run, type:

    docker-compose up --build
    
    0 讨论(0)
提交回复
热议问题