美文网首页程序员
# Docker安装Nginx

# Docker安装Nginx

作者: 努力耕耘少问收获 | 来源:发表于2020-09-10 16:54 被阅读0次

    docker安装nginx

    1.查询docker下面的nginx
    [root@VM-0-2-centos ~]# docker search nginx
    NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
    nginx                              Official build of Nginx.                        13694               [OK]                
    jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con…   1870                                    [OK]
    richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable of…   782                                     [OK]
    linuxserver/nginx                  An Nginx container, brought to you by LinuxS…   127                                     
    bitnami/nginx                      Bitnami nginx Docker Image                      89                                      [OK]
    tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rtmp…   89                                      [OK]
    jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ho…   84                                      
    alfg/nginx-rtmp                    NGINX, nginx-rtmp-module and FFmpeg from sou…   75                                      [OK]
    nginxdemos/hello                   NGINX webserver that serves a simple page co…   59                                      [OK]
    jlesage/nginx-proxy-manager        Docker container for Nginx Proxy Manager        53                                      [OK]
    nginx/nginx-ingress                NGINX Ingress Controller for Kubernetes         41                                      
    privatebin/nginx-fpm-alpine        PrivateBin running on an Nginx, php-fpm & Al…   32                                      [OK]
    schmunk42/nginx-redirect           A very simple container to redirect HTTP tra…   19                                      [OK]
    nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  17                                      
    nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter                       15                                      
    centos/nginx-112-centos7           Platform for running nginx 1.12 or building …   14                                      
    raulr/nginx-wordpress              Nginx front-end for the official wordpress:f…   13                                      [OK]
    centos/nginx-18-centos7            Platform for running nginx 1.8 or building n…   13                                      
    staticfloat/nginx-certbot          Opinionated setup for automatic TLS certs lo…   12                                      [OK]
    mailu/nginx                        Mailu nginx frontend                            7                                       [OK]
    sophos/nginx-vts-exporter          Simple server that scrapes Nginx vts stats a…   7                                       [OK]
    bitwarden/nginx                    The Bitwarden nginx web server acting as a r…   7                                       
    bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Contr…   6                                       [OK]
    wodby/nginx                        Generic nginx                                   1                                       [OK]
    ansibleplaybookbundle/nginx-apb    An APB to deploy NGINX                          1                                       [OK]
    
    2.拉取镜像
    [root@VM-0-2-centos ~]# docker pull nginx
    Using default tag: latest
    latest: Pulling from library/nginx
    bf5952930446: Already exists 
    cb9a6de05e5a: Pull complete 
    9513ea0afb93: Pull complete 
    b49ea07d2e93: Pull complete 
    a5e4a503d449: Pull complete 
    Digest: sha256:b0ad43f7ee5edbc0effbc14645ae7055e21bc1973aee5150745632a24a752661
    Status: Downloaded newer image for nginx:latest
    docker.io/library/nginx:latest
    
    3.查询镜像
    [root@VM-0-2-centos ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              4bb46517cac3        3 weeks ago         133MB
    redis               latest              1319b1eaa0b7        4 weeks ago         104MB
    mysql               latest              0d64f46acfd1        4 weeks ago         544MB
    java                8                   d23bdf5b1b1b        3 years ago         643MB
    [root@VM-0-2-centos ~]# 
    
    4.运行
    [root@VM-0-2-centos ~]# docker run --name my-nginx -p 8081:80 -d nginx
    76414c320fdfe8e8034b96894442ac72857d2a100ce9af9eb447827a0feca6b9
    
    5.查看启动的容器
    [root@VM-0-2-centos ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                                NAMES
    76414c320fdf        nginx               "/docker-entrypoint.…"   5 seconds ago       Up 4 seconds                0.0.0.0:8081->80/tcp                 my-nginx
    5003b856a8af        redis:latest        "docker-entrypoint.s…"   15 minutes ago      Up 15 minutes               0.0.0.0:6379->6379/tcp               my-redis
    af4ce8ae02f5        redis:latest        "docker-entrypoint.s…"   3 weeks ago         Exited (0) 16 minutes ago                                        mystifying_elgamal
    13e442ccdff6        mysql               "docker-entrypoint.s…"   3 weeks ago         Up About an hour            33060/tcp, 0.0.0.0:33060->3306/tcp   my-mysql
    [root@VM-0-2-centos ~]# 
    [root@VM-0-2-centos ~]# 
    

    docker挂载数据卷

    1.配置docker 镜像
    vim  /etc/docker/daemon.json 
    {"registry-mirrors":["https://reg-mirror.qiniu.com/"]}
    
    2.重启
    $ sudo systemctl daemon-reload
    $ sudo systemctl restart docker
    
    3.检查效果
    $ docker info
    1
    Registry Mirrors:
     https://reg-mirror.qiniu.com/
    Live Restore Enabled: false
    Registries: docker.io (secure)
    [root@cyt ~]# 
    
    4.新建文件夹
    [root@VM-0-2-centos opt]# mkdir nginx
    [root@VM-0-2-centos opt]# cd nginx
    [root@VM-0-2-centos nginx]# mkdir conf
    [root@VM-0-2-centos nginx]# mkdir conf.d
    [root@VM-0-2-centos nginx]# mkdir logs
    [root@VM-0-2-centos nginx]# mkdir html
    [root@VM-0-2-centos nginx]# ll
    
    
    [root@VM-0-2-centos opt]# mkdir -p nginx/{conf,html,logs}
    
    5.目录结构
    50.jpg
    6.创建nginx.xonf
    [root@VM-0-2-centos conf]# touch nginx.conf
    [root@VM-0-2-centos conf]# vim nginx.conf 
    
    7.添加配置文件
    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;
    }
    
    8.创建默认的defautl.conf
    [root@VM-0-2-centos conf.d]# touch defautl.conf
    [root@VM-0-2-centos conf.d]# vim defautl.conf 
    
    9.默认配置文件如下
    server { 
      listen    80; 
      server_name localhost; 
     
      #charset koi8-r; 
      #access_log /var/log/nginx/log/host.access.log main; 
     
      location / { 
        # root  /opt/nginx/html; 
        root   /usr/share/nginx/html;  
        index index.html index.htm; 
        autoindex on; 
        #try_files $uri /index/index/page.html; 
        #try_files $uri /index/map/page.html; 
      } 
     
      #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; 
      #} 
    }
    
    10.挂载页面修改
    [root@VM-0-2-centos conf.d]# touch /opt/nginx/html/index.html
    [root@VM-0-2-centos conf.d]# vim /opt/nginx/html/index.html
    
    11.页面如下
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>系统时间</title>
    </head>
    <body>
    <div id="datetime">
        <script>
            setInterval("document.getElementById('datetime').innerHTML=new Date().toLocaleString();", 1000);
        </script>
    </div>
    </body>
    
    12.创建容器
    [root@VM-0-2-centos conf.d]# docker run --name nginx81 -d -p 81:80 -v /opt/nginx/html:/usr/share/nginx/html -v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/logs:/var/log/nginx -v /opt/nginx/conf.d:/etc/nginx/conf.d -d nginx
    1bc2ee5754b6a625567b37606b13e5c06a6e44de258989632aefac88998f572e
    [root@VM-0-2-centos conf.d]# 
    [root@VM-0-2-centos conf.d]# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                NAMES
    1bc2ee5754b6        nginx               "/docker-entrypoint.…"   33 seconds ago      Up 32 seconds       0.0.0.0:81->80/tcp                   nginx81
    76414c320fdf        nginx               "/docker-entrypoint.…"   38 minutes ago      Up 38 minutes       0.0.0.0:8081->80/tcp                 my-nginx
    5003b856a8af        redis:latest        "docker-entrypoint.s…"   53 minutes ago      Up 53 minutes       0.0.0.0:6379->6379/tcp               my-redis
    13e442ccdff6        mysql               "docker-entrypoint.s…"   3 weeks ago         Up 2 hours          33060/tcp, 0.0.0.0:33060->3306/tcp   my-mysql
    [root@VM-0-2-centos conf.d]# 
    
    [root@iZ2vc2em3ma84cj1zfcwqfZ html]# docker run --name nginx80 -d -p 80:80 -v /opt/nginx/html:/usr/share/nginx/html -v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/logs:/var/log/nginx -v /opt/nginx/conf.d:/etc/nginx/conf.d -d nginx
    Unable to find image 'nginx:latest' locally
    latest: Pulling from library/nginx
    d121f8d1c412: Pull complete 
    ebd81fc8c071: Pull complete 
    655316c160af: Pull complete 
    d15953c0e0f8: Pull complete 
    2ee525c5c3cc: Pull complete 
    Digest: sha256:9a1f8ed9e2273e8b3bbcd2e200024adac624c2e5c9b1d420988809f5c0c41a5e
    Status: Downloaded newer image for nginx:latest
    bb2713a6a0ca907bfae60f836f5ee2ff1d581f0deb3a513b8354025add327ce8
    [root@iZ2vc2em3ma84cj1zfcwqfZ html]# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                NAMES
    bb2713a6a0ca        nginx               "/docker-entrypoint.…"   9 seconds ago       Up 7 seconds        0.0.0.0:80->80/tcp                   nginx80
    2a71e3741df5        mysql               "docker-entrypoint.s…"   12 days ago         Up 12 days          33060/tcp, 0.0.0.0:33060->3306/tcp   my-mysql
    [root@iZ2vc2em3ma84cj1zfcwqfZ html]# 
    
    13.各数据卷描述
    #将容器中nginx的80端口映射到本地的81端口
    docker run --name nginx81 -d -p 81:80 
    -v /opt/nginx/html:/usr/share/nginx/html 
    -v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf 
    -v /opt/nginx/logs:/var/log/nginx 
    -v /opt/nginx/conf.d:/etc/nginx/conf.d 
    -d nginx:latest
    

    文章来源

    https://blog.csdn.net/qq_41291945/article/details/107898035

    http://132.232.25.195:81/

    51.jpg

    https://blog.csdn.net/ddhsea/article/details/92203713

    http://132.232.25.195:8081/

    查看端口

    [root@iZ2vc2em3ma84cj1zfcwqfZ html]# lsof -i:80
    -bash: lsof: command not found
    

    先要安装lsof

    [root@iZ2vc2em3ma84cj1zfcwqfZ html]# yum install -y lsof
    Loaded plugins: fastestmirror
    Determining fastest mirrors
    base                                                                                                                                                                  | 3.6 kB  00:00:00     
    docker-ce-stable                                                                                                                                                      | 3.5 kB  00:00:00     
    epel                                                                                                                                                                  | 4.7 kB  00:00:00     
    extras                                                                                                                                                                | 2.9 kB  00:00:00     
    mysql-connectors-community                                                                                                                                            | 2.5 kB  00:00:00     
    mysql-tools-community                                                                                                                                                 | 2.5 kB  00:00:00     
    mysql57-community                                                                                                                                                     | 2.5 kB  00:00:00     
    openresty                                                                                                                                                             | 2.9 kB  00:00:00     
    updates                                                                                                                                                               | 2.9 kB  00:00:00     
    (1/2): epel/x86_64/updateinfo                                                                                                                                         | 1.0 MB  00:00:00     
    (2/2): epel/x86_64/primary_db                                                                                                                                         | 6.9 MB  00:00:00     
    Resolving Dependencies
    --> Running transaction check
    ---> Package lsof.x86_64 0:4.87-6.el7 will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    =============================================================================================================================================================================================
     Package                                    Arch                                         Version                                            Repository                                  Size
    =============================================================================================================================================================================================
    Installing:
     lsof                                       x86_64                                       4.87-6.el7                                         base                                       331 k
    
    Transaction Summary
    =============================================================================================================================================================================================
    Install  1 Package
    
    Total download size: 331 k
    Installed size: 927 k
    Downloading packages:
    lsof-4.87-6.el7.x86_64.rpm                                                                                                                                            | 331 kB  00:00:00     
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      Installing : lsof-4.87-6.el7.x86_64                                                                                                                                                    1/1 
      Verifying  : lsof-4.87-6.el7.x86_64                                                                                                                                                    1/1 
    
    Installed:
      lsof.x86_64 0:4.87-6.el7                                                                                                                                                                   
    
    Complete!
    

    然后在安装工具类

    [root@iZ2vc2em3ma84cj1zfcwqfZ html]# yum install -y net-tools
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    Package net-tools-2.0-0.25.20131004git.el7.x86_64 already installed and latest version
    Nothing to do
    [root@iZ2vc2em3ma84cj1zfcwqfZ html]# lsof -i:8088
    COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    java    23684 root   42u  IPv6 378630      0t0  TCP *:radan-http (LISTEN)
    

    这是我们就可以查看端口了

    [root@iZ2vc2em3ma84cj1zfcwqfZ html]# lsof -i:80
    COMMAND     PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    AliYunDun 13148 root   22u  IPv4 309685      0t0  TCP iZ2vc2em3ma84cj1zfcwqfZ:37620->100.100.30.25:http (ESTABLISHED)
    

    或者

    [root@iZ2vc2em3ma84cj1zfcwqfZ html]# netstat -ntulp | grep 80
    tcp        0      0 0.0.0.0:801             0.0.0.0:*               LISTEN      11122/nginx: worker 
    tcp        0      0 0.0.0.0:802             0.0.0.0:*               LISTEN      11122/nginx: worker 
    tcp6       0      0 :::8088                 :::*                    LISTEN      23684/java          
    tcp6       0      0 :::8089                 :::*                    LISTEN      23774/java          
    

    后者

    [root@iZ2vc2em3ma84cj1zfcwqfZ html]# ps -ef|grep java
    root     23684     1  0 Sep02 ?        00:05:07 java -jar -XX:PermSize=128M -XX:MaxPermSize=256M hongren-admin.jar --server.port=8088 --spring.profiles.active=sim
    root     23774     1  0 Sep02 ?        00:04:30 java -jar -XX:PermSize=128M -XX:MaxPermSize=256M hongren-api.jar --server.port=8089 --spring.profiles.active=sim
    root     25887 24872  0 09:32 pts/0    00:00:00 grep --color=auto java
    [root@iZ2vc2em3ma84cj1zfcwqfZ html]# lsof -i:80
    COMMAND     PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    AliYunDun 13148 root   22u  IPv4 309685      0t0  TCP iZ2vc2em3ma84cj1zfcwqfZ:37620->100.100.30.25:http (ESTABLISHED)
    [root@iZ2vc2em3ma84cj1zfcwqfZ html]# lsof -i:8088
    COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    java    23684 root   42u  IPv6 378630      0t0  TCP *:radan-http (LISTEN)
    [root@iZ2vc2em3ma84cj1zfcwqfZ html]# 
    

    相关文章

      网友评论

        本文标题:# Docker安装Nginx

        本文链接:https://www.haomeiwen.com/subject/rexyektx.html