0

There are a few posts on how to use Docker + SSH. There are also posts on how to edit files mounted in a docker container, such that editing them won't cause the permissions to become root.

I'm trying to combine the 2 things, so I can SSH into a docker container and edit files without messing up their permissions.

For, using the correct file permissions, I use:

      - /etc/passwd:/etc/passwd:ro
      - /etc/group:/etc/group:ro

in my docker-compose.yml and

docker compose -f commands/dev/docker-compose.yml run \
    --service-ports \
    --user $(id -u) \
    develop \
    bash

so that when I start the docker container, my user is the same user as my local computer. However, this breaks up my SSH setup inside the Docker container:

useradd -rm -d /home/ubuntu -s /bin/bash -g root -G sudo ubuntu
echo 'ubuntu:ubuntu' | chpasswd
# passwd -d ubuntu

apt install -y --no-install-recommends openssh-server vim-tiny sudo

# See: https://stackoverflow.com/questions/22886470/start-sshd-automatically-with-docker-container
sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
mkdir /var/run/sshd
bash -c 'install -m755 <(printf "#!/bin/sh\nexit 0") /usr/sbin/policy-rc.d'
ex +'%s/^#\zeListenAddress/\1/g' -scwq /etc/ssh/sshd_config
ex +'%s/^#\zeHostKey .*ssh_host_.*_key/\1/g' -scwq /etc/ssh/sshd_config
RUNLEVEL=1 dpkg-reconfigure openssh-server
ssh-keygen -A -v
update-rc.d ssh defaults

# Configure sudo
ex +"%s/^%sudo.*$/%sudo ALL=(ALL:ALL) NOPASSWD:ALL/g" -scwq! /etc/sudoers

Here I'm creating a user called ubuntu with password ubuntu for SSH-ing. This lets me SSH in ubuntu@localhost using the password ubuntu.

The issue is that by mounting the /etc/passwd file into my container, I erase the ubuntu user inside the container. This means when I try to ssh in with ssh -p 9002 ubuntu@localhost, the authentication fails (9002 is what I bind port 22 in the container to on the host).

Does anyone have a solution?

Foobar
  • 7,458
  • 16
  • 81
  • 161
  • "how to edit files mounted in a docker container, such that editing them won't cause the permissions to become root" changing the contents of a file and changing permissions are different operations – erik258 Oct 04 '22 at 23:23
  • If I try to edit the (mounted) files inside the container, I'll get a permissions error. If I use `sudo` to edit the files inside the container, they'll be inaccessible from outside the container – Foobar Oct 04 '22 at 23:25

1 Answers1

0

Here's a first pass answer.

I can use:

useradd -rm -d /home/yourusername -s /bin/bash -g root -G sudo yourusername

instead of

useradd -rm -d /home/ubuntu -s /bin/bash -g root -G sudo ubuntu
echo 'ubuntu:ubuntu' | chpasswd

then, I:

  1. Run the ssh server in the container with:
su root
/usr/sbin/sshd -D -o ListenAddress=0.0.0.0 -o PermitRootLogin=yes

I can ssh into the container as root (using the root password "root", which I set with RUN echo 'root:root' | chpasswd in the Dockerfile).

Then, I can do su yourusername, to switch my user. While this works, it is pretty annoying since I need to bake the user name into the Docker container.

Foobar
  • 7,458
  • 16
  • 81
  • 161