基于容器的方式制作分离lnmp镜像

nginx镜像

#拉取镜像
[root@localhost ~]# 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


#创建容器,设置端口映射进入容器,编译安装nginx
[root@localhost ~]# docker run --name  centos01 -it  centos:7 
[root@b01836a30980 /]# 
[root@b01836a30980 /]# useradd -r -M -s /sbin/nologin nginx
[root@b01836a30980 /]# id nginx
uid=999(nginx) gid=998(nginx) groups=998(nginx)
[root@b01836a30980 /]#  yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@b01836a30980 /]# mkdir -p /var/log/nginx
[root@b01836a30980 /]# chown -R nginx.nginx /var/log/nginx

#另起一个终端,然后执行命令传输nginx源码包
[root@localhost ~]# docker cp /usr/src/nginx-1.18.0.tar.gz  centos01:/usr/src


#回到原来的终端,继续编译安装nginx
[root@b01836a30980 /]#  cd /usr/src/
[root@b01836a30980 src]# ls
debug  kernels  nginx-1.18.0.tar.gz
[root@b01836a30980 src]# tar xf nginx-1.18.0.tar.gz 
[root@b01836a30980 src]# ls
debug  kernels  nginx-1.18.0  nginx-1.18.0.tar.gz
[root@b01836a30980 src]# cd nginx-1.18.0
[root@b01836a30980 nginx-1.18.0]# ls
CHANGES  CHANGES.ru  LICENSE  README  auto  conf  configure  contrib  html  man  src
[root@b01836a30980 nginx-1.18.0]#  ./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@b01836a30980 ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@b01836a30980 ~]# . /etc/profile.d/nginx.sh



#没有ss命令,安装下列软件包
[root@b01836a30980 ~]# yum install iproute iproute-doc
[root@b01836a30980 ~]# ss -antl
State      Recv-Q Send-Q                                        Local Address:Port                                                       Peer Address:Port              
LISTEN     0      128                                                       *:80                                                                    *:*               

#配置php
[root@b01836a30980  ~]# 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;		//修改
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;		//修改
            include        fastcgi_params;
        }
...


#编写启动脚本
[root@b01836a30980 /]# mkdir /scripts/
[root@b01836a30980 /]# 
[root@b01836a30980 /]# cd /scripts/
[root@b01836a30980 scripts]# ls
[root@b01836a30980 scripts]# touch start.sh
[root@b01836a30980 scripts]# chmod +x start.sh
[root@b01836a30980 scripts]# vi start.sh
[root@b01836a30980 /]# cat /scripts/start.sh 
#!/bin/bash
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
/bin/bash



[root@localhost ~]# docker commit -a 'csl <593582553@qq.com>' -c 'CMD ["/scripts/start.sh"]' -p 8013bb7a62fe bravealove1/nginx:v1.18


[root@localhost ~]# docker images 
REPOSITORY          TAG       IMAGE ID       CREATED        SIZE
bravealove1/nginx   v1.18     6e04ced64dd4   2 hours ago    523MB
centos              7         eeb6ee3f44bd   2 months ago   204MB
centos              latest    5d0da3dc9764   2 months ago   231MB


[root@localhost ~]# docker run --name nginx -dit -p 80:80  bravealove1/nginx:v1.18
08552d2ba061031f8ade88703c83a66e98198f7e0369af406852cf6ae3daea2d

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                       COMMAND               CREATED       STATUS          PORTS                               NAMES
08552d2ba061   bravealove1/nginx:v1.18     "/scripts/start.sh"   2 hours ago   Up 2 hours      0.0.0.0:80->80/tcp, :::80->80/tcp   nginx



[root@localhost ~]# docker exec -it nginx  /bin/bash
[root@08552d2ba061 ~]# ss -antl
State      Recv-Q Send-Q                                        Local Address:Port                                                       Peer Address:Port              
LISTEN     0      128                                                       *:80                                                                    *:*                  

mysql镜像

#创建容器,编译安装mysql
[root@localhost ~]# docker run --name centos02 -dit centos:7 /bin/bash
eab87d65300edd0aff341f7279e2b8498d41ec4918c4addf54cfaed509037a4f

