Start sshd automatically with docker container

I think the correct way to do it would follow docker's instructions to dockerizing the ssh service.

And in correlation to the specific question, the following lines added at the end of the dockerfile will achieve what you were looking for:

EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

Dockerize a SSHD service


You can start ssh server when starting your container probably. Something like this:

docker run ubuntu /usr/sbin/sshd -D

Check out this official tutorial.


Here is a Dockerfile which installs ssh server and runs it:

# Build Ubuntu image with base functionality.
FROM ubuntu:focal AS ubuntu-base
ENV DEBIAN_FRONTEND noninteractive
SHELL ["/bin/bash", "-o", "pipefail", "-c"]

# Setup the default user.
RUN useradd -rm -d /home/ubuntu -s /bin/bash -g root -G sudo ubuntu
RUN echo 'ubuntu:ubuntu' | chpasswd
USER ubuntu
WORKDIR /home/ubuntu

# Build image with Python and SSHD.
FROM ubuntu-base AS ubuntu-with-sshd
USER root

# Install required tools.
RUN apt-get -qq update \
    && apt-get -qq --no-install-recommends install vim-tiny=2:8.1.* \
    && apt-get -qq --no-install-recommends install sudo=1.8.* \
    && apt-get -qq --no-install-recommends install python3-pip=20.0.* \
    && apt-get -qq --no-install-recommends install openssh-server=1:8.* \
    && apt-get -qq clean    \
    && rm -rf /var/lib/apt/lists/*

# Configure SSHD.
# 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
RUN mkdir /var/run/sshd
RUN bash -c 'install -m755 <(printf "#!/bin/sh\nexit 0") /usr/sbin/policy-rc.d'
RUN ex +'%s/^#\zeListenAddress/\1/g' -scwq /etc/ssh/sshd_config
RUN ex +'%s/^#\zeHostKey .*ssh_host_.*_key/\1/g' -scwq /etc/ssh/sshd_config
RUN RUNLEVEL=1 dpkg-reconfigure openssh-server
RUN ssh-keygen -A -v
RUN update-rc.d ssh defaults

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

# Generate and configure user keys.
USER ubuntu
RUN ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519
#COPY --chown=ubuntu:root "./files/authorized_keys" /home/ubuntu/.ssh/authorized_keys

# Setup default command and/or parameters.
EXPOSE 22
CMD ["/usr/bin/sudo", "/usr/sbin/sshd", "-D", "-o", "ListenAddress=0.0.0.0"]

Build with the following command:

docker build --target ubuntu-with-sshd -t ubuntu-with-sshd .

Then run with:

docker run -p 2222:22 ubuntu-with-sshd

To connect to container via local port, run: ssh -v localhost -p 2222.

To check for container IP address, use docker ps and docker inspect.


Here is example of docker-compose.yml file:

---
version: '3.4'
services:
  ubuntu-with-sshd:
    image: "ubuntu-with-sshd:latest"
    build:
      context: "."
      target: "ubuntu-with-sshd"
    networks:
      mynet:
        ipv4_address: 172.16.128.2
    ports:
      - "2222:22"
    privileged: true # Required for /usr/sbin/init
networks:
  mynet:
    ipam:
      config:
        - subnet: 172.16.128.0/24

To run, type:

docker-compose up --build

Just try:

ENTRYPOINT service ssh restart && bash

in your dockerfile, it works fun for me!

more details here: How to automatically start a service when running a docker container?

Tags:

Docker

Sshd