Home > Blockchain >  How can i execute 2 seperate commands in a Dockerfile?
How can i execute 2 seperate commands in a Dockerfile?

Time:03-26

I have a Dockerfile that installs multiple services on a ubuntu baseimage such as npm, nodejs and ssh.
I want be able to ssh into the container and also run a node express application.
It works perfectly to run one of that. But i cant figure out how to start both services!
To run ssh i did:

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

For the node application i clone a git repo and run:

CMD ["node" "app.js"]

Each of that runs perfectly.
But how can i execute both commands?
I tried putting them both in the CMD directive:

CMD ["/usr/sbin/sshd","-D", "node", "app.js"]

I also tried to execute one of them with RUN:

RUN node app.js
CMD ["/usr/sbin/sshd","-D"]

It executes but is than stuck at this point and doesnt continue to compute the image..
How can i execute /usr/sbin/sshd -D (which i need to run ssh) and also node app.js?

Heres the full Dockerfile:

FROM ubuntu:latest

RUN apt update && apt install  openssh-server sudo -y

RUN apt install git -y

RUN apt install nodejs -y

RUN apt install npm -y

RUN npm install express

RUN npm install better-sqlite3

RUN npm install morgan

RUN echo "PermitRootLogin yes">etc/ssh/sshd_config

RUN  echo 'root:root' | chpasswd

RUN git clone https://github.com/mauriceKalevra/Web2-Projekt.git

WORKDIR Web2-Projekt

RUN npm install


RUN service ssh start

EXPOSE 22


#CMD ["/usr/sbin/sshd","-D", "&&", "node", "app.js"lss" ]

CMD ["node", "app.js"]

CodePudding user response:

Two options are available to do this.

Option 1

Use a shell and && to execute two commands.

FROM debian:stretch-slim as builder
CMD touch test.txt && ls

Option 2

Put all commands you want to execute in executabel script entrypoint.sh and run that script in CMD

FROM debian:stretch-slim as builder
COPY entrypoint.sh /entrypoint.sh
CMD ./entrypoint.sh

entrypoint.sh

#!/bin/sh
touch test.txt
ls

EDIT

Please note, that the commands will by default be executed sequentially so the second command will only be executed after the first. If your first process does never terminate, the second one will never start. Use & to execute commands in the background. For more information on how to run commands in parallel or sequentially please see this thread.

  • Related