基于docker搭建lnmp

本文详细记录了如何在Docker环境下,基于CentOS镜像分别构建Nginx、MySQL和PHP镜像,并进行集成测试。首先拉取最新版的CentOS镜像,然后通过`docker run`创建并启动容器,安装必要的软件和库,编译安装Nginx、MySQL和PHP,最后通过`docker commit`将容器保存为镜像,并使用新镜像启动新的容器进行验证。
摘要由CSDN通过智能技术生成

在docker环境中拉取最新的centos镜像,基于centos镜像来制作nginx、mysql、php镜像

[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
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
centos       latest    5d0da3dc9764   2 months ago   231MB

nginx

//基于centos镜像创建一个新容器命名为nginx
[root@localhost ~]# docker run --name nginx -it centos
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED              STATUS                      PORTS     NAMES
f9c82b62551d   centos    "/bin/bash"   About a minute ago   Exited (0) 45 seconds ago             nginx

[root@localhost ~]# docker start nginx
nginx
[root@localhost ~]# docker exec -it nginx /bin/bash
[root@f9c82b62551d /]# ls
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr

//在容器中创建nginx用户并安装所需依赖
[root@f9c82b62551d /]# useradd -r -M -s /sbin/nologin nginx
[root@f9c82b62551d /]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make

[root@f9c82b62551d /]#  mkdir -p /var/log/nginx
[root@f9c82b62551d /]# chown -R nginx.nginx /var/log/nginx
[root@f9c82b62551d /]# wget http://nginx.org/download/nginx-1.20.2.tar.gz

[root@f9c82b62551d /]# tar xf nginx-1.20.2.tar.gz 
[root@f9c82b62551d /]# cd nginx-1.20.2
[root@f9c82b62551d 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@f9c82b62551d nginx-1.20.2]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@f9c82b62551d nginx-1.20.2]# . /etc/profile.d/nginx.sh 
[root@f9c82b62551d nginx-1.20.2]# nginx
[root@f9c82b62551d nginx-1.20.2]# 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@localhost ~]# docker commit -c 'CMD ["/bin/bash","/opt/nginx-start.sh"]' -p nginx weixiaoya/nginx:v0.1
sha256:8c6b4e2ac8a62b606e6e71035a4b5a23f70a5edf0f2a67b1d1e0fefffc0749be
[root@localhost ~]# docker images
REPOSITORY        TAG       IMAGE ID       CREATED         SIZE
weixiaoya/nginx   v0.1      8c6b4e2ac8a6   9 seconds ago   550MB
centos            latest    5d0da3dc9764   2 months ago    231MB
[root@localhost ~]# docker run --name test -d weixiaoya/nginx:v0.1 
a8d060ed9cb27b6723620b0c33c1ec6aa6972503f566e28d9f6ac5bd5b07e507
 
 //访问
 [root@localhost ~]# curl 172.17.0.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

mysql

基于centos镜像创建一个新容器命名为mysql

[root@localhost ~]# docker run --name mysql -it centos
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS                       PORTS     NAMES
8b3787cae48a   centos                 "/bin/bash"              18 seconds ago   Exited (0) 6 seconds ago               mysql
a8d060ed9cb2   weixiaoya/nginx:v0.1   "/bin/bash /opt/ngin…"   6 minutes ago    Exited (127) 6 minutes ago             test
f9c82b62551d   centos                 "/bin/bash"              21 minutes ago   Up 17 minutes                          nginx

[root@localhost src]# docker start mysql
mysql
[root@localhost src]# docker exec -it mysql /bin/bash
[root@8b3787cae48a /]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs libaio numactl

[root@8b3787cae48a /]# useradd -r -M -s /sbin/nologin mysql
[root@8b3787cae48a /]# cd /usr/src/
[root@8b3787cae48a src]# ls
debug  kernels  mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
[root@8b3787cae48a src]# 
[root@8b3787cae48a src]# tar xf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz 
[root@8b3787cae48a src]# mv mysql-5.7.35-linux-glibc2.12-x86_64 /usr/local/mysql
[root@8b3787cae48a src]# cd ..
[root@8b3787cae48a usr]# cd local/
[root@8b3787cae48a local]# 
[root@8b3787cae48a local]# chown -R mysql.mysql /usr/local/mysql
[root@8b3787cae48a local]# ls -ld /usr/local/mysql 
drwxr-xr-x 9 mysql mysql 129 Dec  4 04:09 /usr/local/mysql
[root@8b3787cae48a local]# cd             
[root@8b3787cae48a ~]# mkdir /opt/data
[root@8b3787cae48a ~]# chown  -R mysql.mysql /opt/data 
[root@8b3787cae48a ~]# ls -ld /opt/data/
drwxr-xr-x 2 mysql mysql 6 Dec  4 04:11 /opt/data/
[root@8b3787cae48a ~]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data
2021-12-04T04:11:42.738410Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-12-04T04:11:42.977233Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-12-04T04:11:43.030385Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-12-04T04:11:43.088789Z 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: 49d27577-54b8-11ec-b5ba-0242ac110003.
2021-12-04T04:11:43.090252Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-12-04T04:11:43.772281Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2021-12-04T04:11:43.772300Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2021-12-04T04:11:43.772742Z 0 [Warning] CA certificate ca.pem is self signed.
2021-12-04T04:11:43.819790Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
[root@8b3787cae48a ~]# 

