基于容器搭建lamp架构
项目描述
使用Docker容器基于centos镜像分别制作nginx镜像,mysql镜像和php镜像使用编译安装的方式,最后通过镜像启动成容器时使用container模式网络模式并访问到php测试页面
准备工作
在docker环境中拉取最新的centos镜像,基于centos镜像来搭建lnmp架构
//拉取centos镜像
[root@docker ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@docker ~]#
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest ad17c88403e2 13 days ago 143MB
nginx latest ea335eea17ab 2 weeks ago 141MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@docker ~]#
1 构建nginx镜像
1.1 基于centos镜像创建一个新容器命名为nginx
//创建容器
[root@docker ~]# docker run --name nginx 5d0da3dc9764
[root@docker ~]# docker exec -it 0133fa0bf147 /bin/bash
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0133fa0bf147 centos "/bin/bash" 29 hours ago Up 1 minutes nginx
1.2 在容器对nginx进行编译安装
//另起一个终端,然后执行命令把本地nginx包传输到centos容器中
[root@docker ~]# docker cp /opt/nginx-1.20.1.tar.gz 0133fa0bf147:/usr/src/
//在容器中下载编译工具和依赖包
[root@0133fa0bf147 /]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
//在容器里面创建nginx用户
[root@0133fa0bf147 /]# useradd -r -M -s /sbin/nologin nginx
[root@0133fa0bf147 /]# id nginx
uid=998(nginx) gid=996(nginx) groups=996(nginx)
//创建日志服务
[root@0133fa0bf147 /]# mkdir -p /var/log/nginx
[root@0133fa0bf147 /]# chown -R nginx.nginx /var/log/nginx/
//在容器中进行编译安装
[root@0133fa0bf147 /]# cd /usr/src/
[root@0133fa0bf147 src]# tar xf nginx-1.20.1.tar.gz
[root@0133fa0bf147 src]# ls
debug kernels nginx-1.20.1 nginx-1.20.1.tar.gz
[root@0133fa0bf147 nginx-1.20.1]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@0133fa0bf147 nginx-1.20.1]# make && make install
// 启动nginx
[root@0133fa0bf147 ~]# /usr/local/nginx/sbin/nginx
[root@0133fa0bf147 ~]# 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@0133fa0bf147 ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@0133fa0bf147 ~]# . /etc/profile.d/nginx.sh
//编写一个nginx启动文件,以便于用生成的nginx镜像去创建容器时,nginx是开启状态
[root@0133fa0bf147 ~]# vi /start.sh
[root@0133fa0bf147 ~]# cat /start.sh
#!/bin/sh
/usr/local/nginx/sbin/nginx
/bin/bash
[root@0133fa0bf147 ~]# chmod +x /start.sh
//修改nginx配置文件
[root@0133fa0bf147 ~]#vi /usr/local/nginx/conf/nginx.conf
......
location / {
root html;
index index.php index.html index.htm; //加入index.php
}
......
location ~ \.php$ {
root /var/www/html; //修改为php访问页面
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name; //修改
include fastcgi_params;
}
......
//重新加载配置文件
[root@2a73e41f9c08 ~]# nginx -s reload
1.3 制作nginx镜像,并将生成的镜像做编译安装
在创建镜像时,我们不能关闭容器,必须使其处于运行状态,所以我们必须要另起一个终端,然后执行
//创建nginx镜像
[root@docker ~]# docker commit -p -c 'CMD ["/bin/bash","/start.sh"]' 0133fa0bf147 caiaoc/nginx:v1.0
sha256:2df3cf34d50610c80f7330d3f81bacec15afaaecf5c0fe31572925409de7c225
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
caiaoc/nginx v1.0 c2834d859617 22 hours ago 549MB
httpd latest ad17c88403e2 2 weeks ago 143MB
nginx latest ea335eea17ab 2 weeks ago 141MB
centos latest 5d0da3dc9764 2 months ago 231MB
//用创建的nginx镜像再启动一个容器
[root@docker ~]# docker run -itd --name n1 caiaoc/nginx:v1.0
704a324f8208fa6bc9bffbef2aded046987280712bac55965e2012ee7d750f18
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
704a324f8208 caiaoc/nginx:v1.0 "/bin/bash /start.sh" 7 seconds ago Up 6 seconds g2
0133fa0bf147 centos "/bin/bash" 29 hours ago Up 33 minutes nginx
[root@docker ~]#
//进入nginx容器,查看nginx是否是启动的
[root@docker ~]# docker exec -it n1 /bin/bash
[root@704a324f8208 /]# 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:*
2构建mysql镜像
2.1 基于centos镜像创建一个新容器命名为mysql
[root@docker ~]# docker run --name mysql -it centos
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8dda2d73cb30 centos "/bin/bash" 5 hours ago Up 2 hours mysql
0133fa0bf147 centos "/bin/bash" 30 hours ago Up 1 hours nginx
[root@docker ~]#
2.2在容器中编译安装mysql
//在容器中安装mysql需要的依赖包
[root@8dda2d73cb30 ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs
//创建mysql用户和组
[root@8dda2d73cb30 ~]# useradd -r -M -s /sbin/nologin mysql
//将本地mysql包传到容器中
[root@docker ~]# docker cp /opt/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz 8dda2d73cb30:/usr/src/
//解压mysql包
[root@8dda2d73cb30 ~]# cd /usr/src/
[root@8dda2d73cb30 src]# tar xf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@8dda2d73cb30 src]# cd /usr/local/
[root@8dda2d73cb30 local]# ls
bin games lib libexec sbin src
etc include lib64 mysql-5.7.35-linux-glibc2.12-x86_64 share
//做软连接
[root@8dda2d73cb30 local]# ln -sv mysql-5.7.35-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.35-linux-glibc2.12-x86_64/'
[root@8dda2d73cb30 local]#
//修改属主和属组
[root@8dda2d73cb30 local]# chown -R mysql.mysql /usr/local/mysql*
//设置环境变量
[root@8dda2d73cb30 ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@8dda2d73cb30 ~]# . /etc/profile.d/mysql.sh
//头文件软连接
[root@8dda2d73cb30 ~]# ln -s /usr/local/mysql/include /usr/include/mysql
//库文件软连接
[root@8dda2d73cb30 ~]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@8dda2d73cb30 ~]#
//建立数据存放目录
[root@8dda2d73cb30 ~]# mkdir -p /opt/data
[root@8dda2d73cb30 ~]# chown -R mysql.mysql /opt/data/
//初始化数据库,在容器中做初始化需下载下面两个包
[root@8dda2d73cb30 bin]# yum -y install libaio*
[root@8dda2d73cb30 bin]# yum -y install numactl.x86_64
[root@8dda2d73cb30 bin]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data
//生成配置文件
[root@8dda2d73cb30 bin]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /data
socket = /tmp/mysql.sock
port = 3306
pid-file = /data/mysql.pid
user = mysql
skip-name-resolve
[root@8dda2d73cb30 bin]#
//配置启动服务
[root@8dda2d73cb30 bin]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /usr/local/mysql/support-files/mysql.server
[root@8dda2d73cb30 bin]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /usr/local/mysql/support-files/mysql.server
//启动MySQL
[root@8dda2d73cb30 /]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL SUCCESS!
root@8dda2d73cb30 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 80 *:3306 *:*
[root@8dda2d73cb30 ~]#
//编写一个mysql的启动脚本
[root@8dda2d73cb30 ~]# cat ../start.sh
#!/bin/sh
/usr/local/mysql/support-files/mysql.server start
/bin/bash
[root@8dda2d73cb30 ~]# chmod +x ../start.sh
2.3 再次开启一个终端制作mysql镜像
在创建镜像时,我们不能关闭容器,必须使其处于运行状态,所以我们必须要另起一个终端,然后执行
[root@docker ~]# docker commit -p -c 'CMD ["/bin/bash","/start.sh"]'
8dda2d73cb30 caiaoc/mysql:v1.0
c083bb9b443234ecc621448783cb640c5790aeec0ee17311a2b60496d679f834
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
caiaoc/mysql v1.0 9ba0d2147bb4 About an hour ago 3.94GB
caiaoc/nginx v1.0 c2834d859617 22 hours ago 549MB
httpd latest ad17c88403e2 2 weeks ago 143MB
nginx latest ea335eea17ab 2 weeks ago 141MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@docker ~]# docker run -itd --name m1 9ba0d2147bb493 caiaoc/mysql:v1.0
[root@24e92ea9885e /]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 80
3 构建php镜像
3.1基于centos镜像创建一个新容器命名为php
[root@docker ~]# docker run --name php -it centos
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED About
fa7ad2a9ee0f 5d0da3dc9764 "/bin/bash" 3 hours ago Up 3 hours php
8dda2d73cb30 centos "/bin/bash" 6 hours ago Up 2 hours mysql
0133fa0bf147 centos "/bin/bash" 30 hours ago Up 1 hours nginx
[root@docker ~]#
3.2 在容器中编译安装php
//下载epel-release源
[root@28f17de6e853 /]# yum -y install epel-release
[root@28f17de6e853 /]# 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
//下载php所需依赖包
//因为libcurl-minimal-7.61.1-18.el8.x86_64 与 libcurl-7.61.1-22.el8.x86_64 提供的 libcurl(x86-64) 冲突 (所以得分开下载)
[root@28f17de6e853 /]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libicu-devel libjpeg libjpeg-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 php-mysqlnd libzip-devel libsqlite3x libsqlite3x-devel oniguruma libzip-devel gcc gcc-c++ make
[root@28f17de6e853 /]# yum -y install libcurl-devel
//把包传到容器中
[root@docker ~]# docker cp php-7.4.24.tar.xz fa7ad2a9ee0f:/usr/src/
//编译安装php
[root@28f17de6e853 /]# cd /usr/src/
[root@28f17de6e853 /]# ls
debug kernels php-7.4.24.tar.xz
[root@28f17de6e853 /]# tar xf php-7.4.24.tar.xz
[root@28f17de6e853 /]# ls
debug kernels php-7.4.24 php-7.4.24.tar.xz
[root@28f17de6e853 src]# cd php-7.4.24
[root@28f17de6e853 php-7.4.24]# ./configure --prefix=/usr/local/php \
--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@28f17de6e853 /]# make && make install
//设置环境变量
[root@fa7ad2a9ee0f php-7.4.24]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@fa7ad2a9ee0f php-7.4.24]# . /etc/profile.d/php.sh
//配置php-fpm
[root@bab883f85a33 php-7.4.24]# cp php.ini-production /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[root@bab883f85a33 php-7.4.24]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@bab883f85a33 php-7.4.24]# chmod +x /etc/init.d/php-fpm
[root@bab883f85a33 php-7.4.24]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@bab883f85a33 php-7.4.24]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[root@bab883f85a33 php-7.4.24]#
//在php容器中编写一个php访问页面
[root@bab883f85a33 ~]# mkdir -p /var/www/html
[root@bab883f85a33 ~]# vi /var/www/html/index.php
[root@bab883f85a33 ~]# cat /var/www/html/index.php
<?php
phpinfo()
?>
[root@bab883f85a33 ~]#
//启动php
[root@fa7ad2a9ee0f bin]# /usr/local/php/sbin/php-fpm
[root@fa7ad2a9ee0f bin]# 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:*
//编写一个php启动脚本
[root@fa7ad2a9ee0f /]# cat /start.sh
#!/bin/sh
/usr/local/php/sbin/php-fpm
/bin/bash
[root@fa7ad2a9ee0f /]#
[root@fa7ad2a9ee0f /]# chmod +x /start.sh
3.3 创建php镜像
//创建php镜像
[root@docker ~]# docker commit -p -c 'CMD ["/bin/bash","/start.sh"]' fa7ad2a9ee0f caicaoc/php:v1.0
sha256:f7e7503087bbb9d17aa93f0ec4d1c3e067f6e4f9edfab5243624feb916c88540
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
caicaoc/php v1.0 f7e7503087bb 5 seconds ago 1.46GB
caiaoc/mysql v1.0 9ba0d2147bb4 About an hour ago 3.94GB
caiaoc/nginx v1.0 c2834d859617 22 hours ago 549MB
httpd latest ad17c88403e2 2 weeks ago 143MB
nginx latest ea335eea17ab 2 weeks ago 141MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@docker ~]#
4 使用container模式使容器间通信
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker ~]#
[root@docker ~]# docker run -itd --name nginx01 -p 80:80 caiaoc/nginx:v1.0
eeb33778322cb71a4fa764b7530b2373adba607e2831f583f8f909af1488d42d
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b0fd334b82c a985a6973aec "/bin/bash /start.sh" 3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx01
[root@docker ~]#
[root@docker ~]# docker exec -it nginx01 /bin/bash
[root@1b0fd334b82c /]# 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@1b0fd334b82c /]#
[root@docker ~]# docker run -dit --name mysql01 --network container:1b0fd334b82c caiaoc/mysql:v1.0
dadc041fcc66148daf75d065b0d65143a2bea5de6c5393af5f0cf35abb5c1f1f
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dadc041fcc66 caiaoc/mysql:v1.0 "/bin/bash /start.sh" 17 seconds ago Up 15 seconds mysql01
1b0fd334b82c a985a6973aec "/bin/bash /start.sh" 3 minutes ago Up 3 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nginx01
[root@docker ~]#
[root@docker ~]# docker exec -it mysql01 /bin/bash
[root@1b0fd334b82c /]# 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@docker ~]# docker run -dit --name php01 --network container:1b0fd334b82c caiaoc/php:v1.0
9741c932f54db519020799310f20794dbc3eb8c671a18d25e617197c843a7f50
[root@docker ~]#
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9741c932f54d caiaoc/php:v1.0 "/bin/bash /start.sh" 11 seconds ago Up 10 seconds php01
dadc041fcc66 caiaoc/mysql:v1.0 "/bin/bash /start.sh" 5 minutes ago Up 5 minutes mysql01
1b0fd334b82c a985a6973aec "/bin/bash /start.sh" 9 minutes ago Up 9 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nginx01
[root@docker ~]#
[root@docker ~]# docker exec -it php01 /bin/bash
[root@1b0fd334b82c /]# 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@1b0fd334b82c /]#
访问页面测试