×

使用Docker安装Nginx

hqy hqy 发表于2026-01-08 23:16:35 浏览22 评论0

抢沙发发表评论

1、安装Docker









# 安装Dockerhttps://docs.docker.com/get-docker/
# 安装Docker Composehttps://docs.docker.com/compose/install/
# CentOS安装Dockerhttps://mp.weixin.qq.com/s/nHNPbCmdQs3E5x1QBP-ueA
2、安装Nginx
创建目录:


mkdir -p nginx/{conf,log,html}cd nginx

创建conf/default.conf文件:













































server {    listen       80;    listen  [::]:80;    server_name  localhost;
    #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;    #}}
创建html/index.html文件:

hello world
2.1、方式1

创建docker-compose.yaml文件:
















services:  nginx:    image: nginx:latest    container_name: nginx    restart: unless-stopped    privileged: true    ports:      - 8080:80      - 8443:443      - 8081:81 #预留端口      - 8082:82 #预留端口    volumes:      - ./conf:/etc/nginx/conf.d      - ./log:/var/log/nginx      - ./html:/usr/share/nginx/html
创建并启动容器:

docker-compose up -d
查看容器列表:

docker ps
查看日志:

docker logs nginx
重启容器:

docker restart nginx

停止并销毁容器:


docker-compose down
删除镜像:

docker rmi nginx:latest
删除目录:

rm -rf ./conf ./log ./html
2.2、方式2
拉取镜像:

docker pull nginx:latest
运行容器:











docker run -it -d \  --name nginx \  -p 8080:80 \  -p 8443:443 \  -p 8081:81 \  -p 8082:82 \  -v ./conf:/etc/nginx/conf.d \  -v ./log:/var/log/nginx \  -v ./html:/usr/share/nginx/html \  --privileged \  nginx:latest
查看容器列表:

docker ps
查看日志:

docker logs nginx
重启容器:

docker restart nginx

停止容器:


docker stop nginx
删除容器:

docker rm nginx
删除镜像:

docker rmi nginx:latest
删除目录:

rm -rf ./conf ./log ./html
2.3、方式3
拉取镜像:

docker pull nginx:latest
下载:

wget https://nginx.org/download/nginx-1.28.0.tar.gz
创建Dockerfile文件:









































FROM alpine:latestADD nginx-1.28.0.tar.gz /root/RUN cp /etc/apk/repositories /etc/apk/repositories-bak; \  sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories; \  apk update; \  apk add build-base gcc g++ pcre-dev openssl-dev zlib-dev curl ca-certificates; \  adduser -S -s /bin/false -H nginx; \  addgroup -S nginx; \  cd /root/nginx-1.28.0; \  ./configure \    --prefix=/usr/local/nginx \    --user=nginx \    --group=nginx \    --with-compat \    --with-threads \    --with-http_addition_module \    --with-http_auth_request_module \    --with-http_dav_module \    --with-http_flv_module \    --with-http_gunzip_module \    --with-http_gzip_static_module \    --with-http_mp4_module \    --with-http_random_index_module \    --with-http_realip_module \    --with-http_secure_link_module \    --with-http_slice_module \    --with-http_ssl_module \    --with-http_stub_status_module \    --with-http_sub_module \    --with-http_v2_module \    --with-http_v3_module \    --with-mail \    --with-mail_ssl_module \    --with-stream \    --with-stream_realip_module \    --with-stream_ssl_module \    --with-stream_ssl_preread_module; \  make && make install; \  rm -rf /root/nginx-1.28.0WORKDIR /usr/local/nginxCMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
构建镜像:

docker build -t nginx:v1 .
运行容器:










docker run -it -d \  --name nginx \  -p 8080:80 \  -p 8443:443 \  -p 8081:81 \  -p 8082:82 \  -v ./log:/usr/local/nginx/logs \  -v ./html:/usr/local/nginx/html \  --privileged \  nginx:v1
查看容器列表:

docker ps
查看日志:

docker logs nginx
查看nginx.conf文件:








# 从容器中复制nginx.conf文件:docker cp nginx:/usr/local/nginx/conf/nginx.conf nginx.conf
# 查看文件:cat nginx.conf
# 将nginx.conf文件复制到容器中:# docker cp nginx.conf nginx:/usr/local/nginx/conf/nginx.conf
重启容器:

docker restart nginx

停止容器:


docker stop nginx
删除容器:

docker rm nginx
删除镜像:

docker rmi nginx:v1
删除目录:

rm -rf ./conf ./log ./html
3、浏览器访问


假设当前ip为192.168.186.128浏览器访问:http://192.168.186.128:8080
4、详见


https://nginx.org/https://github.com/nginx/nginx


打赏

本文链接:https://www.kinber.cn/post/6145.html 转载需授权!

分享到:


推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

 您阅读本篇文章共花了: 

群贤毕至

访客