[root@localhost ~]# docker exec -it centos02 /bin/bash
[root@eab87d65300e /]# 
[root@eab87d65300e /]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs
[root@eab87d65300e /]# groupadd -r  mysql
[root@eab87d65300e /]# useradd -r -M -s /sbin/nologin -g mysql mysql

#另起一个终端,上传mysql软件包
[root@localhost ~]# cd /usr/src
[root@localhost src]# docker cp mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz  eab87d65300e:/usr/src/


#解压
[root@eab87d65300e /]# cd /usr/src
[root@eab87d65300e src]# ls
debug  kernels  mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
[root@eab87d65300e src]# tar xf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@eab87d65300e src]# cd /usr/local/
[root@eab87d65300e local]# ln -sv mysql-5.7.35-linux-glibc2.12-x86_64/ mysql
[root@eab87d65300e local]# chown -R mysql.mysql /usr/local/mysql*
[root@eab87d65300e local]# ll
total 0
drwxr-xr-x. 2 root  root    6 Apr 11  2018 bin
drwxr-xr-x. 2 root  root    6 Apr 11  2018 etc
drwxr-xr-x. 2 root  root    6 Apr 11  2018 games
drwxr-xr-x. 1 root  root   19 Dec  3 09:41 include
drwxr-xr-x. 2 root  root    6 Apr 11  2018 lib
drwxr-xr-x. 2 root  root    6 Apr 11  2018 lib64
drwxr-xr-x. 2 root  root    6 Apr 11  2018 libexec
lrwxrwxrwx. 1 mysql mysql  36 Dec  3 09:33 mysql -> mysql-5.7.35-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 mysql mysql 129 Dec  3 09:32 mysql-5.7.35-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root  root    6 Apr 11  2018 sbin
drwxr-xr-x. 5 root  root   49 Nov 13  2020 share
drwxr-xr-x. 2 root  root    6 Apr 11  2018 src

#添加环境变量
[root@eab87d65300e /]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@eab87d65300e /]# . /etc/profile.d/mysql.sh
[root@eab87d65300e /]# echo $PATH
/usr/local/mysql/bin:/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

#创建数据存放目录
[root@eab87d65300e /]# mkdir -p /opt/data
[root@eab87d65300e /]# chown -R mysql.mysql /opt/data/

#初始化数据库
[root@eab87d65300e /]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/
2021-12-03F18:48:41.261781Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-12-03F18:48:41.438873Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-12-03F18:48:41.459162Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-12-03F18:48:41.527098Z 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: 0d70e7f6-1c4b-11ec-a20e-000c2984a301.
2021-12-03F18:48:41.528205Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-12-03F18:48:42.036191Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2021-12-03F18:48:42.036204Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2021-12-03F18:48:42.036605Z 0 [Warning] CA certificate ca.pem is self signed.
2021-12-03F18:48:42.238241Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.




[root@eab87d65300e /]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
‘/usr/local/include/mysql’ -> ‘/usr/local/mysql/include/’
[root@eab87d65300e /]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@eab87d65300e /]# ldconfig     

