基于容器搭建lnmp架构
在docker环境中拉取最新的centos镜像,基于centos镜像来制作nginx、mysql、php镜像
[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 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 2 months ago 231MB
构建nginx镜像
基于centos镜像创建一个新容器命名为nginx
[root@docker ~]# docker run --name nginx -it centos
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e9be839be5ed centos "/bin/bash" 17 seconds ago Up 16 seconds nginx
在容器中编译安装nginx
[root@docker src]# docker cp nginx-1.20.2.tar.gz nginx:/usr/src
[root@e9be839be5ed /]# useradd -r -M -s /sbin/nologin nginx
[root@e9be839be5ed /]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
[root@e9be839be5ed /]# mkdir -p /var/log/nginx
[root@e9be839be5ed /]# chown -R nginx.nginx /var/log/nginx
[root@e9be839be5ed /]# cd /usr/src/
[root@e9be839be5ed src]# ls
debug kernels nginx-1.20.2.tar.gz
[root@e9be839be5ed src]# tar xf nginx-1.20.2.tar.gz
[root@e9be839be5ed src]# cd nginx-1.20.2
[root@e9be839be5ed nginx-1.20.2]# ls
CHANGES CHANGES.ru LICENSE README auto conf configure contrib html man src
[root@e9be839be5ed nginx-1.20.2]# ./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@e9be839be5ed nginx-1.20.2]# make && make install
再开一个终端制作nginx镜像并用该镜像运行容器测试
[root@docker ~]# docker commit -c 'CMD ["/bin/bash","/opt/nginx-start.sh"]' -p nginx 93quan/nginx:v1
sha256:c6c354211762480e4b501a0c401f065e6987910f536c3efd77d300e8c562e45f
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
93quan/nginx v1 c6c354211762 38 seconds ago 549MB
93quan/php8 v1 8470c6b46097 57 seconds ago 1.52GB
93quan/mysql v1 f7f5cd3137f3 2 hours ago 3.81GB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@docker ~]# docker run --name test -d 93quan/nginx:v1
b048cca10c08802082dabcd682da4b8e4e67bddf98d50c3305afa882d0763f0c
构建mysql镜像
基于centos镜像创建一个新容器命名为mysql
[root@docker ~]# docker run --name mysql -it centos
[root@docker src]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cf924d8e6410 centos "/bin/bash" 4 minutes ago Up 4 minutes mysql
e9be839be5ed centos "/bin/bash" 10 minutes ago Up 10 minutes nginx
在容器中编译安装mysql
[root@docker src]# docker cp mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz mysql:/usr/src/
[root@cf924d8e6410 /]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs libaio numactl
[root@cf924d8e6410 /]# useradd -r -M -s /sbin/nologin mysql
[root@cf924d8e6410 /]# cd /usr/src/
[root@cf924d8e6410 src]# ls
debug kernels mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
[root@cf924d8e6410 src]# tar xf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
[root@cf924d8e6410 src]# mv mysql-5.7.36-linux-glibc2.12-x86_64 /usr/local/mysql
[root@cf924d8e6410 local]# chown -R mysql.mysql /usr/local/mysql
[root@cf924d8e6410 local]# ls -ld /usr/local/mysql
drwxr-xr-x. 9 mysql mysql 142 Dec 3 11:05 /usr/local/mysql
[root@cf924d8e6410 /]# mkdir /opt/data
[root@cf924d8e6410 /]# chown -R mysql.mysql /opt/data
[root@cf924d8e6410 /]# ls -ld /opt/data/
drwxr-xr-x. 2 mysql mysql 6 Dec 3 11:20 /opt/data/
[root@cf924d8e6410 /]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data
2021-12-03T11:33:21.043710Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-12-03T11:33:21.388832Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-12-03T11:33:21.450001Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-12-03T11:33:21.507554Z 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: d1b28824-542c-11ec-bb11-0242ac110003.
2021-12-03T11:33:21.509081Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-12-03T11:33:22.755510Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2021-12-03T11:33:22.755575Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2021-12-03T11:33:22.756821Z 0 [Warning] CA certificate ca.pem is self signed.
2021-12-03T11:33:22.841559Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
[root@cf924d8e6410 /]# 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@cf924d8e6410 /]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@cf924d8e6410 /]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@cf924d8e6410 /]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
[root@cf924d8e6410 /]# vi opt/mysql-start.sh
#!/bin/bash
/etc/init.d/mysqld start
sleep 1
/usr/local/mysql/bin/mysql
[root@cf924d8e6410 /]# chmod +x opt/mysql-start.sh
再开一个终端制作mysql镜像并用该镜像运行容器测试
[root@docker ~]# docker commit -c 'CMD ["/opt/mysql-start.sh"]' -p mysql
sha256:f7f5cd3137f35f9706fd55d1690decefdb88ac00731f09fd66f4639806edfdd4
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> f7f5cd3137f3 54 seconds ago 3.81GB
93quan/nginx v1 baa21218ccd8 About an hour ago 549MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@docker ~]# docker tag f7f5cd3137f3 93quan/mysql:v1
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
93quan/mysql v1 f7f5cd3137f3 About a minute ago 3.81GB
93quan/nginx v1 baa21218ccd8 About an hour ago 549MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@docker ~]# docker run --name test2 -it 93quan/mysql:v1
Starting MySQL SUCCESS!
Logging to '/opt/data/6d8a513dfe74.err'.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 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>
构建php镜像
基于centos镜像创建一个新容器命名为php8
[root@docker ~]# docker run --name php8 -it centos
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
01e01a05481b centos "/bin/bash" 10 seconds ago Up 8 seconds php8
cf924d8e6410 centos "/bin/bash" 31 minutes ago Up 31 minutes mysql
e9be839be5ed centos "/bin/bash" 37 minutes ago Up 37 minutes nginx
在容器中编译安装php8
[root@docker src]# docker cp php-8.0.12.tar.gz php8:/usr/src/
[root@01e01a05481b /]# yum -y install epel-release
[root@01e01a05481b /]# yum -y install sqlite-devel libzip-devel 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 readline readline-devel libxslt libxslt-devel oniguruma gcc gcc-c++ make libcurl-devel
[root@01e01a05481b /]# 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@01e01a05481b /]# cd /usr/src/
[root@01e01a05481b src]# tar xf php-8.0.12.tar.gz
[root@01e01a05481b src]# ls
debug kernels php-8.0.12 php-8.0.12.tar.gz
[root@01e01a05481b 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@01e01a05481b php-8.0.12]# make && make install
[root@01e01a05481b php-8.0.12]# /usr/local/php8/bin/php -v
PHP 8.0.12 (cli) (built: Dec 3 2021 13:15:17) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.12, Copyright (c) Zend Technologies
[root@01e01a05481b ~]# cp -f /usr/src/php-8.0.12/php.ini-production /etc/php.ini
[root@01e01a05481b ~]# cp -f /usr/src/php-8.0.12/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@01e01a05481b ~]# chmod +x /etc/init.d/php-fpm
[root@01e01a05481b ~]# cp -f /usr/local/php8/etc/php-fpm.conf.default /usr/local//php8/etc/php-fpm.conf
[root@01e01a05481b ~]# cp -f /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
[root@01e01a05481b /]# vi /usr/local/php8/etc/php-fpm.conf
...
daemonize = no
...
[root@01e01a05481b /]# vi /opt/start.sh
#!/bin/bash
/etc/init.d/php-fpm start
sleep 1
[root@01e01a05481b /]# chmod +x /opt/start.sh
再开一个终端制作php8镜像并用该镜像运行容器测试
[root@docker ~]# docker commit -c 'CMD ["/bin/bash","/opt/start.sh"]' -p php8 93quan/php8:v1
sha256:8470c6b46097627e61e9608c69e9bef8e9e910f532d151c9ba89c3aa450f9619
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
93quan/nginx v1 c6c354211762 Less than a second ago 549MB
93quan/php8 v1 8470c6b46097 19 seconds ago 1.52GB
93quan/mysql v1 f7f5cd3137f3 2 hours ago 3.81GB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@docker ~]# docker run --name test3 -it 93quan/php8:v1
[03-Dec-2021 14:53:29] NOTICE: fpm is running, pid 7
[03-Dec-2021 14:53:29] NOTICE: ready to handle connections
组合
修改nginx配置文件,制作v2版本
[root@e9be839be5ed /]# 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@docker ~]# docker commit -c 'CMD ["/bin/bash","/opt/nginx-start.sh"]' -p nginx 93quan/nginx:v2
sha256:7d4582f48ab8581dba1af5bbbfb2c4f92f769bba4b837bc7c71d6dea71ecfb4c
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
93quan/nginx v2 7d4582f48ab8 28 seconds ago 549MB
93quan/php8 v1 8aaa4281bd90 9 minutes ago 1.52GB
93quan/nginx v1 c6c354211762 39 minutes ago 549MB
93quan/mysql v1 f7f5cd3137f3 3 hours ago 3.81GB
centos latest 5d0da3dc9764 2 months ago 231MB
制作v2版本php镜像
[root@docker ~]# docker exec -it php8 /bin/bash
[root@01e01a05481b /]# mkdir -p /var/www/html
[root@01e01a05481b /]# vi /var/www/html/index.php
<?php
phpinfo();
?>
[root@docker ~]# docker commit -c 'CMD ["/bin/bash","/opt/start.sh"]' -p php8 93quan/php8:v2
sha256:2dbd04ad88f598050a48a9c0055deed69b607abce5c2a8d1c1246d17bfca8296
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
93quan/php8 v2 2dbd04ad88f5 26 seconds ago 1.52GB
93quan/nginx v2 7d4582f48ab8 4 minutes ago 549MB
93quan/php8 v1 8aaa4281bd90 13 minutes ago 1.52GB
93quan/nginx v1 c6c354211762 43 minutes ago 549MB
93quan/mysql v1 f7f5cd3137f3 3 hours ago 3.81GB
centos latest 5d0da3dc9764 2 months ago 231MB
使用container模式使容器间通信
[root@docker ~]# docker run -itd --name nginx-1 -p 80:80 93quan/nginx:v2
b9a1916e94545cb4017f77965746f2f5d67be759ab1a283ce44918fe6fac17cd
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b9a1916e9454 93quan/nginx:v2 "/bin/bash /opt/ngin…" 6 seconds ago Up 5 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx-1
01e01a05481b centos "/bin/bash" 4 hours ago Up 7 minutes php8
e9be839be5ed centos "/bin/bash" 4 hours ago Up 16 minutes nginx
[root@docker ~]# docker run -d --name php8-1 --network container:nginx-1 93quan/php8:v2
79a562e84fa803c877dc45bde01781f2ab5306fe7b3b2801b38ae9b2af069b83
[root@docker ~]# 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 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
到web界面访问测试
|