docker : do not override base image entrypoint

Viewed 1517

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 ?

2 Answers

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

Docker Machine

It looks like you could use a docker-machine to simulate your needs. From the official documentation:

Docker Machine is a tool that lets you install Docker Engine on virtual hosts, and manage the hosts with docker-machine command.

If I interpret it right, you want to manage your host where mongo container is running. docker-machine enables you to provision a VM with docker-engine installed in it. You can then run a mongo container in this VM.
To access your docker host (VM), you can use docker-machine ssh.
To transfer files to your docker host (VM), you can use docker-machine scp.

Related