rhel docker usage - 在容器启动时启动进程

时间:2015-03-09 12:48:47

标签: docker rhel6

所以我有以下场景来创建我的docker镜像和容器。问题是:如何在容器启动时启动我的流程?

1. create image
cat /var/tmp/mod_sm.tar | docker import - mod_sm_39

2. see images
[root@sl1cdi151 etc]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
mod_sm_39           latest              1573470bfa06        2 hours ago         271 MB
mod_site_m0305      latest              c029826a2253        4 days ago          53.8 MB
<none>              <none>              ee67b9aec2d3        4 days ago          163.4 MB
mod_site_soft       latest              0933a386d56c        6 days ago          53.8 MB
mod_site_vm151      latest              4461c32e4772        6 days ago          53.8 MB

3. create  container
docker run -it --net=host  -v /root:/root -v /usr/share/Modules:/usr/share/Modules -v /usr/libexec:/usr/libexec -v /var:/var -v /tmp:/tmp -v /bin:/bin -v /cgroup:/cgroup -v /dev:/dev -v /etc:/etc -v /home:/home -v /lib:/lib -v /lib64:/lib64 -v /sbin:/sbin -v /usr/lib64:/usr/lib64 -v /usr/bin:/usr/bin --name="mod_sm_39_c2" -d mod_sm_39 /bin/bash

4. now in container i go to my application and start the following:
[root@sl1cdi151 ven]# ./service.sh sm_start

5. check if it's up
[root@sl1cdi151 etc]# ps -ef | grep http
root        33     1  0 11:15 ?        00:00:00 ./httpd -k start -f /usr/local/clo/ven/mod_web/httpd/conf/httpd.conf
daemon      34    33  0 11:15 ?        00:00:00 ./httpd -k start -f /usr/local/clo/ven/mod_web/httpd/conf/httpd.conf
daemon      36    33  0 11:15 ?        00:00:00 ./httpd -k start -f /usr/local/clo/ven/mod_web/httpd/conf/httpd.conf
daemon      37    33  0 11:15 ?        00:00:00 ./httpd -k start -f /usr/local/clo/ven/mod_web/httpd/conf/httpd.conf
daemon      39    33  0 11:15 ?        00:00:00 ./httpd -k start -f /usr/local/clo/ven/mod_web/httpd/conf/httpd.conf
daemon      41    33  0 11:15 ?        00:00:00 ./httpd -k start -f /usr/local/clo/ven/mod_web/httpd/conf/httpd.conf
daemon      43    33  0 11:15 ?        00:00:00 ./httpd -k start -f /usr/local/clo/ven/mod_web/httpd/conf/httpd.conf
daemon      45    33  0 11:15 ?        00:00:00 ./httpd -k start -f /usr/local/clo/ven/mod_web/httpd/conf/httpd.conf
daemon      47    33  0 11:15 ?        00:00:00 ./httpd -k start -f /usr/local/clo/ven/mod_web/httpd/conf/httpd.conf
root        80     1  0 13:32 pts/2    00:00:00 grep http

所以我需要在容器启动时将其设置为"./service.sh sm_start"。我该如何实现呢。提前谢谢

1 个答案:

答案 0 :(得分:0)

将命令指定为docker run命令的一部分:

docker run [options] mod_sm_39 /path/to/service.sh sm_start

或将该命令指定为映像的Dockerfile的一部分,以便在没有显式命令的情况下启动容器时它将运行:

# In the mod_sm_39 Dockerfile:
CMD ["/path/to/service.sh", "sm_start"]