#生成配置文件
[root@eab87d65300e /]# cat > /etc/my.cnf <<EOF
[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
EOF

#配置服务启动文件
[root@eab87d65300e /]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /usr/local/mysql/support-files/mysql.server
[root@eab87d65300e /]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /usr/local/mysql/support-files/mysql.server

[root@eab87d65300e /]# /usr/local/mysql/support-files/mysql.server start
[root@eab87d65300e ~]# ss -antl
State      Recv-Q Send-Q                                        Local Address:Port                                                       Peer Address:Port              
LISTEN     0      80                                                     [::]:3306                                                               [::]:*                  

[root@eab87d65300e /]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35 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> 

#设置新密码
mysql> set password = password('123');
Query OK, 0 rows affected, 1 warning (0.00 sec)

#编写启动脚本
[root@eab87d65300e /]# mkdir /scripts/
[root@eab87d65300e /]#  
[root@eab87d65300e /]#  cd /scripts/
[root@eab87d65300e scripts]# ls
[root@eab87d65300e scripts]# touch start.sh
[root@eab87d65300e scripts]# chmod +x start.sh
[root@eab87d65300e scripts]# vi start.sh
[root@eab87d65300e /]# cat /scripts/start.sh 
#!/bin/bash
/usr/local/mysql/support-files/mysql.server start
/bin/bash

[root@localhost ~]# docker commit -a 'csl <593582553@qq.com>' -c 'CMD ["/scripts/start.sh"]' -p eab87d65300e bravealove1/mysql:v5.7.35


[root@localhost ~]# docker images 
[root@localhost src]# docker images 
REPOSITORY          TAG       IMAGE ID       CREATED        SIZE
bravealove1/mysql   v5.7.35   3214668daf54   2 hours ago    3.87GB
bravealove1/nginx   v1.18     6e04ced64dd4   3 hours ago    523MB
centos              7         eeb6ee3f44bd   2 months ago   204MB
centos              latest    5d0da3dc9764   2 months ago   231MB


[root@localhost ~]# docker run --name mysql -dit --net=container:nginx  bravealove1/mysql:v5.7.35 
b665c3009eba1c1b44f650de74b5761fa93f2cf2536127b46fc6525efd4b044d


[root@localhost src]# docker ps
CONTAINER ID   IMAGE                       COMMAND               CREATED          STATUS             PORTS                                   NAMES
b665c3009eba   bravealove1/mysql:v5.7.35   "/scripts/start.sh"   2 hours ago      Up 2 hours                                                 mysql
08552d2ba061   bravealove1/nginx:v1.18     "/scripts/start.sh"   3 hours ago      Up 2 hours         0.0.0.0:80->80/tcp, :::80->80/tcp       nginx


[root@localhost ~]# docker exec -it mysql  /bin/bash
[root@b665c3009eba ~]# ss -antl
State      Recv-Q Send-Q                                        Local Address:Port                                                       Peer Address:Port              
LISTEN     0      128                                                       *:80                                                                    *:*                  
LISTEN     0      80                                                     [::]:3306                                                               [::]:*                  





可能会遇到的报错
在初始化mysql5.7的时候,报以下错误
error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory


解决方法
yum -y install numactl.x86_64
yum install -y libaio.x86_64




php镜像

#首先下载epel源
[root@ffd26d85f5bf /]# yum -y install epel-release
#下载依赖环境
[root@ffd26d85f5bf /]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@ffd26d85f5bf /]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-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   

#yum源oniguruma包版本过低使用下列方法
[root@ffd26d85f5bf /]# yum -y install http://repo.almalinux.org/almalinux/8/AppStream/x86_64/os/Packages/oniguruma-6.8.2-2.el8.x86_64.rpm
[root@ffd26d85f5bf /]# yum -y install http://repo.almalinux.org/almalinux/8/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm


#另起一个终端,传输php软件包
[root@localhost ~]# cd /usr/src
[root@localhost src]# docker cp php-7.4.24.tar.gz ffd26d85f5bf:/usr/src/



#回到终端继续
[root@ffd26d85f5bf /]# cd /usr/src/
[root@ffd26d85f5bf src]# 
[root@ffd26d85f5bf src]# tar -xvf  php-7.4.24.tar.gz
[root@ffd26d85f5bf src]# cd php-7.4.24
[root@ffd26d85f5bf php-7.4.24]#  ./configure --prefix=/usr/local/php7  \
--with-config-file-path=/etc \
--enable-fpm \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif  \
--enable-ftp \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir \
--with-gettext \
--enable-json \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix

[root@ffd26d85f5bf php-7.4.24]# make -j $(cat /proc/cpuinfo |grep processor|wc -l)
[root@ffd26d85f5bf php-7.4.24]# make install

#配置环境变量
[root@ffd26d85f5bf ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php.sh
[root@ffd26d85f5bf ~]# source /etc/profile.d/php.sh

#生成配置文件
[root@ffd26d85f5bf ~]# cd /usr/src/php-7.4.24
[root@ffd26d85f5bf php-7.4.24]# cp php.ini-production  /etc/php.ini
[root@ffd26d85f5bf php-7.4.24]# cp sapi/fpm/init.d.php-fpm  /etc/init.d/php-fpm
[root@ffd26d85f5bf ~]# chmod +x /etc/rc.d/init.d/php-fpm
[root@ffd26d85f5bf ~]# cd /usr/local/php7/etc/
[root@ffd26d85f5bf etc]# cp php-fpm.conf.default  php-fpm.conf
[root@ffd26d85f5bf etc]# cd php-fpm.d/
[root@ffd26d85f5bf php-fpm.d]# cp www.conf.default  www.conf

[root@ffd26d85f5bf ~]# /usr/local/php7/sbin/php-fpm
[root@ffd26d85f5bf ~]# ss -antl
State      Recv-Q Send-Q                                        Local Address:Port                                                       Peer Address:Port              
LISTEN     0      128                                               127.0.0.1:9000                                                                  *:*                  

#编写启动脚本
[root@ffd26d85f5bf ~]# mkdir  /scripts/
[root@ffd26d85f5bf ~]# cd /scripts/
[root@ffd26d85f5bf scripts]# touch start.sh
[root@ffd26d85f5bf scripts]# chmod +x start.sh 
[root@ffd26d85f5bf scripts]# ls
start.sh
[root@ffd26d85f5bf scripts]# vi start.sh
#!/bin/bash
/usr/local/php7/sbin/php-fpm
/bin/bash

[root@ffd26d85f5bf ~]#  mkdir  -p /var/www/html
[root@ffd26d85f5bf ~]# cd /var/www/html
[root@ffd26d85f5bf html]# vi index.php
<?php
  phpinfo();
?>



[root@localhost ~]# docker commit -a 'csl <593582553@qq.com>' -c 'CMD ["/scripts/start.sh"]' -p  ffd26d85f5bf   bravealove1/php:v7.24
sha256:b23aeee1d942a6b7f24d6f47d1e31f2680a8dc175a356da79d85053a3244ce41
[root@localhost ~]# docker images 
REPOSITORY          TAG       IMAGE ID       CREATED          SIZE
bravealove1/php     v7.24     b23aeee1d942   27 seconds ago   1.45GB
bravealove1/mysql   v5.7.35   3214668daf54   3 hours ago      3.87GB
bravealove1/nginx   v1.18     6e04ced64dd4   4 hours ago      523MB
centos              7         eeb6ee3f44bd   2 months ago     204MB
centos              latest    5d0da3dc9764   2 months ago     231MB





[root@localhost ~]# docker images 
REPOSITORY          TAG       IMAGE ID       CREATED             SIZE
bravealove1/php     v7.24     b23aeee1d942   2 hours ago         1.45GB
bravealove1/mysql   v5.7.35   3214668daf54   5 hours ago         3.87GB
bravealove1/nginx   v1.18     6e04ced64dd4   5 hours ago         523MB
centos              7         eeb6ee3f44bd   2 months ago        204MB
centos              latest    5d0da3dc9764   2 months ago        231MB



[root@localhost ~]# docker run --name php -dit --net=container:nginx  bravealove1/php:v7.24 
8810bff62c479a129777c724bf4f25c48a231bf079dee5837f7750a627b96e12

[root@localhost src]# docker ps
CONTAINER ID   IMAGE                       COMMAND               CREATED          STATUS             PORTS                                   NAMES
b665c3009eba   bravealove1/mysql:v5.7.35   "/scripts/start.sh"   2 hours ago      Up 2 hours                                                 mysql
08552d2ba061   bravealove1/nginx:v1.18     "/scripts/start.sh"   3 hours ago      Up 2 hours         0.0.0.0:80->80/tcp, :::80->80/tcp       nginx
8810bff62c47   bravealove1/php:v7.24       "/scripts/start.sh"   46 minutes ago   Up 19 minutes                                           php

[root@localhost ~]# docker exec -it php /bin/bash
[root@8810bff62c47 /]# ss -antl
State      Recv-Q Send-Q                                        Local Address:Port                                                       Peer Address:Port              
LISTEN     0      128                                                       *:80                                                                    *:*                  
LISTEN     0      128                                               127.0.0.1:9000                                                                  *:*                  
LISTEN     0      80                                                     [::]:3306                                                               [::]:*                  

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值