构建LNP镜像
阅读原文时间:2023年07月09日阅读:1

1、构建ubuntu-base环境

 #10.0.0.28:
 [root@rocky8 ubuntu_base]#vim Dockerfile
 FROM ubuntu:20.04
 LABEL maintainer="www.wang.org" version="ubuntu_base:v1.0"
 ​
 RUN sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
 ​
 RUN apt update && \
     apt -y install tree wget vim curl iproute2 tzdata zip unzip && \
     rm -f /etc/localtime && \
     ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
 [root@rocky8 ubuntu_base]#docker build -t ubuntu_base:v1.0 .

2、基于ubuntu-base镜像构建php环境

#10.0.0.28:
 [root@rocky8 ubuntu_php]#vim 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/
 ​
 ​
 [root@rocky8 ubuntu_php]#ls
 Dockerfile  php-7.4.30.tar.gz  php-fpm.conf  php.ini  www.conf
 ​
 [root@rocky8 ubuntu_php]#docker build -t ubuntu_php:v1.0 .

3、基于php镜像构建nginx镜像

 #10.0.0.28:
 [root@rocky8 ubuntu_nginx]#vim Dockerfile
 FROM ubuntu_php:v1.0
 LABEL maintainer="www.wang.org" version="ubuntu_nginx: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 chown -R nginx.nginx /apps/nginx/
 ​
 COPY run_nginx.sh /usr/local/sbin/
 ​
 EXPOSE 80 443
 ​
 CMD ["run_nginx.sh"]
 ​
 [root@rocky8 ubuntu_nginx]#vim run_nginx.sh
 /apps/php74/sbin/php-fpm
 /apps/nginx/sbin/nginx -g "daemon off;"
 ​
 [root@rocky8 ubuntu_nginx]#chmod +x run_nginx.sh
 ​
 [root@rocky8 conf.d]#vim wordpress.wang.org.conf
 server {
     listen 80;
     server_name wordpress.wang.org;
     root /data/wordpress;
     index index.php index.html index.htm;
     location ~ \.php$|ping {
         root /data/wordpress;
         fastcgi_pass 127.0.0.1:9000;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
     }
 }
 ​
 [root@rocky8 ubuntu_nginx]#docker build -t ubuntu_nginx:v1.0 .
 ​
 [root@rocky8 data]#groupadd -r -g 888 nginx
 [root@rocky8 data]#useradd -u 888 -g nginx -s /sbin/nologin -r nginx
 [root@rocky8 data]#id nginx
 uid=888(nginx) gid=888(nginx) groups=888(nginx)
 [root@rocky8 data]#chmod -R nginx.nginx /data/wordpress/      #如果不给权限,wordpress连接数据会报无法写入的错误
 ​
 ​
 [root@rocky8 conf.d]#docker run -it -d -p 80:80 --name wordpress -v /data/conf.d:/apps/nginx/conf/conf.d  -v /data/wordpress:/data/wordpress ubuntu_nginx:v1.0 

4、安装数据库

 #10.0.0.28:
 root@rocky8 ~]#docker pull mysql:8.0.30-oracle
 ​
 [root@rocky8 ~]#docker run -d --name mysql --network container:wordpress -e MYSQL_ROOT_PASSWORD=123456  -e MYSQL_DATABASE=wordpress  -e MYSQL_USER=wordpress -e MYSQL_PASSWORD=123456 -v /data/mysql:/var/lib/mysql mysql:8.0.30-oracle
 ​
 [root@rocky8 conf.d]#docker ps
 CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS                                        NAMES
 b5c7e672d29f   mysql:8.0.30-oracle   "docker-entrypoint.s…"   9 minutes ago    Up 9 minutes                                                 mysql
 15a1a13becbe   ubuntu_nginx:v1.0     "run_nginx.sh"           12 minutes ago   Up 12 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp   wordpress
 ​

5、实现harbor高可用

 #10.0.0.101:
 [root@ubuntu2004 ~]#bash install_harbor.sh
 [root@ubuntu2004 ~]#vim /apps/harbor/harbor.yml
 hostname: 10.0.0.101
 [root@ubuntu2004 ~]#/apps/harbor/prepare
 ​
 [root@ubuntu2004 ~]#vim /etc/docker/daemon.json
 {
   "registry-mirrors": ["https://pgavrk5n.mirror.aliyuncs.com"],
   "insecure-registries": ["10.0.0.101","10.0.0.102"]
 ​
 }
 [root@ubuntu2004 ~]#systemctl restart docker.service
 [root@ubuntu2004 ~]#systemctl restart harbor.service
 ​
 ​
 ##10.0.0.102:
 [root@ubuntu2004 ~]#bash install_harbor.sh
 [root@ubuntu2004 ~]#vim /apps/harbor/harbor.yml
 hostname: 10.0.0.102
 [root@ubuntu2004 ~]#/apps/harbor/prepare
 ​
 [root@ubuntu2004 ~]#vim /etc/docker/daemon.json
 {
   "registry-mirrors": ["https://pgavrk5n.mirror.aliyuncs.com"],
   "insecure-registries": ["10.0.0.101","10.0.0.102"]
 }
 [root@ubuntu2004 ~]#systemctl restart docker.service
 [root@ubuntu2004 ~]#systemctl restart harbor.service
 ​
 ​
 #浏览器分别登陆101和102设置:项目、目标和规则
 ​

5-1、上传测试
 [root@rocky8 ubuntu_nginx]#docker login 10.0.0.101
 [root@rocky8 ubuntu_nginx]#docker tag ubuntu_nginx:v1.0 10.0.0.101/test/ubuntu_nginx:v1.0
 [root@rocky8 ubuntu_nginx]#docker push 10.0.0.101/test/ubuntu_nginx:v1.0
 ​