PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, May 14, 2022

[FIXED] How can i execute 2 seperate commands in a Dockerfile?

 May 14, 2022     docker, docker-compose, dockerfile, node.js, ubuntu     No comments   

Issue

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

Solution

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



Answered By - Mushroomator
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing