docker-compose编排启动LNMP
阅读原文时间:2023年07月08日阅读:2

2-1、docker-compose启动php

#生成php镜像
 [root@rocky8 data]#vim /data/dockerfile/ubuntu_php/Dockerfile
 FROM ubuntu_base:v1.0
 LABEL maintainer="www.wang.org" version="ubuntu_php:v1.0"
 ​
 ADD php-7.4.30.tar.gz /usr/local/src/
 ​
 RUN sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
 RUN apt update && \
     apt -y install gcc make libssl-dev libxml2-dev libsqlite3-dev libcurl3-dev libxml2 sqlite3 pkg-config zlib1g-dev libonig-dev libkrb5-dev libssl-dev libbz2-dev libcurl4-openssl-dev libpng-dev libjpeg-dev libfreetype-dev g++ libonig-dev libxslt-dev libzip-dev && \
     cd /usr/local/src/php-7.4.30 && \
     ./configure --prefix=/apps/php74 --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-zlib --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-mbstring --enable-xml --enable-sockets --enable-fpm --enable-maintainer-zts --disable-fileinfo && \
     make && make install && \
     useradd -r -u 888 nginx
 ​
 COPY php.ini /apps/php74/etc/
 COPY www.conf /apps/php74/etc/php-fpm.d/
 COPY php-fpm.conf /apps/php74/etc/
 COPY run_php.sh /
 ​
 CMD ["/run_php.sh"]
 ​
 [root@rocky8 data]#vim /data/dockerfile/ubuntu_php/run_php.sh
 /apps/php74/sbin/php-fpm
 tail -f /etc/hosts
 ​

 #编写docker-compose.yml
 [root@rocky8 php]#vim docker-compose.yml
 version: '3.3'
 services:
   ubuntu_php:
     container_name: php
     image: "ubuntu_php:v1.1"
 ​

2-2、docker-compose启动nginx

 #生成nginx镜像
 [root@rocky8 ubuntu_nginx_base]#vim Dockerfile
 FROM ubuntu_base:v1.0
 LABEL maintainer="www.wang.org" version="ubuntu_nginx_base:v1.0"
 ​
 ADD nginx-1.22.0.tar.gz /usr/local/src/
 ​
 RUN apt update && \
     apt install -y nfs-kernel-server nfs-common gcc openssh-server lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev unzip zip make
 ​
 RUN cd /usr/local/src/nginx-1.22.0 && \
     ./configure --prefix=/apps/nginx && \
     make && make install && \
     ln -s /apps/nginx/sbin/nginx /usr/bin && \
     mkdir /apps/nginx/conf/conf.d && \
     rm -rf /usr/local/src/nginx-1.22.0/*
 ​
 ADD nginx.conf /apps/nginx/conf/nginx.conf
 ​
 RUN groupadd -g 888 -r nginx && \
     useradd -u 888 -g nginx -r -s /sbin/nologin nginx && \
     chown -R nginx.nginx /apps/nginx/
 ​
 EXPOSE 80 443
 ​
 CMD ["/apps/nginx/sbin/nginx","-g","daemon off;"]
 ​
 [root@rocky8 ubuntu_nginx_base]#docker build -t ubuntu_nginx_base:v1.0 .

 [root@rocky8 nginx]#vim docker-compose.yml
 version: '4.4'
 services:
   ubuntu_nginx_base:
     container_name: ubuntu_nginx
     image: "ubuntu_nginx_base:v1.0"
     volumes:
       - '/data/conf.d:/apps/nginx/conf/conf.d'
       - '/data/wordpress:/data/wordpress'
     ports:
       - "80:80"
     restart: always
 ​

2-3、docker-compose启动mysql

 #编写docker-compose.yml
 [root@rocky8 mysql]#vim docker-compose.yml
 ​
 version: '5.5'
 services:
   mysql:
     container_name: mysql
     image: mysql:8.0.30-oracle
     environment:
       - MYSQL_ROOT_PASSWORD=123456
       - MYSQL_DATABASE=wordpress
       - MYSQL_USER=wordpress
       - MYSQL_PASSWORD=123456
     ports:
       - "3306:3306"
     volumes:
       - '/data/mysql:/var/lib/mysql'
 ​

2-4、docker-compose启动多个容器

 #编写docker-compose.yml启动php+nginx+mysql:
 [root@rocky8 docker-compose]#vim docker-compose.yml
 version: '1.0'
 services:
   nginx:
     image: ubuntu_nginx_base:v1.0
     container_name: nginx
     volumes:
       - '/data/conf.d:/apps/nginx/conf/conf.d'
       - '/data/wordpress:/data/wordpress'
     restart: unless-stopped
     ports:
       - "80:80"
       - "443:443"
   db:
     image: mysql:8.0.30-oracle
     container_name: mysql
     restart: unless-stopped
     environment:
       - MYSQL_ROOT_PASSWORD=123456
       - MYSQL_DATABASE=wordpress
       - MYSQL_USER=wordpress
       - MYSQL_PASSWORD=123456
     volumes:
       - '/data/mysql:/var/lib/mysql'
     network_mode: 'container:nginx'
     depends_on:
       - nginx
 ​
   php:
     image: ubuntu_php:v1.1
     container_name: php
     restart: unless-stopped
     network_mode: 'container:nginx'
     volumes:
       - '/data/wordpress:/data/wordpress'
     depends_on:
       - nginx
 ​
 [root@rocky8 docker-compose]#docker ps -a
 CONTAINER ID   IMAGE                    COMMAND                  CREATED         STATUS         PORTS                                                                      NAMES
 5390b2ec1732   ubuntu_php:v1.1          "/run_php.sh"            7 minutes ago   Up 7 minutes                                                                              php
 32252fe6a544   mysql:8.0.30-oracle      "docker-entrypoint.s…"   7 minutes ago   Up 7 minutes                                                                              mysql
 975ecf45a190   ubuntu_nginx_base:v1.0   "/apps/nginx/sbin/ng…"   7 minutes ago   Up 7 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp   nginx
 [root@rocky8 docker-compose]#ss -ntlp
 State  Recv-Q Send-Q Local Address:Port   Peer Address:Port Process                                  
 LISTEN 0      128          0.0.0.0:22          0.0.0.0:*     users:(("sshd",pid=1039,fd=4))          
 LISTEN 0      100        127.0.0.1:25          0.0.0.0:*     users:(("master",pid=1524,fd=16))        
 LISTEN 0      128          0.0.0.0:443         0.0.0.0:*     users:(("docker-proxy",pid=279394,fd=4))
 LISTEN 0      128          0.0.0.0:80          0.0.0.0:*     users:(("docker-proxy",pid=279423,fd=4))
 LISTEN 0      128             [::]:22             [::]:*     users:(("sshd",pid=1039,fd=6))          
 LISTEN 0      100            [::1]:25             [::]:*     users:(("master",pid=1524,fd=17))        
 LISTEN 0      128             [::]:443            [::]:*     users:(("docker-proxy",pid=279401,fd=4))
 LISTEN 0      128             [::]:80             [::]:*     users:(("docker-proxy",pid=279429,fd=4))
 # 注意:端口映射和自定义网络模式冲突,先启动nginx容器,端口映射80和443,别的容器共享nginx容器网络,所以需要加上depends_on(意思依赖nginx容器)。
 # php也需要挂载/data/wordpress卷,因为nginx代理动态页面都需要php处理。