ninja_nugget
ninja_nugget

Reputation: 742

Can not ssh into docker container centOS server

I am pretty new to Docker, and I am making a simple centOS docker image that can run ssh processes and can also authenticate using ssh keys. However, when trying to run ssh username@hostname, I get a ssh: connect to host hostname port 22: Connection refused.

Confused, I tried to see if ssh is actually running by typing in ssh service status and I got the following error: System has not been booted with systemd as init system (PID 1). Can't operate. Failed to connect to bus: Host is down

I tried running it with the privilege flag but it didn't do any good. Here is my dockerfile:

FROM centos:latest

RUN yum update -y && yum -y install openssh openssh-server openssh-clients sudo initscripts

# Generate keys
RUN ssh-keygen -q -N "" -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN ssh-keygen -q -N "" -t rsa -f /root/.ssh/id_rsa
RUN cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
EXPOSE 22

Any help is appreciated. Thank you!

Edited: Added EXPOSE 22 to the end of my dockerfile. Still not working.

Also this is what I see when I docker ps. enter image description here

Also my telnet status: enter image description here

Upvotes: 0

Views: 3527

Answers (1)

ninja_nugget
ninja_nugget

Reputation: 742

FROM centos/systemd:latest

RUN yum update -y && yum -y install openssh openssh-server openssh-clients sudo initscripts

# Generate keys
RUN ssh-keygen -q -N "" -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN ssh-keygen -q -N "" -t rsa -f /root/.ssh/id_rsa
RUN cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
EXPOSE 22

As @masseyb suggested, I changed the docker image that I pulled to one that already has systemd running. After that everything is working well!

Upvotes: 1

Related Questions