菜鸟教程:Docker 安装 Nginx

1、查看镜像来源:docker search nginx

$ docker search nginx
NAME                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                     Official build of Nginx.                        3260      [OK]       
jwilder/nginx-proxy       Automated Nginx reverse proxy for docker c...   674                  [OK]
richarvey/nginx-php-fpm   Container running Nginx + PHP-FPM capable ...   207                  [OK]
million12/nginx-php       Nginx + PHP-FPM 5.5, 5.6, 7.0 (NG), CentOS...   67                   [OK]
maxexcloo/nginx-php       Docker framework container with Nginx and ...   57                   [OK]

2、获取 nginx 镜像:docker pull nginx

docker pull nginx

报错 Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

解决办法:

$ systemctl daemon-reload
$ sudo service docker restart
$ sudo service docker status

3、查看本地镜像

docker images

4、创建将要挂载的目录

mkdir -p /data/nginx/{conf,conf.d,html,logs}

5、创建配置文件(这里和Docker默认一样)

5.1 创建nginx.conf

# 进入/data/nginx/conf目录
cd /data/nginx/conf
# 创建nginx.conf
touch nginx.conf
# 编辑nginx.conf
vim /data/nginx/conf/nginx.conf
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  0;
    keepalive_timeout  65;

    include /etc/nginx/conf.d/*.conf;

}

5.2 创建default.conf

创建方法和5.1一样

# 编辑default.conf
vim /data/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   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   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;
    #}
}

6 获取docker 镜像中nginx的文件路径(这里是我提前找好的,可以直接套用。如果想要自己尝试去寻找,可以先将镜像启动后,通过docker exec -it nginx bash命令进入到容器内部自己寻找)

html文件路径:/etc/nginx/html
配置文件路径:/etc/nginx/nginx.conf
日志存放路径:/var/log/nginx

7 运行docker run命令启动容器 并将文件挂载出来

docker run --restart=always \
--privileged=true \
--name nginx-test \
-d -p 80:80 \
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /data/nginx/conf.d:/etc/nginx/conf.d \
-v /data/nginx/html:/etc/nginx/html \
-v /data/nginx/logs:/var/log/nginx  \
-d nginx

注意观察:绿色字体目录为自己挂载的目录,褐色字体为Docker默认挂载的目录

报错:

docker: Error response from daemon: driver failed programming external connectivity on endpoint nginx-gzmp (d508d046b38fc01450fd732a83d45a78fb14b1b80cbba4757315349ba86b4482): (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 80 -j DNAT --to-destination 172.17.0.3:80 ! -i docker0: iptables: No chain/target/match by that name.

解决办法:重启Docker

systemctl restart docker

 

8 查看 Nginx 已经运行:docker ps

docker ps

9 创建index.html测试是否挂载成功

vim /data/nginx/html/index.html
<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>

10 重新启动容器

docker restart [CONTAINER]

11 网页访问nginx

挂载成功。

posted on 2020-05-22 15:31  java先生  阅读(143)  评论(0编辑  收藏  举报