1、基于 Dockerfile 构建 Nginx-CentOS7.9 镜像

[root@centos7 ~]# docker pull centos:7

[root@centos7 ~]# curl -o ./CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2523  100  2523    0     0   6735      0 --:--:-- --:--:-- --:--:--  6728

[root@centos7 ~]# ls
anaconda-ks.cfg  CentOS-Base.repo  Dockerfile

[root@centos7 ~]# vim Dockerfile 
FROM centos:7
LABEL MAINTAINER="zhouhao"
COPY CentOS-Base.repo /etc/yum.repos.d/
RUN yum install -y gcc gcc-c++ make \
    openssl-devel pcre-devel gd-devel \
    iproute net-tools telnet wget curl && \
    yum clean all && \
    rm -rf /var/cache/yum/*
RUN wget http://nginx.org/download/nginx-1.15.5.tar.gz && \
    tar zxf nginx-1.15.5.tar.gz && \
    cd nginx-1.15.5 &&\
    ./configure --prefix=/usr/local/nginx \
    --with-http_ssl_module \
    --with-http_stub_status_module && \
    make -j 4 && make install && \
    rm -rf /usr/local/nginx/html/* && \
    echo "ok" >> /usr/local/nginx/html/status.html && \
    cd / && rm -rf nginx-1.12.2* && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ENV PATH $PATH:/usr/local/nginx/sbin
WORKDIR /usr/local/nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

[root@centos7 ~]# docker build -t nginx:v1 .

[root@centos7 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
nginx        v1        f8006ac5cb26   12 seconds ago   400MB

[root@centos7 ~]# docker run -d -p 80:80 --name web1 nginx:v1 
e9d21f72cde662c94576de75ed93c083839cc6f5cd96b91cad1153ef92fe52f4

[root@centos7 ~]# docker ps 
CONTAINER ID   IMAGE      COMMAND                   CREATED              STATUS              PORTS                               NAMES
e9d21f72cde6   nginx:v1   "nginx -g 'daemon of…"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp, :::80->80/tcp   web1

[root@centos7 ~]# curl localhost/status.html
ok

2、基于 Dockerfile 构建 PHP71-CentOS7.9 镜像

[root@centos7 ~]# wget http://cn2.php.net/distributions/php-7.1.5.tar.gz --no-check-certific

[root@centos7 php]# vim Dockerfile 
FROM centos:7
LABEL MAINTAINER="zhouhao"
RUN yum install -y install epel-release && \
    yum -y install git wget lrzsz vim  libxml2 libxml2-devel openssl openssl-devel curl curl-devel libjpeg-turbo libjpeg-turbo-devel libpng-devel libpng freetype-devel freetype icu libicu-devel libicu libmcrypt libmcrypt-devel libxslt libxslt-devel php-mysql && \
    yum -y groupinstall "Development Tools" && \
    yum provides "*/applydeltarpm" && \ 
    yum install deltarpm -y && \   
    yum clean all && \
    groupadd www && \
    useradd -g www www   
ADD php-7.1.5.tar.gz   /usr/local/src/
RUN cd /usr/local/src/php-7.1.5 && \
    ./configure --prefix=/usr/local/php71 \
    --with-config-file-path=/usr/local/php71/etc \
    --with-config-file-scan-dir=/usr/local/php71/conf.d \
    --enable-fpm --with-fpm-user=www \
    --with-fpm-group=www \
    --with-mysql=mysqlnd \
    --with-mysqli=mysqlnd \
    --with-pdo-mysql=mysqlnd \
    --with-iconv-dir \
    --with-freetype-dir=/usr/local/freetype \
    --with-jpeg-dir \
    --with-png-dir \
    --with-zlib \
    --with-libxml-dir=/usr \
    --enable-xml \
    --disable-rpath \
    --enable-bcmath \
    --enable-shmop \
    --enable-sysvsem \
    --enable-inline-optimization \
    --with-curl \
    --enable-mbregex \
    --enable-mbstring \
    --with-mcrypt \ 
    --enable-ftp \
    --with-gd \
    --enable-gd-native-ttf \
    --with-openssl \
    --with-mhash \
    --enable-pcntl \
    --enable-sockets \
    --with-xmlrpc \
    --enable-zip \
    --enable-soap \
    --with-gettext \
    --disable-fileinfo \
    --enable-opcache \
    --enable-intl \
    --with-xsl && \
    make -j 4 && make install && \
    cp /usr/local/php71/etc/php-fpm.conf.default /usr/local/php71/etc/php-fpm.conf && \
    cp ./php.ini-production  /usr/local/php71/etc/php.ini && \
    cp /usr/local/php71/etc/php-fpm.d/www.conf.default /usr/local/php71/etc/php-fpm.d/www.conf && \
    cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && \
    chmod +x /etc/init.d/php-fpm && \
    rm -rf /usr/loacl/src/php-7.1.5
