Docker supervisord 自启动 SSH Nginx Or Apache2 Mysql Redis

2019年5月22日

1 创建 Dockerfile 文件

建好相应目录(个人习惯好看)
mkdir /home/ubuntu
cat Dockerfile

  1. FROM ubuntu:latest
  2. MAINTAINER [email protected]
  3. ##### 使用机器位于国内故而 切换更新源为 163
  4. RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak && \
  5.     echo "deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse" >/etc/apt/sources.list && \
  6.     echo "deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse " >>/etc/apt/sources.list && \
  7.     echo "deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse" >>/etc/apt/sources.list && \
  8.     echo "deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse" >>/etc/apt/sources.list && \
  9.     echo "deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse" >>/etc/apt/sources.list && \
  10.     echo "deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse" >>/etc/apt/sources.list && \
  11.     echo "deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse" >>/etc/apt/sources.list && \
  12.     echo "deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse" >>/etc/apt/sources.list && \
  13.     echo "deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse" >>/etc/apt/sources.list && \
  14.     echo "deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse" >>/etc/apt/sources.list
  15. ##### Update system
  16. RUN apt-get update && apt-get upgrade -y
  17. ### 命令补全
  18. RUN apt-get install bash-completion
  19. RUN echo "if [ -f /etc/bash_completion ]; then" >> /etc/profile && \
  20.     echo ". /etc/bash_completion" >> /etc/profile && \
  21.     echo "fi" >> /etc/profile
  22. #### install apache ssh supervisor or nginx
  23. RUN apt-get install -y openssh-server apache2 supervisor curl vim net-tools && \
  24. mkdir -p /var/run/sshd &&\
  25. mkdir -p /var/log/supervisor &&\
  26. mkdir -p /root/.ssh &&\
  27. touch /root/.ssh/authorized_keys &&\
  28.     echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDLbkQ4YnM3nHdwFhSp5hIxR+9wvprPDCd7t1zKB563WrPr3iM5zemNbOAwW261u9sjH+2D722lcn+d/FcnxLDbqvrp4DkCGad4k+kqt1f+Px8L2NWJn0MFwbshcPFzLUVxZKEQgpLXrStwG9Pf14KWhgzDZIWTHsNz5ipz4KG3tN3iy8QZD84eRnF/ta8UCw8Z58NE9TaQUJ6a6rKB543qmXKsC43U+aThAQwiL/4p75YGQf4DcwS33F58MLcx4U1//gpemOTUY+UmqrQdeBBgSaHrf/4i6RJxEdkKP9C2gPGdBaY5Yuw6ul8lfzEj0G1I7LxP9m9rkZvq60SbOOyZ [email protected]" > /root/.ssh/authorized_keys
  29. #### copy supervisord.conf
  30. COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
  31. EXPOSE 22 80
  32. CMD ["/usr/bin/supervisord"]

2, supervisord.conf

cat supervisord.conf

  1. [supervisord]
  2. nodaemon=true[program:sshd]
  3. command=/usr/sbin/sshd -D
  4. ;[program:apache2]
  5. ;command=/bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND"
  6. ;autostart=true
  7. ;autorestart=true
  8. ;startretries=1
  9. ;startsecs=1
  10. ;redirect_stderr=true
  11. ;stderr_logfile=/var/log/myapache.err.log
  12. ;stdout_logfile=/var/log/myapache.out.log
  13. ;killasgroup=true
  14. ;stopasgroup=true
  15. [program:nginx]
  16. ;directory=/
  17. command=/usr/sbin/nginx -g 'daemon off;'
  18. stdout_logfile=/var/log/supervisor/%(program_name)s_access.log
  19. stderr_logfile=/var/log/supervisor/%(program_name)s_err.log
  20. autostart=true
  21. autorestart=true
  22. startsecs=10

3 创建镜像

  1. docker build -t test/supervisord .

4 启动容器

  1. docker run -p 0.0.0.0:1022:22 -p 0.0.0.0:8000:80 -t -i test/supervisord:latest

注:apache 刚启动的容器 supervisor 启动apache 报错 , 需要进入 service apache2 start , service apache2 stop 然后 supervisor 才能管控(原因暂不了解)

5 进入容器

docker-enter 容器ID

6  Docker  supervisord  LNMP Or  LAMP OR REDIS  ...  (安装php mysql 等会出现需要选择or 输入的地方 如果写到 Dockerfile 中,在创建的过程中会卡住造成无法成功创建镜像,所以我在上述完成的容器中安装 php mysql 等 然后保存该容器,即可达到目的)

进入容器后 

  1. apt-get install software-properties-common
  2. apt-get  install  -y  php7.2 php7.2-cli  php7.2-curl  php7.2-gd  php7.2-dev php7.2-json php7.2-mysql php7.2-xml  php7.2-fpm  php7.2-mbstring  php7.2-zip php7.2-bcmath php7.2-ldap
  3. apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
  4. add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/10.3/ubuntu bionic main
  5. apt update
  6. apt install mariadb-server

添加 supervisord mysql

  1. [program:mysql]
  2. command=/usr/sbin/mysqld
  3. priority=1
  4. numprocs=1
  5. autostart=true
  6. autorestart=true
  7. startretries=3
  8. exitcodes=0
  9. stopsignal=KILL
  10. stopwaitsecs=10
  11. redirect_stderr=true
  12. stdout_logfile_maxbytes = 1024MB
  13. stdout_logfile_backups  = 10
  14. stdout_logfile=/var/run/log/mysql.log

保存容器为镜像

  1. docker  commit -a "zhongzuzhu" -m "Add mysql" 21a24bd3349f  test/supervisord:V1

docker images

REPOSITORY                                             TAG                 IMAGE ID            CREATED             SIZE
test/supervisord                                       V1                  705512e0fd8c        13 seconds ago      960MB
test/supervisord                                       latest              dcf6c6a082da        2 days ago          383MB

 

启动更新后的容器

  1. docker run -p 0.0.0.0:1023:22 -p 0.0.0.0:8001:80 -t -i test/supervisord:V1

 

添加 Redis

  1. [program:redis]
  2. command=/usr/bin/redis-server
  3. user=root
  4. autostart=true
  5. autorestart=true
  6. startsecs=3

添加 PHP-FPM

  1. [program:php-fpm]
  2. command=/usr/sbin/php-fpm7.2 --nodaemonize
  3. autostart=true
  4. autorestart=true

参考文档:https://blog.csdn.net/bbwangj/article/details/81059730

                 https://www.cnblogs.com/feihufeihu/p/10676281.html

                 https://blog.51cto.com/14134461/2332635

 

没有评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注