# Docker安装Nginx

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]# 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,033评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,725评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,473评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,846评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,848评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,691评论 1 282
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,053评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,700评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,856评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,676评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,787评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,430评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,034评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,990评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,218评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,174评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,526评论 2 343