EXPOSE 9000
CMD ["/etc/init.d/php-fpm","start"]

[root@centos7 php]# docker build -t centos7-php71:base .

[root@centos7 php]# docker run -d -p 9000:9000 centos7-php71:base tail -f /etc/hosts

[root@centos7 php]# docker ps
CONTAINER ID   IMAGE                COMMAND                CREATED          STATUS          PORTS                                       NAMES
7a8575b82c91   centos7-php71:base   "tail -f /etc/hosts"   27 seconds ago   Up 25 seconds   0.0.0.0:9000->9000/tcp, :::9000->9000/tcp   brave_mirzakhani

[root@centos7 php]# ss -ntl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port            
LISTEN      0      128      *:9000                 *:*                
LISTEN      0      128      *:22                   *:*                
LISTEN      0      100    127.0.0.1:25                   *:*                
LISTEN      0      128       [::]:9000                  [::]:*                
LISTEN      0      128       [::]:22                    [::]:*                
LISTEN      0      100      [::1]:25                    [::]:*

[root@centos7 php]# docker exec -it 7 bash

[root@7a8575b82c91 /]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

[root@7a8575b82c91 /]# yum -y install httpd net-tools

[root@7a8575b82c91 /]# cd /var/www/html/

[root@7a8575b82c91 html]# vim index.php
<?php
phpinfo();
?>

[root@7a8575b82c91 html]# httpd

[root@7a8575b82c91 html]# netstat -tulnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name  
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      104/httpd

[root@7a8575b82c91 html]# curl -I localhost/index.php
HTTP/1.1 200 OK
Date: Fri, 01 Mar 2024 14:52:22 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Fri, 01 Mar 2024 14:50:23 GMT
ETag: "14-6129a7eebdd52"
Accept-Ranges: bytes
Content-Length: 20

[root@centos7 ~]# docker commit 7a8575b82c91 centos79-php7-httpd:v1 
sha256:9b873e344c15007e69508371ac12762d0ed5078cca3fa512476a6881cac92f81

