Search code examples
dockersshkubernetesopenssh

Automate starting ssh service after running the container


  • I was working on a Dockerfile used to create an Android docker image.
  • After creating the image and running the container , I have checked ssh service using command (service ssh status) and noticed that ssh service is not running.
  • I have tried to put some commands on Dockerfile like :

    • CMD ["/usr/sbin/sshd","-D"]
    • EXPOSE 22
    • RUN service ssh start

But none of these commands is able to run ssh service after running the container.

Can anyone help here please ?


Solution

  • I have run SSH service in a Docker container successfully using the steps in this link.

    Below is the Dockerfile that worked for me:

    FROM ubuntu:16.04
    
    RUN apt-get update && apt-get install -y openssh-server
    RUN mkdir /var/run/sshd
    RUN echo 'root:THEPASSWORDYOUCREATED' | chpasswd
    RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
    
    RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
    
    ENV NOTVISIBLE "in users profile"
    RUN echo "export VISIBLE=now" >> /etc/profile
    
    EXPOSE 22
    CMD ["/usr/sbin/sshd", "-D"]
    

    You could also try the following Docker images on Docker Hub that enable SSH server inside a container: