启动docker
systemctl start docker
查看镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
yunjisuanlp/nginx v1 ac3319cbbafb 17 hours ago 550MB # nginx镜像已经完成
centos latest 5d0da3dc9764 2 months ago 231MB
启动并映射nginx
[root@localhost ~]# docker run -idt --name nginx88 -p 80:80 yunjisuanlp/nginx:v1
# 查看
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3367881fd446 yunjisuanlp/nginx:v1 "/usr/local/nginx/sb…" 12 minutes ago Up 12 minutes confident_wing
# 重新打开一个终端访问nginx默认页面
[root@localhost ~]# curl 172.17.0.2
……………………
Welcome to nginx!
……………………
做mysql镜像并使用容器模式的网络
[root@localhost ~]# docker run -it --name mysql01 --network container:3367881fd446 centos:latest /bin/bash
[root@3367881fd446 /]# # 启动本地镜像centos 在里面安装MySQL --network container:3367881fd446(以nginx容器ID为共享网络)
重新打开一个终端查看
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2102e9a88ce8 centos:latest "/bin/bash" 34 seconds ago Up 33 seconds mysql01 # mysql容器正在运行
3367881fd446 yunjisuanlp/nginx:v1 "/usr/local/nginx/sb…" 13 minutes ago Up 13 minutes confident_wing
上传MySQL包到本地上传到centos容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2102e9a88ce8 centos:latest "/bin/bash" 34 seconds ago Up 33 seconds mysql01 # mysql容器正运行
3367881fd446 yunjisuanlp/nginx:v1 "/usr/local/nginx/sb…" 13 minutes ago Up 13 minutes confident_wing
[root@localhost ~]# cd /usr/src/
[root@localhost src]# ls
debug kernels mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# docker cp mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz 2102e9a88ce8:/usr/src # cp到mysql01容器ID下的/usr/src
MySQL容器下查看
[root@3367881fd446 ~]# cd /usr/src
[root@3367881fd446 src]# ls
debug kernels mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
# 下载
yum clean all # 清理缓存
yum makecache # 生成新缓存
yum -y install which numactl-libs ncurses-compat-libs # 安装
# 创建用户
[root@3367881fd446 src]# useradd -r -M -s /sbin/nologin mysql
[root@3367881fd446 src]# id mysql
uid=998(mysql) gid=996(mysql) groups=996(mysql)
# 解压MySQL包
[root@3367881fd446 src]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@3367881fd446 src]# cd /usr/local/
[root@3367881fd446 local]# ls
bin games lib libexec sbin src
etc include lib64 mysql-5.7.34-linux-glibc2.12-x86_64 share
# 创建软连接并修改属主属组
[root@3367881fd446 local]# ln -sv mysql-5.7.34-linux-glibc2.12-x86_64 mysql
'mysql' -> 'mysql-5.7.34-linux-glibc2.12-x86_64'
[root@3367881fd446 local]# ls -l
total 0
………………
lrwxrwxrwx 1 root root 35 Dec 3 08:24 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x 9 root root 129 Dec 3 08:21 mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x 2 root root 6 Nov 3 2020 sbin
drwxr-xr-x 5 root root 49 Sep 15 14:17 share
drwxr-xr-x 2 root root 6 Nov 3 2020 src
[root@3367881fd446 local]# chown -R mysql.mysql mysql*
[root@3367881fd446 local]# ls -l
total 0
……………………
lrwxrwxrwx 1 mysql mysql 35 Dec 3 08:24 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x 9 mysql mysql 129 Dec 3 08:21 mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x 2 root root 6 Nov 3 2020 sbin
drwxr-xr-x 5 root root 49 Sep 15 14:17 share
drwxr-xr-x 2 root root 6 Nov 3 2020 src
# 添加环境变量
[root@3367881fd446 local]# echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
[root@3367881fd446 local]# bash
[root@3367881fd446 bin]# which mysql
/usr/local/mysql/bin/mysql
# 创建数据存放目录
[root@3367881fd446 local]# mkdir /opt/data
[root@3367881fd446 local]# ls -l /opt
total 0
drwxr-xr-x 2 root root 6 Dec 3 08:29 data
[root@3367881fd446 local]# chown -R mysql.mysql /opt/data
[root@3367881fd446 local]# ls -l /opt
total 0
drwxr-xr-x 2 mysql mysql 6 Dec 3 08:29 data
# 初始哈数据库不要密码
[root@3367881fd446 local]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data
2021-12-03T08:42:57.345198Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-12-03T08:42:57.466893Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-12-03T08:42:57.489097Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-12-03T08:42:57.542400Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 03bd41c9-5415-11ec-ae6b-0242ac110002.
2021-12-03T08:42:57.543336Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-12-03T08:42:58.297624Z 0 [Warning] CA certificate ca.pem is self signed.
2021-12-03T08:42:58.547329Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
# 生成配置文件
[root@3367881fd446 local]# vi /etc/my.cnf
[mysqld]
port = 3306
datadir = /opt/data
basedir = /usr/local/mysql
socket = /tmp/mysql.sock
pid-file = /opt/data/mysql.pid
log-error = /opt/data/mysql.err
skip-name-resolve
# 修改文件
[root@3367881fd446 local]# vi /usr/local/mysql/support-files/mysql.server
……………………
basedir=/usr/local/mysql
datadir=/opt/data
……………………
# 启动
[root@3367881fd446 local]# 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:*
[root@3367881fd446 local]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL.Logging to '/opt/data/mysql.err'.
SUCCESS!
[root@3367881fd446 local]# 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@3367881fd446 local]# mysql
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> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> exit
Bye
[root@3367881fd446 local]# mysql -uroot -p123456
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 3
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> exit
Bye
将MySQL提交为一个镜像
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2102e9a88ce8 centos:latest "/bin/bash" About an hour ago Up 42 minutes mysql01
3367881fd446 yunjisuanlp/nginx:v1 "/usr/local/nginx/sb…" About an hour ago Up About an hour confident_wing
[root@localhost ~]# docker commit -p mysql01 # 暂停mysql01
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
77281e1fab7e 3 seconds ago 3.78GB # 新提交的镜像
yunjisuanlp/nginx v1 ac3319cbbafb 19 hours ago 550MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@localhost ~]# docker tag 77281e1fab7e yunjisuanlp/mysql:v1 # 起名
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
yunjisuanlp/mysql v1 77281e1fab7e About a minute ago 3.78GB # 成功
yunjisuanlp/nginx v1 ac3319cbbafb 19 hours ago 550MB
centos latest 5d0da3dc9764 2 months ago 231MB
安装php
# 运行一个php容器和nginx共享网络
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2102e9a88ce8 centos:latest "/bin/bash" About an hour ago Up 49 minutes mysql01
3367881fd446 yunjisuanlp/nginx:v1 "/usr/local/nginx/sb…" About an hour ago Up About an hour confident_wing
[root@localhost ~]# docker run -it --name php8 --network container:3367881fd446(nginxID) centos:latest /bin/bash
[root@3367881fd446 /]#
# 重新打开一个终端查看正在运行的容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f482b46f4f0 centos:latest "/bin/bash" About a minute ago Up About a minute php8
2102e9a88ce8 centos:latest "/bin/bash" About an hour ago Up 55 minutes mysql01
3367881fd446 yunjisuanlp/nginx:v1 "/usr/local/nginx/sb…" 2 hours ago Up 2 hours confident_wing
回到php启动的终端
[root@3367881fd446 /]# yum clean all
[root@3367881fd446 /]# yum makecache
[root@3367881fd446 /]# yum -y install epel-release # 下载依托源
# 下载依赖包
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 --allowerasing
yum -y install http://mirror.centos.org/centos/8/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
# 打开另一个终端上传php包到容器内
[root@localhost ~]# ls
anaconda-ks.cfg nginx.tar.gz php-8.0.10.tar.gz
[root@localhost ~]# docker cp php-8.0.10.tar.gz 3f482b46f4f0:/usr/src
# 回到容器终端查看并解压
[root@3367881fd446 /]# cd /usr/src
[root@3367881fd446 src]# ls
debug kernels php-8.0.10.tar.gz
[root@3367881fd446 src]# tar xf php-8.0.10.tar.gz -C /usr/local/
[root@3367881fd446 src]# cd /usr/local/
[root@3367881fd446 local]# ls
bin etc games include lib lib64 libexec php-8.0.10 sbin share src
# 编译安装
[root@3367881fd446 ]# cd php-8.0.10/
[root@3367881fd446 php-8.0.10]# ./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
.......
# 安装
make && make install
......
# 添加环境变量
[root@3367881fd446 php-8.0.10]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@3367881fd446 php-8.0.10]# bash
# 配置php-fpm
[root@3367881fd446 php-8.0.10]# cp php.ini-production /etc/php.ini
[root@3367881fd446 php-8.0.10]# cd sapi/fpm/
[root@3367881fd446 fpm]# ls
config.m4 init.d.php-fpm Makefile.frag php-fpm.8.in php-fpm.service status.html.in www.conf.in
CREDITS init.d.php-fpm.in php-fpm php-fpm.conf php-fpm.service.in tests
fpm LICENSE php-fpm.8 php-fpm.conf.in status.html www.conf
[root@3367881fd446 fpm]# cp init.d.php-fpm /etc/init.d/php-fpm
[root@3367881fd446 fpm]# chmod +x /etc/init.d/php-fpm
[root@3367881fd446 fpm]# cd /usr/local/php8/etc/
[root@3367881fd446 etc]# cp php-fpm.conf.default php-fpm.conf
[root@3367881fd446 etc]# cd php-fpm.d/
[root@3367881fd446 php-fpm.d]# cp www.conf.default www.conf
# 启动php
[root@3367881fd446 php-fpm.d]# /usr/local/php8/sbin/php-fpm -c /usr/local/php8/etc/php-fpm.conf
[root@3367881fd446 etc]# 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@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f482b46f4f0 centos:latest "/bin/bash" 54 minutes ago Up 54 minutes php8
2102e9a88ce8 centos:latest "/bin/bash" 2 hours ago Up 2 hours mysql01
3367881fd446 yunjisuanlp/nginx:v1 "/usr/local/nginx/sb…" 2 hours ago Up 2 hours confident_wing
[root@localhost ~]# docker exec -it 3367881fd446 /bin/bash
[root@3367881fd446 /]# cd /usr/local/nginx/conf/
[root@3367881fd446 conf]# vi nginx.conf
..........
location / {
root html;
index index.php index.html index.htm; # 添加index.php
}
.........
location ~ \.php$ {
root /var/www/html; # 修改
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name; # 修改
include fastcgi_params;
}
........
[root@3367881fd446 conf]# nginx -s reload
# 在php容器内创建
mkdir -p /var/www/html
vi /var/www/html/index.php
# 修改php配置文件让其前台运行
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
yunjisuanlp/php8 v1 9d2f246b2038 4 hours ago 1.52GB
yunjisuanlp/mysql v1 77281e1fab7e 6 hours ago 3.78GB
yunjisuanlp/nginx v1 ac3319cbbafb 24 hours ago 550MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5b0f3e2d5032 yunjisuanlp/nginx:v1 "/usr/local/nginx/sb…" 2 hours ago Up 2 hours 0.0.0.0:80->80/tcp, :::80->80/tcp nginx88 # nginx正在运行
# 运行php容器进行修改
[root@localhost ~]# docker run -it --name php4 --network container:ac3319cbbafb 9d2f246b2038 /bin/bash
[root@5b0f3e2d5032 /]# 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:*
[root@5b0f3e2d5032 /]# vi /usr/local/php8/etc/php-fpm.conf
.......
daemonize = no # 让php前台运行
....
# 写php启动脚本
[root@5b0f3e2d5032 /]# vi start.sh
#!/bin/bash
/usr/local/php8/sbin/php-fpm -c /usr/local/php8/etc/php-fpm.conf
# 重新打开一个终端生成新的镜像以写的脚本启动
[root@5b0f3e2d5032 /]# docker commit -c 'CMD ["/start.sh"]' 9d2f246b2038 yunjisuanlp/php8:v2
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
yunjisuanlp/php8 v2 6d6316824a81 2 hours ago 1.52GB
yunjisuanlp/php8 v1 9d2f246b2038 5 hours ago 1.52GB
yunjisuanlp/mysql v1 77281e1fab7e 6 hours ago 3.78GB
yunjisuanlp/nginx v1 ac3319cbbafb 25 hours ago 550MB
centos latest 5d0da3dc9764 2 months ago 231MB
docker run -d --name php2 --netowkr container:5b0f3e2d5032 6d6316824a81
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
003c5d748ed1 a0f1c00cb752 "/start.sh" 4 seconds ago Up 3 seconds pgp
5b0f3e2d5032 yunjisuanlp/nginx:v3 "/usr/local/nginx/sb…" 2 hours ago Up 2 hours 0.0.0.0:80->80/tcp, :::80->80/tcp nginx88
访问