skip to Main Content

Wondering what I may be doing wrong, currently trying to revise this CMD to the proper format but it’s not running right. The original w/ no edit is running good, but using the array version is not. Does combining commands not work in the proper format, or what may I be missing? Modified version when run immediately exits once it starts

Original:

CMD sshd & cd /app && npm start

Modified:

CMD ["sshd", "&", "cd", "/app", "&&", "npm", "start"]

My complete dockerfile:

FROM node:10-alpine

WORKDIR /app

COPY . /app

RUN npm install && npm cache clean --force

# CMD sshd & cd /app && npm start
# CMD ["sshd", "&", "cd", "/app", "&&", "npm", "start"]

2

Answers


  1. Define a script and put all of your commands into this script.
    Eg: I call this script is startup.sh

    #!/bin/bash
    sshd
    cd /app
    npm start
    

    And call this script in CMD

    COPY startup.sh /app/data/startup.sh
    CMD ["/app/data/startup.sh"]
    
    Login or Signup to reply.
  2. You should:

    1. Delete sshd: it’s not installed in your image, it’s unnecessary, and it’s all but impossible to set up securely.
    2. Delete the cd part, since the WORKDIR declaration above this has already switched into that directory.
    3. Then your CMD is just a simple command.
    FROM node:10-alpine
    # note, no sshd, user accounts, host keys, ...
    WORKDIR /app         # does the same thing as `cd /app`
    COPY . /app
    RUN npm install && npm cache clean --force
    CMD ["npm", "start"]
    

    If you want to run multiple commands or attempt to launch an unmanaged background process, all of these things require a shell to run and you can’t usefully use the CMD exec form. In the form you show in the question the main command is sshd only, and it takes 6 arguments including the literal strings & and &&.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search