[root@centos7 ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
centos79-php7-httpd   v1        9b873e344c15   11 seconds ago   1.61GB

[root@centos7 ~]# docker run -d -p 80:80 -p 9000:9000 centos79-php7-httpd:v1 tail -f /etc/hosts
5c150849cc6337472b1e955055a1ecbaf09a8642cdb78438dfca7fb39492906e

[root@centos7 ~]# docker exec -it 5c bash

[root@5c150849cc63 /]# vim /usr/local/php71/etc/php-fpm.d/www.conf
listen = 0.0.0.0:9000

[root@5c150849cc63 /]# /etc/init.d/php-fpm start
Starting php-fpm  done

[root@5c150849cc63 /]# httpd

[root@5c150849cc63 /]# netstat -tulnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name  
tcp        0      0 0.0.0.0:9000            0.0.0.0:*               LISTEN      51/php-fpm: master  
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      25/httpd

#解决浏览器访问php是在下载文件的问题
[root@5c150849cc63 /]#yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
[root@5c150849cc63 /]#yum-config-manager --enable remi-php71
[root@5c150849cc63 /]#yum -y install php
[root@5c150849cc63 /]#cd /etc/httpd/modules/
[root@5c150849cc63 modules]# ls libphp7*  
libphp7-zts.so  libphp7.so

[root@5c150849cc63 ~]# vim /etc/httpd/conf/httpd.conf 
#新增下面2行内容
LoadModule php7_module modules/libphp7.so
AddType application/x-httpd-php .php

#修改成如下
<IfModule dir_module>
    DirectoryIndex index.php
</IfModule>

<Files ".ht*">
    Require all granted
</Files>

#退出此容器后,重启容器,然后进入容器启动httpd和php7服务
[root@centos7 ~]# docker restart 5c150849cc63

[root@centos7 ~]# docker exec -it 5c bash

[root@5c150849cc63 /]# httpd

[root@5c150849cc63 /]# /etc/init.d/php-fpm start
Starting php-fpm  done

[root@5c150849cc63 /]# netstat -tulnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name  
tcp        0      0 0.0.0.0:9000            0.0.0.0:*               LISTEN      36/php-fpm: master  
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      27/httpd

#浏览器访问测试

image-20240302133528996

#基于容器构建新镜像
[root@centos7 ~]# docker commit 5c150849cc63 centos79-php71-httpd:v2
sha256:11bb2503da67cd6e458b4a38e0fff1fb80b2c58fd9591267c8af73944f8bd68a

#基于新镜像启动成容器,访问测试,容器此时启动后里面的httpd和php并未启动需手动登入容器启动,解决此问题可基于此镜像进入到容器写一个脚本在起来容器时候进行执行
[root@centos7 ~]# docker images
REPOSITORY             TAG       IMAGE ID       CREATED          SIZE
centos79-php71-httpd   v2        11bb2503da67   4 minutes ago    1.96GB

[root@centos7 ~]# docker run -d -p 80:80 centos79-php71-httpd:v2
[root@cf4320f79f79 /]# vim run_httpd_php.sh
#!/bin/bash
httpd
/etc/init.d/php-fpm start
tail -f /etc/hosts

[root@centos7 ~]# docker commit cf4320f79f79 centos79-php71-httpd:v3

[root@centos7 ~]# docker run -d -p 80:80 centos79-php71-httpd:v3 bash -x /run_httpd_php.sh

[root@centos7 ~]# docker ps
CONTAINER ID   IMAGE                     COMMAND                   CREATED        STATUS                  PORTS                                         NAMES
5313c1108063   centos79-php71-httpd:v3   "bash -x /run_httpd_…"   1 second ago   Up Less than a second   0.0.0.0:80->80/tcp, :::80->80/tcp, 9000/tcp   crazy_bose

image-20240302135826812

3、基于 Dockerfile 构建 Apache2+PHP7.4-Debian11 镜像

[root@centos7 apache2+php7]# vim Dockerfile 
FROM debian:latest
# 换源提速
RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian bullseye main" > /etc/apt/sources.list && \
    echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main" >> /etc/apt/sources.list  && \
    echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian bullseye-updates main" >> /etc/apt/sources.list
RUN apt-get update
RUN apt-get install apache2 libapache2-mod-php php-mysql php-gd php-curl php-mbstring php  php-common php-fpm  php-horde-util php-mdb2 php-mysql php-net-url2 php-pear  php-xml php-xml-htmlsax3  php-xml-svg php-xmlrpc  php-cli php-common php-curl php-fpm php-gd php-json php-mbstring php-readline php-xml php-xmlrpc -y
#追加配置
RUN sh -c "echo 'ServerName localhost:80' >> /etc/apache2/apache2.conf"
#地址重写功能(实现子域名功能)
RUN a2enmod rewrite
#禁用模块(不禁用将无法启用PHP7.4)
RUN a2dismod mpm_event
#支持PHP解析
RUN a2enmod php7.4
# 启动apache2
#清理
RUN apt-get clean
#对外暴漏默认端口,即不指定但默认提供
EXPOSE 80 443
CMD ["/usr/sbin/apache2ctl","-D","FOREGROUND"]

[root@centos7 apache2+php7]# docker build -t apache2-php7-debian:v1 .

[root@centos7 ~]# docker run -d -p 81:80 -v /data/html:/var/www/html --name apache2_PHP7.4 apache2-php7-debian:v1

[root@centos7 ~]# echo hello > /data/html/index.html

[root@centos7 ~]# curl localhost:81
hello

[root@centos7 ~]# vim /data/html/index.php 
<?php
phpinfo();
?>

image-20240302131408356

image-20240302131426968

4、基于 Dockerfile 构建 Nginx124+PHP5-CentOS79 镜像

[root@centos7 nginx+php]# vim nginx.repo 
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

[root@centos7 nginx+php]# vim default.conf 
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

[root@centos7 nginx+php]# vim nginx.ini 
[program:nginx]
command=/usr/sbin/nginx -g "daemon off;"
[program:php-fpm]
command=/usr/sbin/php-fpm -F

[root@centos7 nginx+php]# vim Dockerfile 
FROM centos:7
COPY nginx.repo /etc/yum.repos.d/nginx.repo
RUN yum -y install nginx epel-release php php-fpm && yum -y install supervisor
COPY default.conf /etc/nginx/conf.d/default.conf
COPY nginx.ini /etc/supervisord.d/nginx.ini
RUN echo "<?php phpinfo(); ?>" >/usr/share/nginx/html/index.php
CMD ["supervisord","-n"]

[root@centos7 nginx+php]# docker run --name nginx-php -d -p 80:80 nginx-php5:v1 

[root@centos7 nginx+php]# docker ps
CONTAINER ID   IMAGE           COMMAND            CREATED         STATUS         PORTS                               NAMES
c4355c95e480   nginx-php5:v1   "supervisord -n"   2 minutes ago   Up 2 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx-php

image-20240302141131021

5、基于 Dockerfile 构建 LNMP 镜像

#nginx下载地址:https://nginx.org/download/nginx-1.22.1.tar.gz
#wordpress下载地址:https://cn.wordpress.org/wordpress-6.0.2-zh_CN.tar.gz

[root@centos7 ~]# mkdir -p lnmp/{mysql,nginx,php}
[root@centos7 ~]# cd lnmp/nginx/

1) 部署Nginx

