• docker学习8-搭建nginx环境


    前言

    使用 docker 搭建 nginx 环境

    下载镜像

    使用docker pull 拉取最新的 nginx 镜像

    [root@yoyo ~]# docker pull nginx
    Using default tag: latest
    latest: Pulling from library/nginx
    b8f262c62ec6: Pull complete 
    e9218e8f93b1: Pull complete 
    7acba7289aa3: Pull complete 
    Digest: sha256:aeded0f2a861747f43a01cf1018cf9efe2bdd02afd57d2b11fcc7fcadc16ccd1
    Status: Downloaded newer image for nginx:latest
    

    查看镜像

    [root@yoyo ~]# docker images nginx
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              f949e7d76d63        2 weeks ago         126MB
    [root@yoyo ~]# 
    

    启动容器

    先启动容器: docker run -d --name nginx-tmp nginx

    [root@yoyo nginx.conf]# docker run -d --name nginx-tmp nginx
    7a453ca1aea80eb731de7270dcf6327ae81d2947ffd7faf2ae76253eb7bc0e6a
    [root@yoyo nginx.conf]# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
    7a453ca1aea8        nginx               "nginx -g 'daemon of…"   4 seconds ago       Up 4 seconds        80/tcp                              nginx-tmp
    

    nginx的目录文件在容器内部的位置

    • 日志文件位置:/var/log/nginx
    • 配置文件位置: /etc/nginx
    • 资源存放的位置: /usr/share/nginx/html

    先在本地新建nginx运行的目录,日志目录为软连接,所以不创建logs目录

    mkdir -p /root/nginx/html /root/nginx/conf

    从容器中拷贝配置文件至本地

    docker cp a89b2c5f3dd1:/etc/nginx/nginx.conf /root/nginx/conf

    查看/root/nginx/conf目录下nginx.conf 文件内容

    [root@yoyo conf]# cd /root/nginx/conf
    [root@yoyo conf]# cat 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  65;
    
        #gzip  on;
    
        include /etc/nginx/conf.d/*.conf;
    }
    [root@yoyo conf]# 
    
    

    配置文件nginx.conf 拷贝到本地目录后,停掉上面的nginx容器并删除

    [root@yoyo conf]# docker stop nginx-tmp
    nginx-tmp
    [root@yoyo conf]# docker rm nginx-tmp
    nginx-tmp
    

    正式部署

    正式部署完整的指令

    docker run -d -p 8088:80 --name nginx-yoyo -v ~/nginx/html:/usr/share/nginx/html -v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/nginx/logs:/var/log/nginx nginx

    容器启动后使用docker ps查看有没启动

    [root@yoyo nginx]# docker run -d -p 8088:80 --name nginx-yoyo -v ~/nginx/html:/usr/share/nginx/html -v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/nginx/logs:/var/log/nginx nginx
    1b4566a75078f55831edcff6b9b8f9d7a63f0d44702aae4cf0635a39aa63cded
    [root@yoyo nginx]# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
    1b4566a75078        nginx               "nginx -g 'daemon of…"   2 seconds ago       Up 1 second         0.0.0.0:8088->80/tcp                nginx-yoyo
    

    测试

    如果你是本地访问,输入http://localhost:8088 即可访问到 web 服务器

    通过其它机器访问服务器会报403错误,因为是将容器内的 nginx 的根目录给挂载到本地指定目录。

    进入本地目录 /root/nginx/html 创建测试文件 index.html 写入以下内容

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>nginx</title>
    </head>
    <body>
        <h1>hello world!</h1>
        <p>yoyo</p>
    </body>
    </html>
    

    浏览器输入http://ip:8088 即可访问了

  • 相关阅读:
    腾讯微博
    城市左右选择添加按钮案例
    jQuery元素操作1
    动态创建表格
    五角星评论案例
    点击图片箭头回到顶部案例
    HDU1506: Largest Rectangle in a Histogram(最大子矩阵,好题动态优化左右边界)
    HDU1165: Eddy's research II(递推)
    HDU1158:Employment Planning(线性dp)
    HDU1081:To The Max(最大子矩阵,线性DP)
  • 原文地址:https://www.cnblogs.com/yoyoketang/p/11674950.html
Copyright © 2020-2023  润新知