docker安装Nginx

本文详细介绍了如何使用Docker安装、运行及删除Nginx容器,包括镜像搜索、拉取、容器启动与配置修改,以及通过浏览器验证部署情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单介绍下docker安装和删除nginx

1、查找镜像,docker search nginx

[root@*** ~]# docker search nginx
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                             Official build of Nginx.                        12076               [OK]                
jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   1674                                    [OK]
richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   743                                     [OK]
linuxserver/nginx                 An Nginx container, brought to you by LinuxS…   79                                      
bitnami/nginx                     Bitnami nginx Docker Image                      72                                      [OK]
tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp…   58                                      [OK]
nginxdemos/hello                  NGINX webserver that serves a simple page co…   31                                      [OK]
.
.
.

2、拉取镜像,docker pull nginx,官方最新版本的镜像

[root@*** ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
8d691f585fa8: Pull complete 
047cb16c0ff6: Pull complete 
b0bbed1a78ca: Pull complete 
Digest: sha256:77ebc94e0cec30b20f9056bac1066b09fbdc049401b71850922c63fc0cc1762e
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

3、启动nginx容器,docker run -d --name nginxtest -p 8080:80  nginx

[root@*** ~]# docker run -d --name nginxtest -p 8080:80 nginx
0a1d21d9c7a5e8cf312746a201e220cf202102bcebca4bc79b7b5e8f1b0e0c5f
[root@*** ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
19bfd17efb87        nginx               "nginx -g 'daemon of…"   17 seconds ago      Up 16 seconds       0.0.0.0:8080->80/tcp   nginxtest

4、在浏览器中查看nginx部署情况

5、修改首页

由于nginx容器中不能使用vi/vim等命令,所以采用挂载文件方式来修改首页

1)、创建文件夹,分别对应nginx容器中的目录

[root@*** ~]# pwd
/root
[root@*** ~]# mkdir -p nginx/html nginx/conf nginx/logs
[root@*** ~]# ls nginx
conf  html  logs

2)、复制nginx的配置文件到nginx/conf文件夹

[root@*** ~]# docker cp 19bfd17efb87:/etc/nginx/nginx.conf $PWD/nginx/conf
[root@*** ~]# ls nginx/conf/
nginx.conf

3)、新建首页(index.xml)

[root@*** ~]# vim nginx/html/index.html

index.xml: 

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>nginx1</title>
</head>
<body>
    <h1>简单测试</h1>
</body>
</html>

4)、启动容器,绑定3306端口

[root@*** ~]# docker run -d -p 3306:80 --name nginx1 -v $PWD/nginx/html:/usr/share/nginx/html -v $PWD/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/nginx/logs:/var/log/nginx nginx
970c474b37029d082b9eae786c9ba554333d5184ccd9374e3af3d5735e8788f3

 5)、页面测试

6、删除容器

1)、停止容器

[root@*** ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
970c474b3702        nginx               "nginx -g 'daemon of…"   2 hours ago         Up 2 hours          0.0.0.0:3306->80/tcp   nginx1
19bfd17efb87        nginx               "nginx -g 'daemon of…"   3 hours ago         Up 3 hours          0.0.0.0:8080->80/tcp   nginxtest
[root@*** ~]# docker stop nginx1 nginxtest
nginx1
nginxtest
[root@*** ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
970c474b3702        nginx               "nginx -g 'daemon of…"   2 hours ago         Exited (0) 5 seconds ago                       nginx1
19bfd17efb87        nginx               "nginx -g 'daemon of…"   3 hours ago         Exited (0) 5 seconds ago                       nginxtest

2)、删除容器,也可以删除创建容器时创建的文件

[root@*** ~]# docker rm nginx1 nginxtest
nginx1
nginxtest
[root@*** ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@*** ~]# rm -rf nginx/

 

 

如果有写的不对的地方,请大家多多批评指正,非常感谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值