Start ssh using systemctl inside the docker container

Viewed 4109

I' m a beginner in the Docker; I have pulled a CentOS 7 image from Hub and ran it ;

I need to ssh in to the docker container(CentOS 7) from my host.

Got the docker container's IP using docker inspect container-id

I have installed the following using

 initscripts
 systemd.x86_64                           
 systemd-libs.x86_64                          
 open-ssh
 firewalld
 net-tools

when i tried to start the firewall to open the port for ssh(22)

[root@a6f3e3eb095c ~]# systemctl  start firewall
Failed to get D-Bus connection: Operation not permitted

Also tried,

[root@a6f3e3eb095c ~]# /usr/lib/systemd/systemd --system &
[1] 353
[root@a6f3e3eb095c ~]# systemd 219 running in system mode. (+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN)
Detected virtualization xen.
Detected architecture x86-64.

Welcome to CentOS Linux 7 (Core)!

Set hostname to <a6f3e3eb095c>.
Cannot determine cgroup we are running in: No such file or directory
Failed to allocate manager object: No such file or directory

[1]+  Exit 1                  /usr/lib/systemd/systemd --system

How to start the firewall/ssh inside the docker container ?

4 Answers

inside docker container run following commands :

yum update -y glibc-common

yum install -y sudo passwd openssh-server openssh-clients tar screen crontabs strace telnet perl libpcap bc patch ntp dnsmasq unzip pax which

rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

yum install -y hiera lsyncd sshpass rng-tools

service sshd start; 

sed -i 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config; 

sed -i 's/#UsePAM no/UsePAM no/g' /etc/ssh/sshd_config; 

sed -i 's/#PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config; 

sed -i 's/enabled=0/enabled=1/' /etc/yum.repos.d/CentOS-Base.repo

mkdir -p /root/.ssh/; 

rm -f /var/lib/rpm/.rpm.lock; 

echo "StrictHostKeyChecking=no" > /root/.ssh/config; 

echo "UserKnownHostsFile=/dev/null" >> /root/.ssh/config

echo "root:password" | chpasswd

( or )

Simply you can pull docker image of centos with ssh in docker hub

https://hub.docker.com/search/?isAutomated=0&isOfficial=0&page=1&pullCount=0&q=centos+ssh&starCount=0

https://hub.docker.com/r/kinogmt/centos-ssh/

https://hub.docker.com/r/jdeathe/centos-ssh/

If you are using the Docker CLI, then you can get into the Docker container using the following command

docker exec -it containerId bash 

I am not sure how to ssh into the docker container, but if you want to do basic operation inside the Docker container, you can make use of the above docker command.

Related