[root@centos7 nginx]# vim Dockerfile
FROM centos:7
LABEL MAINTAINER="zhouhao"
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
ADD nginx-1.22.1.tar.gz /opt
WORKDIR /opt/nginx-1.22.1
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
ENV PATH=/usr/local/nginx/sbin:$PATH
ADD nginx.conf /usr/local/nginx/conf/nginx.conf
ADD wordpress-6.0.2-zh_CN.tar.gz /usr/local/nginx/html
RUN chmod 777 -R /usr/local/nginx/html
EXPOSE 80
VOLUME ["/usr/local/nginx/html/"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]

[root@centos7 nginx]# vim nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        charset utf-8;
        location / {
            root   html;
            index  index.html index.php;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {         #当访问以.php结尾的URL时,会连接到172.17.0.20的ip地址上
            root           html;
            fastcgi_pass   172.18.0.20:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

[root@centos7 nginx]# ls
Dockerfile  nginx-1.22.1.tar.gz  nginx.conf  wordpress-6.0.2-zh_CN.tar.gz

[root@centos7 nginx]# docker build -t nginx:lnmp .

#启动访问测试
[root@centos7 php]# docker network create mynetwork
[root@centos7 php]# docker run -d --name nginx --net mynetwork --ip 172.18.0.10 -p 80:80 nginx:lnmp

注:
root@ubuntu2204:~# docker -v
Docker version 24.0.5, build 24.0.5-0ubuntu1~22.04.1

root@ubuntu2204:~# docker network create --subnet 172.18.0.0/24 --gateway 172.18.0.1 mynetwork 

2) 部署mysql

[root@centos7 ~]# cd lnmp/mysql/
[root@centos7 mysql]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.25.tar.gz

[root@centos7 mysql]# ls
mysql-boost-5.7.25.tar.gz

[root@centos7 mysql]# vim Dockerfile 
FROM centos:7
LABEL MAINTAINER="zhouhao"
RUN yum -y install ncurses ncurses-devel bison cmake pcre-devel zlib-devel gcc gcc-c++ make
#创建mysql用户
RUN useradd -M -s /sbin/nologin mysql
#将mysql安装包上传(自带boost)
ADD mysql-boost-5.7.25.tar.gz /opt
#切换到mysql解压后的目录
WORKDIR /opt/mysql-5.7.25
 
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8  \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && make -j4 && make install
 
#开放端口
EXPOSE 3306
ADD my.cnf /etc/my.cnf 
#设置权限
RUN chown -R mysql:mysql /usr/local/mysql/ && chown mysql:mysql /etc/my.cnf
#设置环境变量
ENV PATH /usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
#切换目录
WORKDIR /usr/local/mysql/bin
#初始化mysql
RUN ./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
#复制mysql服务程序到启动文件中
RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
#设置共享目录
VOLUME ["/usr/local/mysql"]
#启动容器之后,可以使用systemctl工具(并且占用前台,保持容器不断)
CMD ["/usr/sbin/init"]


[root@centos7 mysql]# vim my.cnf 
[client]								
port = 3306
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock
 
[mysql]								
port = 3306
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock
auto-rehash
 
[mysqld]
user = mysql 
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

[root@centos7 mysql]# docker build -t mysql:lnmp .

[root@centos7 mysql]# docker run --name mysql -d --privileged -v /usr/local/mysql --net mynetwork --ip 172.18.0.20 mysql:lnmp

[root@centos7 ~]# docker ps
CONTAINER ID   IMAGE        COMMAND            CREATED         STATUS         PORTS      NAMES
7edf27a51ec8   mysql:lnmp   "/usr/sbin/init"   2 minutes ago   Up 2 minutes   3306/tcp   mysq

[root@centos7 ~]# docker exec -it 7 bash

3) 部署php

[root@centos7 ~]# cd lnmp/php/

[root@centos7 php]# ls
Dockerfile  php-7.1.5.tar.gz

