I run a Dockerfile
FROM php:7.4-cli
RUN apt-get update && apt-get install -y openssh-server
# add default public key to authorized_keys
#COPY ./ssh/insecure_id_rsa.pub /root/insecure_id_rsa.pub
#RUN mkdir -p /root/.ssh \
# && cat /root/insecure_id_rsa.pub >> /root/.ssh/authorized_keys \
# && rm -rf /root/insecure_id_rsa.pub \
#;
RUN mkdir /var/run/sshd
RUN echo 'root:passforroot' | 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
# @see https://docs.docker.com/engine/examples/running_ssh_service/
CMD ["/usr/sbin/sshd", "-D"]
Via a docker-compose file
version: "3.7"
services:
deploy:
build:
context: ./docker-compose/deploy
container_name: backend-laravel-deploy
restart: unless-stopped
working_dir: /var/www/
ports:
- "2222:22"
# reserve a tty - otherwise the container shuts down immediately
# corresponds to the "-i" flag
tty: true
volumes:
- ./:/var/www
networks:
- backend-laravel
$ docker-compose build deploy
$ docker-compose up -d deploy
If I uncomment the lines regarding ssh keys, I can login into container via SSH as root (without password, of course) with them.
But, I would use a password, so I tried the other method (based on official guide on docker site) but when I try to connect to ssh root@localhost -p 2222
it requires for the password that it isn't that wrote on Dockerfile.
How can I change root password for that Dockerfile?
With docker exec -it <container ID> bash
you can explore the contents of the container, just in case something is wrong.
So far, I could see the following:
root@d52693a8ea0c:/etc/ssh# grep "PermitRoot" sshd_config
#PermitRootLogin yes
# the setting of "PermitRootLogin without-password".
It seems the PermitRootLogin flag is commented with #
. I removed also the hashtag in that file through the Dockerfile sed command that you have:
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
And after rebuilding and running I could login with the password in the Dockerfile.
Hope this helps!