1

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)

Thanks!

6
  • I assume it has something to do with what this is discussing, but not familiar enough yet with it all. phusion.github.io/baseimage-docker – JakeJ Jan 17 '19 at 17:51
  • I muddled my way through creating that Dockerfile by looking at a GitHub by a much more advanced Docker user, github.com/danielguerra69, specifically he has a ssh-server repo with a Dockerfile and a script. I took part of the script and put it in the Dockerfile, combining the two. It's likely that I severely mangled the setup, but the confusing thing is that it works and I have no idea what it does. – JakeJ Jan 17 '19 at 19:25
  • Found something about the $ and @ variables for bash: stackoverflow.com/questions/5163144/… – JakeJ Jan 17 '19 at 19:25
  • "$@" is an array-like construct of all positional parameters, {$1, $2, $3 ...}. – JakeJ Jan 17 '19 at 19:26
0

Let's break this down:

  1. You're working with a Dockerfile
  2. You're using the CMD instruction
  3. First, let's read https://docs.docker.com/engine/reference/builder/#cmd
  4. Looking at the format of your usage of CMD, docker refers to that as the 'shell' form
  5. "If you use the shell form of the CMD, then the will execute in /bin/sh -c" - which tells you whatever you supplied will be interpreted by /bin/sh 2.4 In linux, reading the man page for sh: $ man sh and searching for exec you'd find:
 exec [command arg ...]
        Unless command is omitted, the shell process is replaced with the specified program (which must be a real program, not a shell

builtin or function). Any redirections on the exec command are marked as permanent, so that they are not undone when the exec command finishes.

What is not clear here is how this affects the container. To help with that, see the answers to this question: https://stackoverflow.com/q/32255814/831750

  1. Since you have supplied a parameter to exec, eg "$@", it will be interpreted by exec as the 'command' part. So what is "$@"
  2. A simple google search for: "$@" which would likely give you a result such as https://stackoverflow.com/questions/9994295/what-does-mean-in-a-shell-script/9995322 in which it is explained that

$@ is all of the parameters passed to the script

  1. Or from the man pages under the section titled "Special Parameters", with a subsection on @
 @            Expands to the positional parameters, starting from one.  When the expansion occurs within double-quotes, each positional parameter expands as a separate argument.  If there
              are no positional parameters, the expansion of @ generates zero arguments, even when @ is double-quoted.  What this basically means, for example, is if $1 is “abc” and $2 is
              “def ghi”, then "$@" expands to the two arguments:
                    "abc" "def ghi"

I hope that not only gives you an answer to the "$@" question, but to somewhat show you how to go about breaking down a question like this for yourself. Have fun :-D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.