微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CentOS7 Docker Nginx部署及运行实例分析

本文小编为大家详细介绍“CentOS7 Docker Nginx部署及运行实例分析”,内容详细,步骤清晰,细节处理妥当,希望这篇“CentOS7 Docker Nginx部署及运行实例分析”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

1、资源准备

dockerfile文件

# "ported" by adam miller <[email protected]> from 
#  https://github.com/fedora-cloud/fedora-dockerfiles 
# 
# originally written for fedora-dockerfiles by 
#  scollier <[email protected]> 
 
from centos:centos7 
maintainer the centos project <[email protected]> 
 
run yum -y update; yum clean all 
run yum -y install epel-release tar ; yum clean all 
run yum -y install Nginx ; yum clean all 
add Nginx.conf /opt/deploy/Nginx/Nginx.conf 
run echo "daemon off;" >> /opt/deploy/Nginx/Nginx.conf 
#run curl https://git.centos.org/sources/httpd/c7/acf5cccf4afaecf3afeb18c50ae59fd5c6504910 \ 
#  | tar -xz -c /usr/local/Nginx/html \ 
#  --strip-components=1 
#run sed -i -e 's/apache/Nginx/g' -e '/apache_pb.gif/d' \  
#  /usr/local/Nginx/html/index.html 
 
expose 80 
 
#cmd [ "/usr/local/Nginx/sbin" ]

注意:路径需要在系统上面存在以及对应

Nginx.conf文件

# for more @R_566_4045@ion on configuration, see: 
#  * official english documentation: http://Nginx.org/en/docs/ 
#  * official russian documentation: http://Nginx.org/ru/docs/ 
 
user Nginx; 
worker_processes 1; 
 
error_log /usr/logs/Nginx/error.log; 
#error_log /var/log/Nginx/error.log notice; 
#error_log /var/log/Nginx/error.log info; 
 
pid    /run/Nginx.pid; 
 
 
events { 
  worker_connections 1024; 
} 
 
 
http { 
  include    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 /usr/logs/Nginx/access.log main; 
 
  sendfile    on; 
  #tcp_nopush   on; 
 
  #keepalive_timeout 0; 
  keepalive_timeout 65; 
 
  #gzip on; 
 
  # load modular configuration files from the /etc/Nginx/conf.d directory. 
  # see http://Nginx.org/en/docs/ngx_core_module.html#include 
  # for more @R_566_4045@ion. 
  #include /etc/Nginx/conf.d/*.conf; 
 
  index  index.html index.htm; 
 
  server { 
    listen    80; 
    server_name localhost; 
    root     /usr/share/Nginx/html; 
 
    #charset koi8-r; 
 
    #access_log /var/log/Nginx/host.access.log main; 
 
    location / { 
      autoindex on; 
    } 
 
    # redirect server error pages to the static page /40x.html 
    # 
    error_page 404       /404.html; 
    location = /40x.html { 
    } 
 
    # redirect server error pages to the static page /50x.html 
    # 
    error_page  500 502 503 504 /50x.html; 
    location = /50x.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; 
    #} 
  } 
 
 
  # another virtual host using mix of ip-, name-, and port-based configuration 
  # 
  #server { 
  #  listen    8000; 
  #  listen    somename:8080; 
  #  server_name somename alias another.alias; 
  #  root     html; 
 
  #  location / { 
  #  } 
  #} 
 
 
  # https server 
  # 
  #server { 
  #  listen    443; 
  #  server_name localhost; 
  #  root     html; 
 
  #  ssl         on; 
  #  ssl_certificate   cert.pem; 
  #  ssl_certificate_key cert.key; 
 
  #  ssl_session_timeout 5m; 
 
  #  ssl_protocols sslv2 sslv3 tlsv1; 
  #  ssl_ciphers high:!anull:!md5; 
  #  ssl_prefer_server_ciphers  on; 
 
  #  location / { 
  #  } 
  #} 
 
}

注意:路径需要在系统上面存在以及对应

2、执行构建镜像命令

复制代码 代码如下:


[root@localhost Nginx]# sudo docker build --rm --tag os7/Nginx:centos7 . 

执行结果截图:

CentOS7 Docker Nginx部署及运行实例分析

3、查看镜像是否安装构建成功 docker images

CentOS7 Docker Nginx部署及运行实例分析

4、创建容器 docker run -i -t -d -p 192.168.32.129:81:80 os7/Nginx /bin/bash

注意:192.168.32.129这个ip的话,则需要在/etc/hosts中添加

192.168.32.129     localhost

5、查看容器是否创建成功并启动 docker ps

CentOS7 Docker Nginx部署及运行实例分析

6、测试是否成功访问 curl http://192.168.32.129:81

CentOS7 Docker Nginx部署及运行实例分析

会出现这个拒绝连接,那怎么办呢?有办法解决的,我们先进入该容器里面

7、进入容器 docker exec -i -t small_hodgkin /bin/sh

CentOS7 Docker Nginx部署及运行实例分析

8、接着在容器里面执行(直接输入即可)

Nginx

9、在容器外面执行 curl http://192.168.32.129:81

CentOS7 Docker Nginx部署及运行实例分析

成功了。

10、再到虚拟机外面通过浏览器访问

CentOS7 Docker Nginx部署及运行实例分析

读到这里,这篇“CentOS7 Docker Nginx部署及运行实例分析”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容文章,欢迎关注编程之家行业资讯频道。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