1 创建 Dockerfile 文件
建好相应目录(个人习惯好看)
mkdir /home/ubuntu
cat Dockerfile
- FROM ubuntu:latest
- MAINTAINER [email protected]
- ##### 使用机器位于国内故而 切换更新源为 163
- RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak && \
- echo "deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse" >/etc/apt/sources.list && \
- echo "deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse " >>/etc/apt/sources.list && \
- echo "deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse" >>/etc/apt/sources.list && \
- echo "deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse" >>/etc/apt/sources.list && \
- echo "deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse" >>/etc/apt/sources.list && \
- echo "deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse" >>/etc/apt/sources.list && \
- echo "deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse" >>/etc/apt/sources.list && \
- echo "deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse" >>/etc/apt/sources.list && \
- echo "deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse" >>/etc/apt/sources.list && \
- echo "deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse" >>/etc/apt/sources.list
- ##### Update system
- RUN apt-get update && apt-get upgrade -y
- ### 命令补全
- RUN apt-get install bash-completion
- RUN echo "if [ -f /etc/bash_completion ]; then" >> /etc/profile && \
- echo ". /etc/bash_completion" >> /etc/profile && \
- echo "fi" >> /etc/profile
- #### install apache ssh supervisor or nginx
- RUN apt-get install -y openssh-server apache2 supervisor curl vim net-tools && \
- mkdir -p /var/run/sshd &&\
- mkdir -p /var/log/supervisor &&\
- mkdir -p /root/.ssh &&\
- touch /root/.ssh/authorized_keys &&\
- echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDLbkQ4YnM3nHdwFhSp5hIxR+9wvprPDCd7t1zKB563WrPr3iM5zemNbOAwW261u9sjH+2D722lcn+d/FcnxLDbqvrp4DkCGad4k+kqt1f+Px8L2NWJn0MFwbshcPFzLUVxZKEQgpLXrStwG9Pf14KWhgzDZIWTHsNz5ipz4KG3tN3iy8QZD84eRnF/ta8UCw8Z58NE9TaQUJ6a6rKB543qmXKsC43U+aThAQwiL/4p75YGQf4DcwS33F58MLcx4U1//gpemOTUY+UmqrQdeBBgSaHrf/4i6RJxEdkKP9C2gPGdBaY5Yuw6ul8lfzEj0G1I7LxP9m9rkZvq60SbOOyZ [email protected]" > /root/.ssh/authorized_keys
- #### copy supervisord.conf
- COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
- EXPOSE 22 80
- CMD ["/usr/bin/supervisord"]
2, supervisord.conf
cat supervisord.conf
- [supervisord]
- nodaemon=true[program:sshd]
- command=/usr/sbin/sshd -D
- ;[program:apache2]
- ;command=/bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND"
- ;autostart=true
- ;autorestart=true
- ;startretries=1
- ;startsecs=1
- ;redirect_stderr=true
- ;stderr_logfile=/var/log/myapache.err.log
- ;stdout_logfile=/var/log/myapache.out.log
- ;killasgroup=true
- ;stopasgroup=true
- [program:nginx]
- ;directory=/
- command=/usr/sbin/nginx -g 'daemon off;'
- stdout_logfile=/var/log/supervisor/%(program_name)s_access.log
- stderr_logfile=/var/log/supervisor/%(program_name)s_err.log
- autostart=true
- autorestart=true
- startsecs=10
3 创建镜像
- docker build -t test/supervisord .
4 启动容器
- 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 等 然后保存该容器,即可达到目的)
进入容器后
- apt-get install software-properties-common
- 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
- apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
- add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/10.3/ubuntu bionic main
- apt update
- apt install mariadb-server
添加 supervisord mysql
- [program:mysql]
- command=/usr/sbin/mysqld
- priority=1
- numprocs=1
- autostart=true
- autorestart=true
- startretries=3
- exitcodes=0
- stopsignal=KILL
- stopwaitsecs=10
- redirect_stderr=true
- stdout_logfile_maxbytes = 1024MB
- stdout_logfile_backups = 10
- stdout_logfile=/var/run/log/mysql.log
保存容器为镜像
- 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
启动更新后的容器
- docker run -p 0.0.0.0:1023:22 -p 0.0.0.0:8001:80 -t -i test/supervisord:V1
添加 Redis
- [program:redis]
- command=/usr/bin/redis-server
- user=root
- autostart=true
- autorestart=true
- startsecs=3
添加 PHP-FPM
- [program:php-fpm]
- command=/usr/sbin/php-fpm7.2 --nodaemonize
- autostart=true
- autorestart=true
参考文档:https://blog.csdn.net/bbwangj/article/details/81059730
https://www.cnblogs.com/feihufeihu/p/10676281.html
https://blog.51cto.com/14134461/2332635
没有评论