[root@8b3787cae48a ~]# 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@8b3787cae48a ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@8b3787cae48a ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@8b3787cae48a ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
[root@8b3787cae48a ~]# vi opt/mysql-start.sh
[root@8b3787cae48a opt]# cat mysql-start.sh 
#!/bin/bash

/etc/init.d/mysqld start
sleep 1
/usr/local/mysql/bin/mysql
[root@8b3787cae48a /]# chmod +x opt/mysql-start.sh 

//制作mysql镜像并用该镜像运行容器测试
[root@localhost ~]# docker commit -c 'CMD ["/opt/mysql-start.sh"]' -p mysql
sha256:86da9163e3f72bc895be0604f2a2030e8049aaaa92728744abbbc267270e339f
[root@localhost ~]# docker images
REPOSITORY        TAG       IMAGE ID       CREATED          SIZE
<none>            <none>    86da9163e3f7   29 seconds ago   3.8GB
weixiaoya/nginx   v0.1      8c6b4e2ac8a6   31 minutes ago   550MB
centos            latest    5d0da3dc9764   2 months ago     231MB
[root@localhost ~]# docker tag 86da9163e3f7 weixiaoya/mysql:v1
[root@localhost ~]# docker images
REPOSITORY        TAG       IMAGE ID       CREATED              SIZE
weixiaoya/mysql   v1        86da9163e3f7   About a minute ago   3.8GB
weixiaoya/nginx   v0.1      8c6b4e2ac8a6   31 minutes ago       550MB
centos            latest    5d0da3dc9764   2 months ago         231MB
[root@8b3787cae48a /]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/opt/data/8b3787cae48a.err'.
 SUCCESS! 
[root@8b3787cae48a /]# /usr/local/mysql/bin/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> 

php

基于centos镜像创建一个新容器命名为php8

[root@localhost ~]#  docker run --name php8 -it centos
[root@0d63d79be35c /]# exit
exit
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS                        PORTS     NAMES
0d63d79be35c   centos                 "/bin/bash"              10 seconds ago   Exited (0) 5 seconds ago                php8
4bde3d4bd614   weixiaoya/mysql:v1     "/opt/mysql-start.sh"    4 minutes ago    Created                                 test1
8b3787cae48a   centos                 "/bin/bash"              30 minutes ago   Up 3 minutes                            mysql
a8d060ed9cb2   weixiaoya/nginx:v0.1   "/bin/bash /opt/ngin…"   36 minutes ago   Exited (127) 36 minutes ago             test
f9c82b62551d   centos                 "/bin/bash"              51 minutes ago   Ex

在容器中编译安装php8

[root@localhost src]# docker cp php-8.0.12.tar.gz php8:/usr/src/
[root@localhost src]# docker start php8
php8
[root@localhost src]# docker exec -it php8 /bin/bash
[root@0d63d79be35c /]# 
[root@0d63d79be35c /]#  yum -y install epel-release
[root@0d63d79be35c /]#  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@0d63d79be35c /]#  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@0d63d79be35c src]# tar xf php-8.0.12.tar.gz 
[root@0d63d79be35c src]# ls
debug  kernels  php-8.0.12  php-8.0.12.tar.gz
[root@0d63d79be35c src]# cd php-8.0.12
[root@0d63d79be35c 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
·····································
config.status: executing default commands

+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE. By continuing this installation  |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

[root@0d63d79be35c php-8.0.12]# make && make install

[root@0d63d79be35c php-8.0.12]#  /usr/local/php8/bin/php -v
PHP 8.0.12 (cli) (built: Dec  4 2021 04:58:51) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.12, Copyright (c) Zend Technologies

[root@0d63d79be35c ~]# cp -f /usr/src/php-8.0.12/php.ini-production /etc/php.ini
[root@0d63d79be35c ~]# cp -f /usr/src/php-8.0.12/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@0d63d79be35c ~]# chmod +x /etc/init.d/php-fpm 
[root@0d63d79be35c ~]# cp -f /usr/local/php8/etc/php-fpm.conf.default /usr/local//php8/etc/php-fpm.conf        
[root@0d63d79be35c ~]# cp -f /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
[root@0d63d79be35c ~]# vi /usr/local/php8/etc/php-fpm.conf
[root@0d63d79be35c ~]# vi /opt/start.sh
[root@0d63d79be35c ~]# cat /opt/start.sh 
#!/bin/bash

/etc/init.d/php-fpm start
sleep 1

[root@0d63d79be35c ~]# chmod +x /opt/start.sh 


再开一个终端制作php8镜像并用该镜像运行容器测试

[root@localhost ~]# docker commit -c 'CMD ["/bin/bash","/opt/start.sh"]' -p php8 weixiaoya/php8:v1
sha256:27afbc2aee89b3dc00382d3949b90712c7a074d331cfedab81001a343da489a8
[root@localhost ~]# docker images
REPOSITORY        TAG       IMAGE ID       CREATED             SIZE
weixiaoya/php8    v1        27afbc2aee89   13 seconds ago      1.52GB
weixiaoya/mysql   v1        86da9163e3f7   41 minutes ago      3.8GB
weixiaoya/nginx   v0.1      8c6b4e2ac8a6   About an hour ago   550MB
centos            latest    5d0da3dc9764   2 months ago        231MB

[root@localhost ~]# docker run --name test3 -it weixiaoya/php8:v1

lnmp

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值