How to SSH into Docker?

前端 未结 5 1094
说谎
说谎 2020-12-02 05:25

I\'d like to create the following infrastructure flow:

5条回答
  •  离开以前
    2020-12-02 05:49

    Create docker image with openssh-server preinstalled:

    Dockerfile

    FROM ubuntu:16.04
    
    RUN apt-get update && apt-get install -y openssh-server
    RUN mkdir /var/run/sshd
    RUN echo 'root:screencast' | chpasswd
    RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
    
    # 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
    
    ENV NOTVISIBLE "in users profile"
    RUN echo "export VISIBLE=now" >> /etc/profile
    
    EXPOSE 22
    CMD ["/usr/sbin/sshd", "-D"]
    

    Build the image using:

    $ docker build -t eg_sshd .
    

    Run a test_sshd container:

    $ docker run -d -P --name test_sshd eg_sshd
    $ docker port test_sshd 22
    
    0.0.0.0:49154
    

    Ssh to your container:

    $ ssh root@192.168.1.2 -p 49154
    # The password is ``screencast``.
    root@f38c87f2a42d:/#
    

    Source: https://docs.docker.com/engine/examples/running_ssh_service/#build-an-eg_sshd-image

提交回复
热议问题