简体   繁体   中英

dockerfile cmd run multiple command

I have combine shellinabox and redis in a docker image。

my Dockerfile is simply as below:

CMD ['/usr/local/work/scripts/run.sh']

ENTRYPOINT  ["bin/bash"]

EXPOSE 4200/tcp

the run.sh is as below:

#!/bin/bash
cd $REDIS_HOME
src/redis-server
/usr/bin/shellinaboxd -p 4200

I want start the redis server and shellinabox both when docker start. by only the redis is start.

when I start the docker and use ctrl +c the shellinabox begin start.

I know how to use 'supervisor' figure out this. Anyone know how to use shell script both start this two services?

You must never start a service in the background in an entrypoint.sh! The docker-stop command will send a SIGINT only to the PID 1 of a container to allow it shutdown cleanly, ten seconds later it will be a SIGKILL to the remaining processes. Unless entrypoint.sh has some "trap" parts one can easily trash the contained data.

I have learned about those details while working on https://github.com/gdraheim/docker-systemctl-replacement so that it can run as a init-replacement as well. In that mode you just say CMD /usr/bin/systemctl.py and every systemD service that was enabled will have their ExecStart sequences be run. Upon a docker-stop it will get their ExecStop be run for a clean shutdown.

That's the way that I run multiple services in one container.

In docker philosophy, you should have one process per container, and you could use docker-compose to start two containers one for redis, one for shellinaboxd.

But sometimes it's easier or for some other reasons :

In your script shell "src/redis-server" launch redis and stay attached to console, until it's stopped (with ctrl+c by eg) and then launch shellinaboxd.

You can try by replace this line "src/redis-server" with "nohup src/redis-server &", then redis will be launch in fork process, and will let shellinaboxd start.

Solution with docker-compose:

version: '2'
services:
  redis:
    build: ./Dockerfile
    command: /usr/bin/src/redis-server
  shellinabox:
    build: ./Dockerfile
    ports:
      - 4200:4200
    command: /usr/bin/shellinaboxd -p 4200

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM