I made a Dockerfile with installs for ssh-server, and was initially having problems with it exiting immediately after the command to start the service. I found a workaround, but I don't understand what the part that keeps it open is doing exactly.
Here is the Dockerfile:
#To make an image from this: docker build -t [image name] . #On host, ufw allow [some other port than 22] #Run container: docker run --rm [image_name] -p [other port from above]:22 FROM ubuntu:latest #add a user RUN useradd -s /bin/bash [some username] #give her sudo privileges RUN adduser [some username] sudo RUN echo [some username]:[some password] | chpasswd #install openssh server RUN apt-get update && apt-get install -y openssh-server #create needed directory for ssh-server RUN mkdir -p /var/run/sshd #start ssh server CMD exec "$@" && /usr/sbin/ssh -D # Expose the SSH port EXPOSE 22
The question I have pertains to the CMD statement, and particularly "exec "$@"". Why does that keep the container running, and what exactly is it doing that the ssh server does not when it provides an interface to the server command line?
More info (not sure if this is relevant, but for those curious, the Docker host is running Ubuntu 18.04.10 LTS, Docker is Docker-ce)