1 项目描述
使用Docker容器基于centos镜像分别制作nginx镜像,mysql镜像和php镜像使用编译安装的方式,最后通过镜像启动成容器时使用container模式网络模式并访问到php测试页面
2 Nginx镜像制作
//拉取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 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 2 months ago 231MB
//运行centos镜像
[root@Docker ~]# docker run -it --name nginx 5d0da3dc9764 /bin/bash
[root@2a73e41f9c08 /]#
//用docker cp将nginx软件包传到容器中
[root@Docker ~]# docker cp /usr/src/nginx-1.20.2.tar.gz 2a73e41f9c08:/usr/src/
//编译安装nginx
[root@2a73e41f9c08 /]# useradd -r -M -s /sbin/nologin nginx
[root@2a73e41f9c08 /]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
[root@2a73e41f9c08 ~]# yum -y groups mark install 'Development Tools'
[root@2a73e41f9c08 /]# mkdir -p /var/log/nginx
[root@2a73e41f9c08 /]# chown -R nginx.nginx /var/log/nginx
[root@2a73e41f9c08 ~]# cd /usr/src/
[root@2a73e41f9c08 src]# ls
debug kernels nginx-1.20.2.tar.gz
[root@2a73e41f9c08 src]# tar xf nginx-1.20.2.tar.gz
[root@2a73e41f9c08 src]# cd nginx-1.20.2
[root@2a73e41f9c08 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@2a73e41f9c08 nginx-1.20.2]# make && make install
[root@2a73e41f9c08 ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@2a73e41f9c08 ~]# source /etc/profile.d/nginx.sh
[root@2a73e41f9c08 ~]# nginx
[root@2a73e41f9c08 ~]# 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@Docker ~]# 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>
[root@2a73e41f9c08 ~]# cat /usr/local/nginx/conf/nginx.conf
......
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
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; #在工作中这里要改为php服务器的地址
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $Document_Root$fastcgi_script_name;
include fastcgi_params;
}
......
daemon off;
//重新加载配置文件
[root@2a73e41f9c08 ~]# nginx -s reload
//创建镜像
[root@Docker ~]# docker commit -a '1720615472@qq.com' -c 'CMD ["/usr/local/nginx/sbin/nginx"]' -p 2a73e41f9c08 zhaojie10/nginx:v0.2
sha256:b6af6930e9b06f4930e7636111322fd2e8951fd2f92205a30354060f64412798
3 mysql镜像制作
//运行centos镜像
[root@Docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 2 months ago 231MB
[root@Docker ~]# docker run -it --name mysql 5d0da3dc9764 /bin/bash
[root@c6fb6192b8bf /]#
[root@Docker ~]# docker cp /usr/src/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz c6fb6192b8bf:/usr/src/
//二进制安装mysql
[root@c6fb6192b8bf /]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs
[root@c6fb6192b8bf ~]# yum install libaio*
[root@c6fb6192b8bf ~]# yum -y install numactl.x86_64
[root@c6fb6192b8bf /]# useradd -r -M -s /sbin/nologin mysql
[root@c6fb6192b8bf /]# cd /usr/src/
[root@c6fb6192b8bf src]# tar xf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@c6fb6192b8bf src]# cd /usr/local/
[root@c6fb6192b8bf local]# ls
bin games lib libexec sbin src
etc include lib64 mysql-5.7.35-linux-glibc2.12-x86_64 share
[root@c6fb6192b8bf local]# ln -sv mysql-5.7.35-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.35-linux-glibc2.12-x86_64/'
[root@c6fb6192b8bf local]# chown -R mysql.mysql /usr/local/mysql*
[root@c6fb6192b8bf ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@c6fb6192b8bf ~]# source /etc/profile.d/mysql.sh
[root@c6fb6192b8bf ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
//头文件软连接
[root@c6fb6192b8bf ~]# ln -s /usr/local/mysql/include /usr/include/mysql
//帮助文档
[root@c6fb6192b8bf ~]# cat /etc/man_db.conf
MANDATORY_MANPATH /usr/local/mysql/man
//库文件
[root@c6fb6192b8bf ~]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@c6fb6192b8bf ~]# ldconfig
//建立数据存放目录
[root@c6fb6192b8bf ~]# mkdir -p /opt/data
[root@c6fb6192b8bf ~]# chown -R mysql.mysql /opt/data/
[root@c6fb6192b8bf ~]# ls -l /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Dec 3 11:03 data
//初始化数据库
[root@c6fb6192b8bf ~]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/
//生成配置文件
[root@c6fb6192b8bf ~]# 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@c6fb6192b8bf ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /usr/local/mysql/support-files/mysql.server
[root@c6fb6192b8bf ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /usr/local/mysql/support-files/mysql.server
//启动服务
[root@c6fb6192b8bf ~]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL.Logging to '/opt/data/fabd06853952.err'.
SUCCESS!
[root@c6fb6192b8bf ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 80 *:3306 *:*
//mysql启动脚本
[root@2749a49a5a37 /]# cat /start.sh
#! /bin/sh
/usr/local/mysql/support-files/mysql.server start
/bin/bash
[root@69e189832922 /]# chmod +x /start.sh
//制作mysql镜像
[root@Docker ~]# docker commit -a '1720615473@qq.com' -c 'CMD ["/bin/bash","/start.sh"]' -p cb32f6d09dc6 zhaojie10/mysql:v0.2
sha256:c5551344ed4e6a283811b1a5d21703c823faa8abd95cfa2c45536a96b8978930
[root@Docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zhaojie10/mysql v0.2 c5551344ed4e About a minute ago 3.81GB
zhaojie10/nginx v0.2 b6af6930e9b0 About an hour ago 550MB
centos latest 5d0da3dc9764 2 months ago 231MB
4 PHP镜像制作
[root@Docker ~]# docker run -it --name php 5d0da3dc9764
[root@24e92ea9885e /]#
[root@Docker ~]# docker cp /usr/src/php-8.0.10.tar.xz 24e92ea9885e:/usr/src/
[root@24e92ea9885e /]# yum -y install epel-release
[root@24e92ea9885e /]# 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
//因为libcurl-minimal-7.61.1-18.el8.x86_64 与 libcurl-7.61.1-22.el8.x86_64 提供的 libcurl(x86-64) 冲突 (所以得分开下载)
[root@24e92ea9885e /]# 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@24e92ea9885e /]# yum -y install libcurl-devel
//解压编译PHP
[root@24e92ea9885e /]# c /usr/src/
bash: c: command not found
[root@24e92ea9885e /]# cd /usr/src/
[root@24e92ea9885e src]# tar xf php-8.0.10.tar.xz
[root@24e92ea9885e src]# cd php-8.0.10
[root@24e92ea9885e php-8.0.10]# ./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@24e92ea9885e php-8.0.10]# make && make install
//设置环境变量
[root@24e92ea9885e php-8.0.10]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@24e92ea9885e php-8.0.10]# source /etc/profile.d/php.sh
//配置php-fpm
[root@24e92ea9885e php-8.0.10]# cp php.ini-production /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[root@24e92ea9885e php-8.0.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm[root@24e92ea9885e php-8.0.10]# chmod +x /etc/init.d/php-fpm
[root@24e92ea9885e php-8.0.10]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@24e92ea9885e php-8.0.10]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
//创建测试页面
[root@24e92ea9885e /]# mkdir /var/www/html
[root@24e92ea9885e /]# cat /var/www/html/index.php
<?php
phpinfo()
?>
//启动php-fpm
[root@24e92ea9885e sbin]# /usr/local/php/sbin/php-fpm
[root@24e92ea9885e sbin]# 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@24e92ea9885e /]# cat /start.sh
#! /bin/sh
/usr/local/php/sbin/php-fpm
/bin/bash
[root@24e92ea9885e /]# chmod +x /start.sh
//制作php镜像
[root@Docker ~]# docker commit -a '1720615473@qq.com' -c 'CMD ["/bin/bash","/start.sh"]' -p afe8f7ca40b8 zhaojie10/php:v0.2
sha256:2866421633a938984a96bae8fe181c0b6fddade018d6f1d223e8b7a9bd76e4a3
[root@Docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zhaojie10/php v0.2 2866421633a9 13 seconds ago 1.52GB
zhaojie10/mysql v0.2 c5551344ed4e 45 minutes ago 3.81GB
zhaojie10/nginx v0.2 b6af6930e9b0 2 hours ago 550MB
centos latest 5d0da3dc9764 2 months ago 231MB
5 运行LNMP
使用container模式网络模式
//启动nginx容器
[root@Docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@Docker ~]# docker run -dit --name nginx -p 80:80 b6af6930e9b0
bc2468f56bb6544185f942223896618d18f98a1f4cece3d01369469aa57f22d2
[root@Docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc2468f56bb6 b6af6930e9b0 "/usr/local/nginx/sb…" 29 seconds ago Up 28 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
[root@Docker ~]# docker exec -it bc /bin/bash
[root@bc2468f56bb6 /]# 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:*
//启动mysql容器
[root@Docker ~]# docker run -dit --name mysql --network container:bc2468f56bb6 c5551344ed4e
6eaabb4faf3d5f13081d409f39e67d1a3950867ce28c8957de500dbedd08eb27
[root@Docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6eaabb4faf3d c5551344ed4e "/bin/bash /start.sh" 35 seconds ago Up 34 seconds mysql
bc2468f56bb6 b6af6930e9b0 "/usr/local/nginx/sb…" 7 minutes ago Up 7 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
[root@Docker ~]# docker exec -it 6e /bin/bash
[root@bc2468f56bb6 /]# 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 *:*
//启动php容器
[root@Docker ~]# docker run -dit --name php --network container:bc2468f56bb6
2866421633a9
aebe0247c7b089597fb2ff79a0f3e69cadc727dd0d84eae3218b130c3f1583e1
[root@Docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aebe0247c7b0 2866421633a9 "/bin/bash /start.sh" 3 seconds ago Up 2 seconds php
6eaabb4faf3d c5551344ed4e "/bin/bash /start.sh" 3 minutes ago Up 3 minutes mysql
bc2468f56bb6 b6af6930e9b0 "/usr/local/nginx/sb…" 10 minutes ago Up 10 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
[root@Docker ~]# docker exec -it ae /bin/bash
[root@bc2468f56bb6 /]# 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 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
测试
|