//拉取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@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 2 months ago 231MB
// 运行centos镜像
[root@localhost ~]# docker run -it –name nginx 5d0da3dc9764 /bin/bash
[root@03ca6bdc0374 /]#
// 传nginx安装包到容器中
[root@localhost ~]# docker cp /usr/src/nginx-1.20.1.tar.gz 03ca6bdc0374:/usr/src/
// 创建nginx账户
[root@03ca6bdc0374 /]# useradd -r -M -s /sbin/nologin nginx
// 安装依赖包
[root@03ca6bdc0374 /]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
// 创建nginx日志存放目录
[root@03ca6bdc0374 /]# mkdir -p /var/log/nginx
[root@03ca6bdc0374 /]# chown -R nginx.nginx /var/log/nginx/
// 解压nginx包进行编译安装
[root@03ca6bdc0374 /]# cd /usr/src/
[root@03ca6bdc0374 src]# ls
debug kernels nginx-1.20.1.tar.gz
[root@03ca6bdc0374 src]# tar xf nginx-1.20.1.tar.gz
[root@03ca6bdc0374 src]# cd nginx-1.20.1
[root@03ca6bdc0374 nginx-1.20.1]# ./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@03ca6bdc0374 nginx-1.20.1]# make && make install
// 设置环境变量
[root@03ca6bdc0374 nginx-1.20.1]# echo export PATH=/usr/local/nginx/sbin:$PATH > /etc/profile.d/nginx.sh
[root@03ca6bdc0374 nginx-1.20.1]# source /etc/profile.d/nginx.sh
// 查看监听端口
[root@03ca6bdc0374 nginx-1.20.1]# nginx
[root@03ca6bdc0374 nginx-1.20.1]# 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 ~]# 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/” rel=”external nofollow” >nginx.org</a>.<br/>
Commercial support is available at
<a href=”http://nginx.com/” rel=”external nofollow” >nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
// 修改配置文件
[root@03ca6bdc0374 nginx-1.20.1]# vim /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@03ca6bdc0374 nginx-1.20.1]# nginx -s reload
[root@03ca6bdc0374 nginx-1.20.1]# 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:*
// 创建nginx镜像
[root@localhost ~]# docker commit -a 1826597954@qq.com -c CMD [“/usr/local/nginx/sbin/nginx”] -p 03ca6bdc0374 gaofan1225/nginx:v0.1
sha256:453bfb1a13ae0aeba38e2e26ebe03e09544aa2ea8b477e45e4fb8aa51fec3e92
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
gaofan1225/nginx v0.1 453bfb1a13ae 16 seconds ago 575MB
centos latest 5d0da3dc9764 2 months ago 231MB