Home > Software engineering >  Running multiple CMD commands at once
Running multiple CMD commands at once

Time:10-14

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"]

CodePudding user response:

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 &&.

CodePudding user response:

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"]
  • Related