Reputation: 356
I want to have a docker image which extends mongo image and have ssh on it. I wrote this lines :
FROM mongo
RUN apt-get update && \
apt-get install -y openssh-server
EXPOSE 22
RUN useradd -s /bin/bash -p $(openssl passwd -1 test) -d /home/nf2/ -m -G sudo test
CMD ["sh", "-c", "service ssh start", "bash"]
This starts only ssh
and not mongo
. If I remove the last line mongod
is executed from the base image.
Any idea to run both commands in the same image ?
Upvotes: 3
Views: 2827
Reputation: 60074
As mentioned by @David, CMD typically run one process so when you override this with service ssh start
it will not run Mongo as it will overide base image CMD
that run Mongo process.
Try to change CMD
to start both processes.
CMD ["sh", "-c", "service ssh start && mongod"]
But you should know in this if service ssh stop due to some reason you container will still keep running and it will die once Mongo process stop. You can verify using below command
docker run -dit --name test --rm abc && docker exec -it test bash -c "service ssh status"
ce30fa23eeb07f1e268008cce7566585ba1f98c0a3054cecb145443f3275a0d4
* sshd is running
Update:
As mongod will only start Mongo process and no init DB will be happened so try to change your command for imitating DB.
FROM mongo
RUN apt-get update && \
apt-get install -y openssh-server
ENV MONGO_INITDB_ROOT_USERNAME=root
ENV MONGO_INITDB_ROOT_PASSWORD=example
RUN useradd -s /bin/bash -p $(openssl passwd -1 test) -d /home/nf2/ -m -G
CMD ["sh", "-c", "service ssh start && docker-entrypoint.sh mongod"]
Upvotes: 3