部署MySQL
1、安装依赖包
[root@localhost ~]# docker run -it --name mysql --network container:nginx01 centos /bin/bash
## --network container:nginx01 指定网卡类型 使用另外一个容器的网卡nginx01 使用nginx01的网卡
[root@93ec2dfe3c14 /]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
Failed to set locale, defaulting to C.UTF-8
CentOS Linux 8 - AppStream 639 kB/s | 8.2 MB 00:13
CentOS Linux 8 - BaseOS 645 kB/s | 3.5 MB 00:05
CentOS Linux 8 - Extras 13 kB/s | 10 kB 00:00
Dependencies resolved.
====================================================================
Package Arch Version Repo Size
====================================================================
......
......
[root@93ec2dfe3c14 /]# yum -y install ncurses-compat-libs
2、下载mysql镜像
可以在网上下载mysql5.7版本,或者在物理机上传输mysql压缩包到容器中,本次采用传输压缩包的方式。
[root@localhost ~]# docker cp /usr/src/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz 1ec177a335f7:/usr/local
[root@93ec2dfe3c14 local]# ls
debug kernels mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
3、解压包并且编译
[root@93ec2dfe3c14 local]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
[root@93ec2dfe3c14 local]# mv mysql-5.7.34-linux-glibc2.12-x86_64/ mysql
[root@93ec2dfe3c14 local]# ls
bin games lib libexec sbin src
etc include lib64 mysql share
[root@ee9ad529a1fe local]# useradd -r -M -s /sbin/nologin mysql
[root@93ec2dfe3c14 local]# chown mysql.mysql mysql/
[root@93ec2dfe3c14 local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@93ec2dfe3c14 local]# source /etc/profile.d/mysql.sh
//配置MySQL的lib库文件,lib文件不在默认的/usr/lib/中所有要写一个配置文件让系统能找到lib库文件
[root@ee9ad529a1fe local]# vi /etc/ld.so.conf.d/mysql.conf
[root@ee9ad529a1fe local]# /usr/local/mysql/lib
[root@ee9ad529a1fe local]# ldconfig
//创建存放数据的目录
[root@ee9ad529a1fe local]# mkdir /opt/data
[root@ee9ad529a1fe local]# chown -R mysql.mysql /opt/data
[root@ee9ad529a1fe local]# ls /opt/
data
4、初始化数据库
[root@ee9ad529a1fe data]# mysqld --initialize --user mysql --datadir /opt/data
## 如果提示缺少依赖包 直接yum安装这两个包即可 libaio、numactl
//生成配置文件
[root@ee9ad529a1fe etc]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
//修改配置文件
[root@ee9ad529a1fe etc]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /usr/local/mysql/support-files/mysql.server
[root@ee9ad529a1fe etc]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /usr/local/mysql/support-files/mysql.server
//启动mysql
[root@ee9ad529a1fe etc]# /usr/local/mysql/support-files/mysql.server start
[root@ee9ad529a1fe mysql]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
[root@ee9ad529a1fe support-files]# mysql -uroot -pwjm123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.34 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
//死循环
[root@ee9ad529a1fe ~]# cat docker-entrypoint.sh
#!/bin/bash
/usr/local/mysql/support-files/mysql.server start && \
while true;do echo hello docker;sleep 1;done;
[root@ee9ad529a1fe ~]# chmod +x docker-entrypoint.sh
5、打包 容器为镜像
[root@localhost ~]# docker commit -p -a 'wjm 1@2.com' -c 'CMD ["/root/docker-entrypoint.sh"]' mysql wjm1734321/mysql:v0.1
sha256:422c8d0aa073e55839d127c4a3091cebafcb6f069e709c0f473a37570d5d9344
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wjm1734321/mysql v0.1 422c8d0aa073 11 seconds ago 3.81GB
wjm1734321/nginx v0.1 35526d74a9e1 4 hours ago 550MB
mysql latest bbf6571db497 27 hours ago 516MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@localhost ~]# docker run -d --name mysql-test wjm1734321/mysql:v0.1
c542a8372311e107bf2704e648f5aa766d133ac0501b426ce3308beae55eb35c
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c542a8372311 wjm1734321/mysql:v0.1 "/root/docker-entryp…" 4 seconds ago Up 3 seconds mysql-test
d90b72fbc1f5 centos "/bin/bash" 2 hours ago Up 2 hours mysql
ee9ad529a1fe wjm1734321/nginx:v0.1 "/usr/local/nginx/sb…" 4 hours ago Up 2 hours 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
[root@localhost ~]#
部署PHP
1、下载所需的依赖包
[root@ee9ad529a1fe /]# yum -y install epel-release
[root@ee9ad529a1fe /]# yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
[root@ee9ad529a1fe /]# yum install sqlite-devel libzip-devel libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg-turbo libjpeg-turbo-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel gcc gcc-c++ make which --allowerasing
//将准备好的PHP安装包传到PHP容器中
[root@localhost ~]# docker cp php-8.0.12.tar.gz 49cbbffc2a11:/usr/src
[root@ee9ad529a1fe /]# cd /usr/src/
[root@ee9ad529a1fe src]# ls
debug kernels php-8.0.12.tar.gz
2、编译安装
[root@ee9ad529a1fe src]# tar xf php-8.0.12.tar.gz
[root@ee9ad529a1fe src]# cd php-8.0.12
[root@ee9ad529a1fe php-8.0.12]# ./configure --prefix=/usr/local/php8 \
--with-config-file-path=/etc \
--enable-fpm \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif \
--enable-ftp \
--enable-gd \
--with-jpeg \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--with-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix
[root@ee9ad529a1fe php-8.0.12]# make && make install
3、配置
//环境变量
[root@ee9ad529a1fe php-8.0.12]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@ee9ad529a1fe php-8.0.12]# source /etc/profile.d/php.sh
[root@ee9ad529a1fe php-8.0.12]# which php
/usr/local/php8/bin/php
[root@ee9ad529a1fe php-8.0.12]# cp -f php.ini-production /etc/php.ini
[root@ee9ad529a1fe php-8.0.12]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm -f
[root@ee9ad529a1fe php-8.0.12]# chmod +x /etc/init.d/php-fpm
[root@ee9ad529a1fe local]# cp -f /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@ee9ad529a1fe local]# cp -f /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
//启动PHP
[root@ee9ad529a1fe sbin]# /usr/local/php8/sbin/php-fpm
[root@ee9ad529a1fe sbin]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
[root@ee9ad529a1fe sbin]# vi /root/docker-entrypoint.sh
#!/bin/bash
/usr/local/php8/sbin/php-fpm && \
while true;do echo hello docker;sleep 1;done;
[root@ee9ad529a1fe ~]# chmod +x docker-entrypoint.sh
4、修改nginx配置文件
//进入nginx中
[root@localhost ~]# docker exec -it ee9ad529a1fe /bin/bash
[root@ee9ad529a1fe conf]# pwd
/usr/local/nginx/conf
[root@ee9ad529a1fe conf]# vi nginx.conf
......
43 location / {
44 root html;
45 index index.php index.html index.htm; //此处添加index.php
46 }
//取消65-71行注释内容并更改69行路径
65 location ~ \.php$ {
66 root /var/www/html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
70 include fastcgi_params;
71 }
6、在PHP主机上创建与配置文件一样的文件路径
[root@ee9ad529a1fe php8]# mkdir /var/www/html -p
[root@ee9ad529a1fe php8]# cd /var/www/html/
[root@ee9ad529a1fe html]# vi index.php
<?php
phpinfo();
?>
7、重启加载nginx
//重新加载配置文件
[root@ee9ad529a1fe html]# nginx -s reload
[root@ee9ad529a1fe html]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
8、打包成镜像
[root@localhost ~]# docker commit -p -a 'wjm 1@2.com' -c 'CMD ["/root/docker-entrypoint.sh"]' php wjm1734321/php:v0.1
sha256:46d819d2ecd21e3403b1ab01d9fe5d0e7aa045932884ebe89866628f9e6df70b
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wjm1734321/php v0.1 46d819d2ecd2 5 seconds ago 1.52GB
wjm1734321/mysql v0.1 422c8d0aa073 16 hours ago 3.81GB
wjm1734321/nginx v0.1 35526d74a9e1 19 hours ago 550MB
mysql latest bbf6571db497 42 hours ago 516MB
centos latest 5d0da3dc9764 2 months ago 231MB