• docker使用nginx


    使用docker run命令运行Nginx应用

    使用命令可以直接启动

    [root@localhost ~]# docker run -d nginx:latest
    Unable to find image 'nginx:latest' locally
    latest: Pulling from library/nginx
    a2abf6c4d29d: Pull complete 下载完成
    a9edb18cadd1: Pull complete
    589b7251471a: Pull complete
    186b1aaa4aa6: Pull complete
    b4df32aa5a72: Waiting 等待下载
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    # docker run -d nginx:latest
    9834c8c18a7c7c89ab0ea4119d11bafe9c18313c8006bc02ce57ff54d9a1cc0c
    
    • 1
    • 2

    之后可以使用复杂命令启动

    docker run -d --name nginx1 -p 3344:80 nginx
    
    • 1

    命令解释
    docker run 启动一个容器
    -d 把容器镜像中需要执行的命令以daemon(守护进程)的方式运行
    –name 给容器一个名称
    nginx 应用容器镜像的名称,通常表示该镜像为某一个软件
    latest 表示上述容器镜像的版本,表示最新版本,用户可自定义其标识,例如v1或v2等

    启动后会生成一个容器的id
    使用docker ps 可以查看启动的容器

    命令解释
    docker ps 类似于Linux系统的ps命令,查看正在运行的容器,如果想查看没有运行的容器,需要在此命令后使用–all

    # docker ps
    CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS        PORTS     NAMES
    9834c8c18a7c   nginx:latest "/docker-entrypoint.…"   24 seconds ago   Up 23 seconds 80/tcp condescending_pare
    
    • 1
    • 2
    • 3

    访问容器中运行的Nginx服务

    确认容器IP地址

     # docker inspect 9834
     
     "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "IPAddress": "172.17.0.2", 容器IP地址
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "MacAddress": "02:42:ac:11:00:02",
                "Networks": {
                    "bridge": {
                        "IPAMConfig": null,
                        "Links": null,
                        "Aliases": null,
                        "NetworkID": "d3de2fdbc30ee36a55c1431ef3ae4578392e552009f00b2019b4720735fe5a60",
                        "EndpointID": "d91f47c9f756ff22dc599a207164f2e9366bd0c530882ce0f08ae2278fb3d50c",
                        "Gateway": "172.17.0.1",
                        "IPAddress": "172.17.0.2",   容器IP地址
                        "IPPrefixLen": 16,
                        "IPv6Gateway": "",
                        "GlobalIPv6Address": "",
                        "GlobalIPv6PrefixLen": 0,
                        "MacAddress": "02:42:ac:11:00:02",
                        "DriverOpts": null
                    }
                }
            }
        }
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    解释
    docker inspect 为查看容器结构信息命令
    9834 为前面生成的容器ID号前4位,使用这个ID号时,由于其较长,使用时能最短识别即可。

    容器网络

    # ip a s
    ......
    docker0网桥,用于为容器提供桥接,转发到主机之外的网络
    5: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
        link/ether 02:42:d5:c3:d4:cc brd ff:ff:ff:ff:ff:ff
        inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
           valid_lft forever preferred_lft forever
        inet6 fe80::42:d5ff:fec3:d4cc/64 scope link
           valid_lft forever preferred_lft forever
           
           
    与容器中的虚拟网络设备在同一个命名空间中,用于把容器中的网络连接到主机
    9: veth393dece@if8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
        link/ether 02:e3:11:58:54:0f brd ff:ff:ff:ff:ff:ff link-netnsid 0
        inet6 fe80::e3:11ff:fe58:540f/64 scope link
           valid_lft forever preferred_lft forever
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    使用curl命令访问

    # curl http://172.17.0.2
    
    返回结果,表示访问成功!
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
    html { color-scheme: light dark; }
    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    如果访问失败,则可能是网络或者系统的原因。
    我这里遇到的是系统原因,由于Linux内核太旧的原因导致的。
    使用 uname 名称查看

    uname -r
    #3.10.0-1160.83.1.el7.x86_64
    
    • 1
    • 2

    开始我的是低于此版本的
    然后更新yum源

    	yum -y update
    
    • 1

    导入ELRepo仓库公共密钥

    	rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
    
    • 1

    安装ELRepo仓库yum源

    rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
    
    • 1

    查看可用内核版本

    yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
    
    # lt表示主线版本
    #ml 表示稳定版本,建议安装 
    
    • 1
    • 2
    • 3
    • 4

    安装最新稳定版内核

    yum -y --enablerepo=elrepo-kernel install kernel-lt
    
    • 1

    安装完成之后需要设置grub2

    #查看系统上可用的内核
    sudo awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg;
    #设置刚安装的内核为默认启动内核,刚刚安装的内核即0 : CentOS Linux (5.4.207-1.el7.elrepo.x86_64) 7 (Core)
    
    grub2-set-default 0
    
    • 1
    • 2
    • 3
    • 4
    • 5

    重新生成grub配置文件并重启

    grub2-mkconfig -o /boot/grub2/grub.cfg;
    #重启
    reboot
    
    • 1
    • 2
    • 3

    验证是否安装成功

    uname -r
    
    • 1

    最后在使用上面的命令访问nginx,即可成功!

  • 相关阅读:
    MapReduce基础学习【博学谷学习记录】
    XJTUSE-离散数学-图论
    风靡全球25年的重磅IP,新作沦为脚本乐园
    如何让Springboot RestTemplate同时支持发送HTTP及HTTPS请求呢?
    chales 重写/断点/映射/手机代理/其他主机代理
    Java容器之set
    React项目实战之租房app项目(四)长列表性能优化&城市选择模块渲染列表
    微电网单台并网逆变器PQ控制matlab仿真模型
    .Net MVC 使用Areas后存在相同Controller时报错的解决办法; 从上下文获取请求的Area名及Controller名
    【计算机视觉 | 图像模型】常见的计算机视觉 image model(CNNs & Transformers) 的介绍合集(六)
  • 原文地址:https://blog.csdn.net/zql1455890112/article/details/133769733