19 张图手把手用 Docker 搭建个人博客平台

2023-11-05 11:58:16 浏览数 (1)

1. 当前需求

需要使用 Docker 技术在一台主机上创建 LNMP 服务并运行 Wordpress 网站平台。

然后对此服务进行相关的性能调优和管理工作

2. 前置准备

主机

操作系统

IP 地址

主要软件

宿主机

CentOS 7.3 x86_64

192.168.67.100

Docker 19.03

Nginx 容器

172.18.0.10

Mysql 容器

172.18.0.20

PHP 容器

172.18.0.30

3. 搭建过程

3.1 创建自定义网络

代码语言:javascript复制
#关闭防火墙和selinux
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
代码语言:javascript复制
#创建自定义网络
docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1"  mynetwork

3.2 部署并配置 nginx

容器 IP:172.18.0.10

3.2.1 创建工作目录并上传相关软件包
代码语言:javascript复制
mkdir /opt/nginx
cd /opt/nginx
3.2.2 解压缩相关软件包
代码语言:javascript复制
mkdir /opt/nginx/html
tar zxvf wordpress-4.9.4-zh_CN.tar.gz -C /opt/nginx/html
代码语言:javascript复制
rm -rf wordpress-4.9.4-zh_CN.tar.gz
#解压缩完成后删除安装包
3.2.3 编写 Dockerfile 文件
代码语言:javascript复制
vim Dockerfile

FROM centos:7 AS first
MAINTAINER this is nginx <scj>
ADD nginx-1.22.0.tar.gz /opt/
RUN yum -y install pcre-devel zlib-devel gcc gcc-c   make && 
useradd -M -s /sbin/nologin nginx && 
cd /opt/nginx-1.22.0 && 
./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/

FROM centos:7
COPY --from=first /usr/local/nginx/ /usr/local/nginx/
RUN useradd -M -s /sbin/nologin nginx

EXPOSE 80
EXPOSE 443

CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
3.2.4 编写 nginx.conf 文件
代码语言:javascript复制
vim nginx.conf

#user  nobody;
worker_processes  auto;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    use epoll;
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  www.scj.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   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   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           html;
            fastcgi_pass   172.18.0.30:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
3.2.5 创建 nginx 镜像
代码语言:javascript复制
docker build -t nginx:lnmp .
3.2.6 运行容器
代码语言:javascript复制
#以nginx容器 在后台运行镜像
docker run -d --name nginx -p 80:80 -v /opt/nginx/html:/usr/local/nginx/html --net mynetwork --ip 172.18.0.10 nginx:lnmp


- "-d" 表示以后台运行的方式启动容器。
- "--name nginx" 设置容器的名称为 "nginx"。
- "-p 80:80" 将主机的 80 端口映射到容器的 80 端口,使得可以通过主机的 IP 地址访问 NGINX 服务器。
- "-v /opt/nginx/html:/usr/local/nginx/html" 将主机上的 /opt/nginx/html 目录与容器内的 /usr/local/nginx/html 目录进行挂载,实现文件共享。
- "--net mynetwork --ip 172.18.0.10" 将容器连接到名为 "mynetwork" 的用户自定义网络,并指定容器的 IP 地址为 172.18.0.10。
- "nginx:lnmp" 指定了要使用的 NGINX 服务器镜像和标签。

3.3 部署并配置 mysql

容器 IP 为 172.168.0.20

多阶段构造

3.3.1 创建工作目录
代码语言:javascript复制
mkdir /opt/mysqld
cd /opt/mysqld

上传 mysql-boost-5.7.20.tar.gz 到 /opt/mysqld 目录中
3.3.2 编写 Dockerfie 文件(多阶段构建)
代码语言:javascript复制
vim Dockerfile

# 第一阶段 - 构建阶段
FROM centos:7 AS builder
MAINTAINER this is mysql image <scj>
RUN yum -y install gcc gcc-c   ncurses ncurses-devel bison cmake make
ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
WORKDIR /usr/local/src/mysql-5.7.20/
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_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

