系统:CentOS7.6
软件:docker 20.10.11
镜像:nginx:1.21,php:7.4-fpm
[root@192 ~]# yum install -y wget vim yum-utils device-mapper-persistent-data lvm2
[root@192 ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@192 ~]# wget http://mirror.centos.org/centos/7/extras/x86_64/Packages/container-selinux-2.119.1-1.c57a6f9.el7.noarch.rpm
[root@192 ~]# yum -y install ./container-selinux-2.119.1-1.c57a6f9.el7.noarch.rpm
#列出并排序存储库中可用的版本
[root@192 ~]# yum list docker-ce --showduplicates | sort -r
#安装
[root@192 ~]# yum install -y docker-ce docker-ce-cli containerd.io
[root@192 ~]# systemctl start docker
[root@192 ~]# systemctl enable docker
[root@192 ~]# docker version
Client: Docker Engine - Community
Version: 20.10.11
API version: 1.41
Go version: go1.16.9
Git commit: dea9396
Built: Thu Nov 18 00:38:53 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.11
API version: 1.41 (minimum version 1.12)
Go version: go1.16.9
Git commit: 847da18
Built: Thu Nov 18 00:37:17 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.12
GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0
[root@192 ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn/",
"https://hub-mirror.c.163.com",
"https://registry.docker-cn.com"
]
}
[root@192 ~]# systemctl daemon-reload
[root@192 ~]# systemctl restart docker
拉取镜像
[root@192 ~]# docker pull php:7.4-fpm
成功结果
Digest: sha256:610af6fce15abd550be76b9231ea32dae2ca9b481a3a439bf75cc5532ab63364
Status: Downloaded newer image for php:7.4-fpm
docker.io/library/php:7.4-fpm
启动容器
[root@192 ~]# docker run -d --name my-php74-fpm \
-p 9000:9000 \
-v /mnt/hgfs/www/lnmp/nginx/html:/var/www/html \
php:7.4-fpm
拉取镜像
[root@192 ~]# docker pull nginx:1.21
成功结果
Digest: sha256:9522864dd661dcadfd9958f9e0de192a1fdda2c162a35668ab6ac42b465f0603
Status: Downloaded newer image for nginx:1.21
docker.io/library/nginx:1.21
创建数据目录
mkdir -pv /mnt/hgfs/www/lnmp/nginx/{conf/conf.d,data,logs}
准备配置文件nginx.conf、conf.d/default.conf
nginx.conf
[root@192 ~]# vim /mnt/hgfs/www/lnmp/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
default.conf
[root@localhost ~]# vim /mnt/hgfs/www/lnmp/nginx/conf/conf.d/default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
启动NGINX
[root@192 ~]# docker run -d --name my-nginx \
-p 80:80 \
-v /mnt/hgfs/www/lnmp/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /mnt/hgfs/www/lnmp/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /mnt/hgfs/www/lnmp/nginx/html:/usr/share/nginx/html \
-v /mnt/hgfs/www/lnmp/nginx/logs:/var/log/nginx \
--link my-php74-fpm:php \
nginx:1.21
启动报错(因为我的是虚拟机共享宿主机Mac目录所以会有这种问题):
docker: Error response from daemon: error while creating mount source path '/mnt/hgfs/www/lnmp/nginx/html': chown /mnt/hgfs/www/lnmp/nginx/html: operation not permitted.
删除失败的容器
[root@192 ~]# docker rm -v nginx
重新赋予文件夹权限
[root@192 ~]# chattr -i /mnt/hgfs/www/lnmp/nginx/html
新建NGINX默认欢迎页
[root@192 ~]# echo "Hello World" > /mnt/hgfs/www/lnmp/nginx/html/index.html
[root@192 ~]# curl 192.168.2.101
Hello World
修改nginx的 default.conf 配置文件,使其支持解析php文件
[root@localhost ~]# vim /mnt/hgfs/www/lnmp/nginx/conf/conf.d/default.conf
# index这一行加个index.php
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
# 打开以下几行的注释
location ~ \.php$ {
fastcgi_pass php:9000; # 这里的php关联上边的my-php74-fpm
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
include fastcgi_params;
}
测试php文件的解析
[root@192 ~]# vim /mnt/hgfs/www/lnmp/nginx/html/index.php
<?php
phpinfo();
[root@192 ~]# curl 192.168.2.101/index.php
测试php连接数据库
[root@192 ~]# vim /mnt/hgfs/www/lnmp/nginx/html/mysql.php
<?php
$link = mysqli_connect('192.168.145.57', 'root', 'root');
if (!$link) {
die('Could not connect: ' . mysqli_connect_error());
}
echo 'Connected successfully';
mysqli_close($link);
[root@192 ~]# curl 192.168.2.101/mysql.php
连接报错:
Fatal error:Call to undefined function mysqli_connect()...
默认PHP没安装连接数据库扩展,进入容器进行安装
[root@192 ~]# docker exec -it my-php74-fpm bash
root@07a0104892c2:/var/www/html# cd /usr/local/etc/php
root@07a0104892c2:/usr/local/etc/php# docker-php-ext-install mysqli
root@07a0104892c2:/usr/local/etc/php# docker-php-ext-install pdo pdo_mysql
# 安装完重启
[root@192 ~]# docker restart my-php74-fpm
在新主机首次安装 Docker Engine-Community之前,需要设置Docker仓库,之后,您可以从仓库安装和更新 Docker。
设置仓库,需要安装所需的软件包。yum-utils 提供了 yum-config-manager ,并且 device mapper 存储驱动程序需要 device-mapper-persistent-data 和 lvm2。
Device Mapper 是 Linux2.6 内核中支持逻辑卷管理的通用设备映射机制,它为实现用于存储资源管理的块设备驱动提供了一个高度模块化的内核架构。
LVM(Logical Volume Manager)逻辑卷管理。它是对磁盘分区进行管理的一种机制,建立在硬盘和分区之上的一个逻辑层,用来提高磁盘管理的灵活性。通过LVM可将若干个磁盘分区连接为一个整块的卷组(Volume Group),形成一个存储池。可以在卷组上随意创建逻辑卷(Logical Volumes),并进一步在逻辑卷上创建文件系统,与直接使用物理存储在管理上相比,提供了更好灵活性。
device-mapper-persistent-data 和 lvm2两者都是Device Mapper所需要的。
Docker Engine - Community(docker-ce,Docker引擎社区版)
CE( Community Edition)是社区版,简单理解是免费使用,提供小企业与小的IT团队使用,希望从Docker开始,并尝试基于容器的应用程序部署。
EE(Docker Enterprise Edition)是企业版,收费。提供功能更强。适合大企业与打的IT团队。为企业开发和IT团队设计,他们在生产中构建、交付和运行业务关键应用程序
containerd是一个工业级标准的容器运行时,它强调简单性、健壮性和可移植性。containerd可以在宿主机中管理完整的容器生命周期,包括容器镜像的传输和存储、容器的执行和管理、存储和网络等。
containerd是从Docker中分离出来的一个项目,可以作为一个底层容器运行时,现在它成了Kubernete容器运行时更好的选择。
不仅仅是Docker,还有很多云平台也支持containerd作为底层容器运行时。