[root@centos7 php]# vim Dockerfile
FROM centos:7
LABEL MAINTAINER="zhouhao"
RUN yum install -y gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel \
gcc gcc-c++ make pcre-devel;useradd -M -s /sbin/nologin nginx
ADD php-7.1.5.tar.gz /opt
WORKDIR /opt/php-7.1.5
RUN ./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip && make -j 4 && make install
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
ADD php.ini /usr/local/php/lib
ADD php-fpm.conf /usr/local/php/etc
ADD www.conf /usr/local/php/etc/php-fpm.d/
#暴露端口
EXPOSE 9000
#启动一个进程,占用前台
CMD /usr/local/php/sbin/php-fpm -F


[root@centos7 php]# vim php.ini 
[PHP]
engine = On
short_open_tag = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = -1
disable_functions =
disable_classes =
zend.enable_gc = On
expose_php = On
max_execution_time = 30
max_input_time = 60
memory_limit = 128M
error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = On
html_errors = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 8M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
default_charset = "UTF-8"
doc_root =
user_dir =
enable_dl = Off
file_uploads = On
upload_max_filesize = 2M
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
[CLI Server]
cli_server.color = On
[Date]
date.timezone = Asia/Shanghai
[filter]
[iconv]
[intl]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.cache_size = 2000
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = On
[SQL]
sql.safe_mode = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[Interbase]
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M:%S"
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.cache_size = 2000
mysqli.default_port = 3306
mysqli.default_socket = /usr/local/mysql/mysql.sock
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = On
[OCI8]
[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.sid_length = 26
session.trans_sid_tags = "a=href,area=href,frame=src,form="
session.sid_bits_per_character = 5
[Assertion]
zend.assertions = 1
[COM]
[mbstring]
[gd]
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[mcrypt]
[dba]
[opcache]
[curl]
[openssl]


[root@centos7 php]# vim php-fpm.conf
[global]
pid = run/php-fpm.pid
include=/usr/local/php/etc/php-fpm.d/*.conf


[root@centos7 php]# vim www.conf 
[www]
user = nginx
group = nginx
listen = 172.18.0.30:9000
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

[root@centos7 php]# docker build -t php:lnmp .

[root@centos7 php]# docker run -itd --name php --net mynetwork --ip 172.18.0.30 -p 9000:9000 --volumes-from nginx --volumes-from mysql php:lnmp

4) 数据库授权

[root@centos7 ~]# docker exec -it mysql bash
[root@7edf27a51ec8 bin]# mysql
mysql> create database wordpress;
mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456';
mysql> flush privileges;

[root@centos7 ~]# docker ps
CONTAINER ID   IMAGE        COMMAND                   CREATED          STATUS          PORTS                                       NAMES
cb37efa4d5e2   nginx:lnmp   "/usr/local/nginx/sb…"   2 minutes ago    Up 2 minutes    0.0.0.0:80->80/tcp, :::80->80/tcp           nginx
dc37b1b0cee4   php:lnmp     "/bin/sh -c '/usr/lo…"   10 minutes ago   Up 10 minutes   0.0.0.0:9000->9000/tcp, :::9000->9000/tcp   php
7edf27a51ec8   mysql:lnmp   "/usr/sbin/init"          20 minutes ago   Up 20 minutes   3306/tcp    mysql

image-20240302161532366

image-20240302161857717

image-20240302161910313

6、基于 Dockerfile 构建 SSHD 镜像

[root@centos7 sshd]# ssh-keygen 
[root@centos7 sshd]# cp /root/.ssh/id_rsa.pub .

[root@centos7 sshd]# vim Dockerfile 
FROM centos:7
LABEL MAINTAINER="zhouhao"
RUN yum -y install openssh-server net-tools openssh-devel lsof telnet && sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config && ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key && ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
ADD id_rsa.pub /root/.ssh/authorized_keys
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
EXPOSE 22
CMD ["/usr/sbin/sshd" , "-D"]

[root@centos7 sshd]# docker build -t centos79:sshd .

[root@centos7 sshd]# docker run -d -p 2222:22 --name sshd-test --restart=always centos79:sshd

[root@centos7 sshd]# ssh localhost -p 2222
The authenticity of host '[localhost]:2222 ([::1]:2222)' can't be established.
RSA key fingerprint is SHA256:yAoYfgwonO9VFUd2uj1WyE/+AhRK5ALjErDlLnzPprU.
RSA key fingerprint is MD5:b2:df:01:62:f5:7f:9d:ee:52:cb:b6:54:bf:92:0c:81.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:2222' (RSA) to the list of known hosts.
[root@85f400a2c792 ~]# 

7、基于 Dockerfile 构建 HTTPD 镜像

[root@centos7 httpd]# vim Dockerfile
FROM centos:7
LABEL MAINTAINER="zhouhao"
RUN yum -y install httpd && echo "hello linux" >/var/www/html/index.html && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
EXPOSE 80
CMD ["httpd","-DFOREGROUND"]

[root@centos7 httpd]# docker build -t centos79:httpd .

[root@centos7 httpd]# curl localhost
hello linux

8、基于 Dockerfile 构建 Nginx1.19 镜像

[root@centos7 nginx]# vim Dockerfile
FROM centos:7
LABEL MAINTAINER="zhouhao"
RUN yum install -y wget proc-devel net-tools gcc zlib zlib-devel make openssl-devel && wget http://nginx.org/download/nginx-1.19.0.tar.gz && tar zxf nginx-1.19.0.tar.gz && cd nginx-1.19.0 && ./configure --prefix=/usr/local/nginx && make && make install
EXPOSE 80
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ADD run.sh /run.sh
RUN chmod 775 /run.sh
CMD ["/run.sh"]

[root@centos7 nginx]# cat run.sh 
#!/bin/bash
/usr/local/nginx/sbin/nginx

[root@centos7 nginx]# docker build -t centos79:nginx11.9 .

[root@centos7 nginx]# docker run -d -p 8000:80 --name nginx-test --restart=always centos79:nginx11.9

[root@centos7 nginx]# curl -I localhost:8000
HTTP/1.1 200 OK
Server: nginx/1.19.0
Date: Sat, 02 Mar 2024 09:00:15 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sat, 02 Mar 2024 08:57:50 GMT
Connection: keep-alive
ETag: "65e2ea0e-264"
Accept-Ranges: bytes

9、基于 Dockerfile 构建 MariaDB 镜像

[root@centos7 mysql]# vim Dockerfile 
FROM centos:7
LABEL MAINTAINER="zhouhao"

#安装mariadb数据库
RUN yum install -y mariadb mariadb-server mariadb-devel 
 
#设置环境变量,便于管理
ENV MARIADB_USER root
ENV MARIADB_PASS 123456
 
#让容器支持中文
ENV LC_ALL en_US.UTF-8
 
#初始化数据库
ADD db_init.sh /root/db_init.sh
RUN chmod 775 /root/db_init.sh && /root/db_init.sh
 
EXPOSE 3306
 
#设置默认启动命令
CMD ["mysqld_safe"]


[root@centos7 mysql]# vim db_init.sh
#!/bin/bash
mysql_install_db --user=mysql
sleep 3
mysqld_safe &
sleep 3
mysql -e "use mysql; grant all privileges on *.* to '$MARIADB_USER'@'%' identified by '$MARIADB_PASS' with grant option;"
h=$(hostname)
mysql -e "use mysql; update user set password=password('$MARIADB_PASS') where user='$MARIADB_USER' and host='$h';"
mysql -e "flush privileges;"

[root@centos7 mysql]# docker build -t centos79:mariadb5.5 .

[root@centos7 mysql]# docker run -d -p 3306:3306 --name=mysql-test centos79:mariadb5.5

#宿主机安装mariadb客户端工具
[root@centos7 mysql]# yum -y install mariadb

[root@centos7 mysql]# mysql -h 10.0.0.6 -u root -P 3306 -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

10、基于 Dockerfile 构建 Redis 镜像

[root@centos7 redis]# vim Dockerfile
FROM centos:7
LABEL MAINTAINER="zhouhao"
RUN yum -y update && yum -y install epel-release && yum -y install redis && sed -i -e 's@bind 127.0.0.1@bind 0.0.0.0@g' /etc/redis.conf && sed -i -e 's@protected-mode yes@protected-mode no@g' /etc/redis.conf && echo "requirepass 123456" >> /etc/redis.conf && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
EXPOSE 6379
CMD [ "/usr/bin/redis-server","/etc/redis.conf"]

[root@centos7 redis]# docker build -t centos79:redis .

[root@centos7 redis]# docker run -d -p 6379:6379 --name redis-test --restart=always centos79:redis

#宿主机安装redis工具
[root@centos7 redis]# yum install epel-release
[root@centos7 redis]# yum -y install redis

[root@centos7 redis]# redis-cli -h localhost -a 123456
localhost:6379> info server
# Server
redis_version:3.2.12
......

11、基于 Docker 启动 PG11

#docker快速启动pg11.5
[root@centos7 pg]# docker run -d --name postgresql postgres:11.5

[root@centos7 pg]# docker ps
CONTAINER ID   IMAGE           COMMAND                   CREATED          STATUS         PORTS      NAMES
31a3a8978162   postgres:11.5   "docker-entrypoint.s…"   10 seconds ago   Up 9 seconds   5432/tcp   postgresql

[root@centos7 pg]# docker exec -it 31 bash

root@31a3a8978162:/# su - postgres
postgres@31a3a8978162:~$ psql 
psql (11.5 (Debian 11.5-3.pgdg90+1))
Type "help" for help.
postgres=# 


#docker快速启动pg14.1
[root@centos7 pg]# docker run -d --name postgresql  -e POSTGRES_PASSWORD=123456 -p 15432:5432 postgres:14.1

[root@centos7 pg]# docker ps
CONTAINER ID   IMAGE           COMMAND                   CREATED        STATUS        PORTS                                         NAMES
b898a2341f57   postgres:14.1   "docker-entrypoint.s…"   1 second ago   Up 1 second   0.0.0.0:15432->5432/tcp, :::15432->5432/tcp   postgresql

[root@centos7 pg]# docker exec -it b bash

root@b898a2341f57:/# su - postgres
postgres@b898a2341f57:~$ psql
psql (14.1 (Debian 14.1-1.pgdg110+1))
Type "help" for help.
postgres=# 

12、基于 Docker 配置 MySQL 主从

#环境:
#主机一,master:10.0.0.6
#主机二,slave:10.0.0.16

#master主机执行:
[root@master ~]# docker run -d -p 3306:3306 --name mysql-master -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.44

[root@master ~]# ls
my.cnf

[root@master ~]# docker cp my.cnf 3a3f36668a5c:/etc/my.cnf

[root@master ~]# docker restart mysql-master

[root@master ~]# docker exec -it 3 bash
bash-4.2# mysql -uroot -p123456

mysql> grant replication slave on *.* to slave@'%' identified by '123456';

mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin-master.000001
         Position: 438
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

#slave主机执行:
[root@slave ~]# docker run -d -p 3306:3306 --name mysql-slave -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.44

#切记修改文件的server-id值
[root@slave ~]# ls
my.cnf

[root@slave ~]# docker exec -it 7 bash
bash-4.2# rm -rf /etc/my.cnf
bash-4.2# exit

[root@slave ~]# docker cp my.cnf 7:/etc/my.cnf
Successfully copied 3.07kB to 7:/etc/my.cnf

[root@slave ~]# docker restart 7

[root@slave ~]# docker exec -it 7 bash
bash-4.2# mysql -uroot -p123456
mysql> CHANGE MASTER TO
  MASTER_HOST='10.0.0.6',
  MASTER_USER='slave',
  MASTER_PASSWORD='123456',
  MASTER_PORT=3306,
  MASTER_LOG_FILE='mysql-bin-master.000001',
  MASTER_LOG_POS=438;
  
mysql> start slave;

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.6
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin-master.000001
          Read_Master_Log_Pos: 438
               Relay_Log_File: 7755cdad2241-relay-bin.000002
                Relay_Log_Pos: 327
        Relay_Master_Log_File: mysql-bin-master.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
......

image-20240302181835136

13、基于 Docker 配置 MySQL 主主

#承接上题,继续完善
#slave主机执行:
#在 my.cnf 新增一行内容
[mysqld]
log-bin=mysql-bin-master

[root@slave ~]# docker exec -it 7 bash
bash-4.2# rm -rf /etc/my.cnf
bash-4.2# exit

[root@slave ~]# docker cp my.cnf 7:/etc/my.cnf

[root@slave ~]# docker restart 7

[root@slave ~]# docker exec -it 7 bash
bash-4.2# mysql -uroot -p123456
mysql> show master logs;
+-------------------------+-----------+
| Log_name                | File_size |
+-------------------------+-----------+
| mysql-bin-master.000001 |       154 |
+-------------------------+-----------+
1 row in set (0.00 sec)

mysql> grant replication slave on *.* to slave@'%' identified by '123456';


#master主机执行:
[root@master ~]# docker exec -it 3 bash
bash-4.2# mysql -uroot -p123456
mysql> mysql> CHANGE MASTER TO
  MASTER_HOST='10.0.0.16',
  MASTER_USER='slave',
  MASTER_PASSWORD='123456',
  MASTER_PORT=3306,
  MASTER_LOG_FILE='mysql-bin-master.000001',
  MASTER_LOG_POS=154;

mysql> start slave;

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.16
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin-master.000001
          Read_Master_Log_Pos: 438
               Relay_Log_File: 3a3f36668a5c-relay-bin.000002
                Relay_Log_Pos: 611
        Relay_Master_Log_File: mysql-bin-master.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
......


#slave主机执行:
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.6
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin-master.000001
          Read_Master_Log_Pos: 438
               Relay_Log_File: 7755cdad2241-relay-bin.000004
                Relay_Log_Pos: 327
        Relay_Master_Log_File: mysql-bin-master.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
......

image-20240302182824612

image-20240302182839519

14、基于 Dockerfile 配置 HAProxy 负载均衡 双主MySQL

image-20240302183914940

#承接上题,继续完善
[root@centos7 ~]# vim Dockerfile
FROM centos:7
LABEL maintainer="zhouhao>"
RUN yum -y install wget && rm -f /etc/yum.repos.d/* && wget -O /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-7-reg.repo && yum -y install vim-enhanced tcpdump lrzsz tree telnet bash-completion net-tools wget curl bzip2 lsof zip unzip nfs-utils gcc make gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel && yum clean all && rm -f /etc/localtime && ln -s ../usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ADD haproxy-2.2.25.tar.gz /usr/local/src/
RUN cd /usr/local/src/haproxy-2.2.25 \
&& make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/apps/haproxy \
&& make install PREFIX=/apps/haproxy \
&& ln -s /apps/haproxy/sbin/haproxy /usr/sbin/ \
&& mkdir /apps/haproxy/run \
&& rm -rf /usr/local/src/haproxy*  
ADD haproxy.cfg /etc/haproxy/
ADD run_haproxy.sh /usr/bin
EXPOSE 80 9999
CMD ["run_haproxy.sh"]  


[root@centos7 ~]# vim haproxy.cfg 
global
chroot /apps/haproxy
uid 99
gid 99
daemon
nbproc 1
pidfile /apps/haproxy/run/haproxy.pid
log 127.0.0.1 local3 info
defaults
option http-keep-alive
option forwardfor
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
listen stats
 mode http
 bind 0.0.0.0:9999
 stats enable
 log global
 stats uri /haproxy-status
 stats auth haadmin:123456 

listen mysql
bind 0.0.0.0:3306
option forwardfor
balance roundrobin
mode tcp
server server1 10.0.0.6:3306 check inter 2s  fall 3 rise 5  weight 1
server server2 10.0.0.16:3306 check inter 2s  fall 3 rise 5  weight 1

[root@haporxy ~]# vim run_haproxy.sh   
#!/bin/bash
haproxy -f /etc/haproxy/haproxy.cfg
tail -f /etc/hosts
  
[root@haporxy ~]# chmod +x run_haproxy.sh

[root@haporxy ~]# vim build.sh
#!/bin/bash
docker build -t centos79:haproxy-2.2.25 .

[root@haporxy ~]# bash build.sh

[root@haporxy data]# docker run -d -p 3306:3306 -p 9999:9999 --name haproxy-db centos79:haproxy-2.2.25

15、基于 Dockerfile 配置网盘可道云

#基于案例4镜像:nginx-php5:v1
[root@centos7 ~]# docker run --name nginx-php -d -p 80:80 nginx-php5:v1

[root@centos7 ~]# docker exec -it 0 bash

[root@0c743dcdab2c /]# vi /etc/nginx/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   /demo/html;
            index  index.php index.html index.htm;
        }
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /demo/html$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

[root@0c743dcdab2c /]# nginx -t

[root@0c743dcdab2c /]# vi /etc/php-fpm.d/www.conf
#修改下面2行用户名
user = nginx
group = nginx

[root@0c743dcdab2c /]# mkdir /demo/html -p
[root@0c743dcdab2c /]# exit

[root@centos7 ~]# docker cp kodexplorer4.40.zip 0c743dcdab2c:/demo/html

[root@centos7 ~]# docker exec -it 0 bash
[root@0c743dcdab2c /]# cd /demo/html/
[root@0c743dcdab2c html]# ls
kodexplorer4.40.zip

[root@0c743dcdab2c html]# yum -y install unzip
[root@0c743dcdab2c html]# unzip kodexplorer4.40.zip
[root@0c743dcdab2c html]# chown -R nginx:nginx .

[root@0c743dcdab2c html]# vim /demo_init.sh 
nginx
php-fpm -D
tail -f /etc/hosts

[root@centos7 ~]# docker commit 0c743dcdab2c demo_kod:v1
[root@centos7 ~]# docker container run -d -p 80:80 demo_kod:v1 /bin/bash -x /demo_init.sh

#浏览器访问:http://10.0.0.6

image-20240302190102207

#解决上图报错
[root@centos7 ~]# docker exec -it c bash
[root@cb67eefdbf36 /]# yum -y install php-gd php-mbstring

[root@centos7 ~]# docker restart c

image-20240302190201442

[root@centos7 ~]# docker commit cb67eefdbf36 demo_kod:v2
[root@centos7 ~]# docker run -d -p 80:80 demo_kod:v2 /bin/bash -x /demo_init.sh

image-20240302190313658

image-20240302190337081