# 第二阶段 - 配置阶段
FROM centos:7 AS config
COPY --from=builder /usr/local/mysql /usr/local/mysql
COPY my.cnf /etc/
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
RUN useradd -M -s /sbin/nologin  mysql && 
chown mysql:mysql /etc/my.cnf && 
chown -R mysql:mysql /usr/local/mysql/ && 
/usr/local/mysql/bin/mysqld 
--initialize-insecure 
--user=mysql 
--basedir=/usr/local/mysql 
--datadir=/usr/local/mysql/data
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld"]
3.3.3 编写配置文件 my.cnf
代码语言:javascript复制
vim my.cnf

[client]
port = 3306
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
socket = /usr/local/mysql/mysql.sock

[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
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

3.3.4 创建镜像
代码语言:javascript复制
docker build -t mysql:lnmp .
#名称:标签
3.3.5 使用新的镜像运行容器并测试
代码语言:javascript复制
docker run --name=mysql -d --privileged -v /usr/local/mysql --net mynetwork --ip 172.18.0.20 mysql:lnmp
- `--name=mysql`:指定容器的名称为mysql。
- `-d`:以后台模式运行容器。
- `--privileged`:在容器内部启用特权模式,可以执行一些底层操作。
- `-v /usr/local/mysql`:将宿主机的`/usr/local/mysql`目录挂载到容器的相应位置。
- `--net mynetwork`:将容器连接到名为mynetwork的网络。

3.4 部署并配置 PHP

容器 IP:172.168.0.30

3.4.1 创建工作目录并上传代码包
代码语言:javascript复制
mkdir /opt/php
cd /opt/php

上传 php-7.1.10.tar.bz2 到 /opt/php 目录中
3.4.2 编写相关配置文件

php-fpm.conf

www.conf

php.ini

3.4.3 编写 Dockerfile 文件
代码语言:javascript复制
vim Dockerfile

FROM centos:7 AS first
MAINTAINER this is php image <byyb 20231021>
ADD php-7.1.10.tar.bz2 /opt/
RUN yum -y install 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 && 
cd /opt/php-7.1.10/ && 
./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 2 && make install && 
cp /opt/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini && 
sed -i -e '939c date.timezone = Asia/Shanghai'  -e '1170c mysqli.default_socket = /usr/local/mysql/mysql.sock' /usr/local/php/lib/php.ini && 
cd /usr/local/php/etc/ && 
cp  php-fpm.conf.default php-fpm.conf && 
sed -i '17 s/^;//' php-fpm.conf && 
cd /usr/local/php/etc/php-fpm.d/ && 
cp www.conf.default www.conf && 
sed -i -e '23c user = nginx' -e '24c group = nginx' -e '36c listen = 172.18.0.30:9000' -e '62c listen.allowed_clients = 172.18.0.10' www.conf

FROM centos:7
COPY --from=first /usr/local/php/ /usr/local/php/
RUN yum -y install gd 
libjpeg-devel 
libpng-devel 
freetype-devel 
libxml2-devel 
zlib-devel 
curl-devel 
openssl-devel && 
useradd -M -s /sbin/nologin nginx && 
rm -rf /var/cache/yum/*
EXPOSE 9000
CMD ["/usr/local/php/sbin/php-fpm", "-F"]
3.4.4 创建 php 镜像
代码语言:javascript复制
docker build -t php:lnmp .
3.4.5 运行容器
代码语言:javascript复制
docker run --name=php -d -p 9000:9000 --volumes-from mysql --volumes-from nginx --net mynetwork --ip 172.18.0.30 php:lnmp

3.5 数据库授权

代码语言:javascript复制
docker exec -it mysql /bin/bash
代码语言:javascript复制
mysql
#登录数据库

create database wordpress;

grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
flush privileges;

3.6 运行 Wordpress

代码语言:javascript复制
浏览器访问:
http://192.168.67.100/wordpress/index.php

作者:相当大佬的一天 出处:https://urlify.cn/rmMJNz

0 人点赞