Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

password define for ssh connection into docker container

I would like to connect with SSH into my docker container. For that I created image with this Dockerfile and this command docker build -t test/toto . :

FROM ubuntu:14.04.4
MAINTAINER Darkomen <[email protected]>

# Let the conatiner know that there is no tty
ENV DEBIAN_FRONTEND noninteractive

RUN sudo apt-get -y update

RUN sudo apt-get -y install software-properties-common python-software-properties
RUN sudo add-apt-repository main
RUN sudo add-apt-repository universe
RUN sudo add-apt-repository restricted
RUN sudo add-apt-repository multiverse

RUN sudo apt-get -y update

#RUN sudo apt-get -y install linux-headers-$(uname -r) build-essential
RUN sudo apt-get -y install linux-headers-generic build-essential
RUN apt-get -y install zlib1g-dev libssl-dev libreadline-gplv2-dev
RUN apt-get -y install curl unzip
RUN apt-get -y install software-properties-common
RUN apt-get -y install gnupg2

# others tools
RUN sudo apt-get -y install nano
RUN sudo apt-get -y install vim
RUN sudo apt-get -y install aptitude
RUN sudo apt-get -y install git
RUN sudo apt-get -y install openjdk-7-jdk
RUN sudo apt-get -y install whois
RUN sudo apt-get -y install dos2unix

# SSH
RUN apt-get -y install openssh-server
RUN mkdir -p /var/run/sshd
RUN echo 'root:screencast' |chpasswd

EXPOSE 22
CMD    /usr/sbin/sshd -D

next I launched my container with this image with this command : docker run test/toto -p 42000:22

My container run perfectly and I launch this command for enter into this container : docker run -dt -p 42000:22 test/toto

Now my docker-machine and docker container (based on my dockerfile) run. I can view that because docker ps -a tell me that :

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS                   NAMES
a28ad08fd393        test/toto           "/bin/sh -c '/usr/sbi"   22 minutes ago      Up 22 minutes                 0.0.0.0:42000->22/tcp   admiring_feynman

For connecting in my container I launch this command : ssh [email protected] -p 42000

But it tell me to enter a password. I try lot of thing but nothing run. What I do forget in my process of creation and configuration docker ?

192.168.99.100 is the IP return by the docker-machine inspect command

I also tried to change root password into my container and repeat ssh command access. But nothing change. I can't connecting to container.

technical information:

  • OS : windows 7 Pro
  • Docker version : 1.12.2 build bb80604
  • Docker system : docker toolbox (based on VirtualBox)
  • VirtualBox version : 5.0.14r105127
like image 760
darkomen Avatar asked Oct 30 '16 14:10

darkomen


People also ask

How do I pass a username and password in docker run?

Provide a password using STDIN To run the docker login command non-interactively, you can set the --password-stdin flag to provide a password through STDIN . Using STDIN prevents the password from ending up in the shell's history, or log-files.

How do I password protect a docker container?

There's no way to do this. Docker containers generally don't have "users"; to the extent that they do, they almost never have passwords set; and you don't "log in" to them, you just run a command. directly runs the interactive shell, as root, with no checks.


2 Answers

You need to configure root login for your sshd:

Manual:

vi /etc/ssh/sshd_config

Change PermitRootLogin without-password to:

PermitRootLogin yes

Then:

service ssh restart

Now try again. If everything went fine, you need to change your docker file to make this change in the build steps.

like image 147
Farhad Farahi Avatar answered Oct 08 '22 05:10

Farhad Farahi


Dockerfile script for create and expose a SSH connection into container :

# SSH
RUN apt-get -y install openssh-server
RUN mkdir -p /var/run/sshd

# authorize SSH connection with root account
RUN sed -i '/^#/!s/PermitRootLogin .*/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sudo service ssh restart

# change password root
RUN echo "root:docker"|chpasswd
like image 32
darkomen Avatar answered Oct 08 '22 06:10

darkomen