Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start multiple processes for a Docker container in a bash script

Tags:

bash

docker

I found very strange behaviour when I build and run docker container. I would like to have container with cassandra and ssh. In my Dockerfile I've got:

RUN echo "deb http://www.apache.org/dist/cassandra/debian 20x main" | sudo tee -a /etc/apt/sources.list
RUN echo "deb-src http://www.apache.org/dist/cassandra/debian 20x main" | sudo tee -a /etc/apt/sources.list
RUN gpg --keyserver pgp.mit.edu --recv-keys 4BD736A82B5C1B00  
RUN apt-key add ~/.gnupg/pubring.gpg  
RUN apt-get update
RUN apt-get -y install cassandra

And then for ssh

RUN apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo '{{ docker_ssh_user }}:{{docker_ssh_password}}' | chpasswd
EXPOSE 22

And I added start script to run everything I want:

USER root
ADD start start
RUN chmod 777 start

CMD ["sh" ,"start"]

And here comes problem. When I have start like this below:

#!/bin/bash
/usr/sbin/sshd -D
/usr/sbin/cassandra -f

SSH is working well. I can do ssh [email protected]. After I log in container I try to run cqlsh to ensure that cassandra is working. But cassandra is not started for some reason and I can't access cqlsh. I've also checked /var/log/cassandra/ but it was empty.

In second scenario I change my start script to this:

#!/bin/bash
/usr/sbin/sshd -D & /usr/sbin/cassandra/ -f

And I again try to connect ssh [email protected] and then when I run cqlsh inside container I have connection to cqlsh.

So I was thinking that ampersand & is doing some voodoo that all works well ? Why I can't run bash staring script with one command below another? Or I'm missing something else??

Thanks for reading && helping.

like image 722
r.piesnikowski Avatar asked Apr 27 '26 02:04

r.piesnikowski


1 Answers

Thanks to my friend linux guru we found the reason of error.

/usr/sbin/sshd -D means that -D : When this option is specified, sshd will not detach and does not become a deamon. This allows easy monitoring of sshd

So in the first script sshd -D was blocking next command to run. In second script I've got & which let sshd -D go background and then cassandra could start. Finally I've got this version of script:

#!/bin/bash
/usr/sbin/sshd
/usr/sbin/cassandra -f
like image 145
r.piesnikowski Avatar answered Apr 30 '26 10:04

r.piesnikowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!