Robert
Robert

Reputation: 5

Why SSH connection to docker container is not working?

So i have this Dockerfile:

FROM debian:squeeze

MAINTAINER Name < email : >

# Update the repository sources list

RUN apt-get update

# Install apache, PHP, and supplimentary programs. curl and lynx-cur are for debugging the container.
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install apache2 build-essential php5 mysql-server openssh-server libapache2-mod-php5 php5-mysql php5-gd php-pear php-apc php5-curl curl lynx-cur

# Enable apache mods.
RUN a2enmod php5
RUN a2enmod rewrite

# Manually set up the apache environment variables
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid

EXPOSE 80

# Copy site into place.
ADD www /var/www/site

# Update the default apache site with the config we created.
ADD apache-config.conf /etc/apache2/sites-enabled/000-default.conf

# start mysqld and apache

EXPOSE 3306

RUN mkdir /var/run/sshd
RUN echo 'root:123' | chpasswd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config

EXPOSE 22

CMD bash -c ' (mysqld &); /usr/sbin/apache2ctl -D FOREGROUND;/usr/sbin/sshd -D'

it builds up, no problem,MySQL and Apache start and work fine but the ssh won't work and i don't know why. openssh-server is installed.

i tried starting it up like this:

#startup.sh file
#/bin/bash

sshd

+

ADD ./startup.sh /opt/startup.sh
ENTRYPOINT ["/opt/startup.sh"]

and many other,i'm stuck.

What am i doing wrong?

Upvotes: 0

Views: 2483

Answers (2)

tk_
tk_

Reputation: 17318

Replace below lines of code in the docker file,

RUN mkdir /var/run/sshd
RUN echo 'root:123' | chpasswd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config

Using these codes

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

this works for me.

Note: Use ssh only for debugging purpose, it is not a good practice at all.

Upvotes: 0

Thomasleveil
Thomasleveil

Reputation: 103925

you are starting apache in the foreground, hence the apachectl process will never give back the hand to the shell that started it and thus the /usr/sbin/sshd -D will never be called (unless you kill apache).

The following instruction will start both mysql and apache in the background and then sshd in the foreground:

CMD bash -c ' (mysqld &); /usr/sbin/apache2ctl start;/usr/sbin/sshd -D'

While such a CMD statement is ok for tests I would advise using a different approach for running multiple processes in a single docker container:

Upvotes: 2

Related Questions