CentOS7 Docker Nginx部署及运行详解

 更新时间:2017年08月10日 15:51:34   作者:lvk618  
这篇文章主要介绍了CentOS7 Docker Nginx部署及运行详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

网上找了一些资料部署,出现不一样的问题,现在总结一下自己的部署流程。

1、资源准备

Dockerfile文件

# "ported" by Adam Miller <maxamillion@fedoraproject.org> from 
#  https://github.com/fedora-cloud/Fedora-Dockerfiles 
# 
# Originally written for Fedora-Dockerfiles by 
#  scollier <scollier@redhat.com> 
 
FROM centos:centos7 
MAINTAINER The CentOS Project <cloud-ops@centos.org> 
 
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 information 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 information. 
  #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 . 

执行结果截图:

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

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

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

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

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

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

nginx

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

成功了。

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

到此为止就成功了。

参考资料:https://github.com/CentOS/CentOS-Dockerfiles

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Apache Airflow 快速入门教程应用场景分析

    Apache Airflow 快速入门教程应用场景分析

    ApacheAirflow是一个用于编排、调度和监控工作流的开源平台,适用于ETL和MLOps用例,它通过有向无环图(DAG)定义管道,支持任务依赖关系、调度、错误处理和日志记录,本文介绍Apache Airflow 快速入门教程,感兴趣的朋友一起看看吧
    2024-12-12
  • Centos6.5全自动安装 vsftpd+dhcp+nfs+tftp

    Centos6.5全自动安装 vsftpd+dhcp+nfs+tftp

    本文主要记述了在Centos6.5中,如何配置无人值守安装vsftpd+dhcp+nfs+tftp,非常实用,希望对大家能有所帮助。
    2014-09-09
  • linux下php-fpm开启关闭使用方法

    linux下php-fpm开启关闭使用方法

    自php5.3.3开始,php源码中包含了php-fpm,不需要单独通过补丁的方式安装php-fpm,在源码安装的时候直接 configure 中增加参数 –enable-fpm即可,使用方法如下
    2014-03-03
  • centos 7 源码安装openssh的方法

    centos 7 源码安装openssh的方法

    这篇文章主要介绍了centos 7 源码安装openssh的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • 对linux下软件(库)的更新命令详解

    对linux下软件(库)的更新命令详解

    今天小编就为大家分享一篇对linux下软件(库)的更新命令详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07
  • 详解linux系统目录sys,tmp,usr,var!

    详解linux系统目录sys,tmp,usr,var!

    在本篇文章里小编给大家详解了关于linux系统目录,sys,tmp,usr,var!的相关知识点内容,有兴趣的朋友们参考下。
    2019-06-06
  • Linux内核设备驱动之proc文件系统笔记整理

    Linux内核设备驱动之proc文件系统笔记整理

    今天小编就为大家分享一篇关于Linux内核设备驱动之proc文件系统笔记整理,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • ubuntu 16.04 64位兼容32位程序三步曲

    ubuntu 16.04 64位兼容32位程序三步曲

    这篇文章主要介绍了ubuntu 16.04 64位兼容32位程序的三步,本文分步骤给大家介绍的非常详细,具有一定的参考借鉴价值 ,需要的朋友可以参考下
    2019-06-06
  • linux 普通用户切换成root免密码的实现

    linux 普通用户切换成root免密码的实现

    下面小编就为大家带来一篇linux 普通用户切换成root免密码的实现。小编觉得挺不错的。现在就分享给大家。也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • ubuntu下安装程序的三种方法总结(推荐)

    ubuntu下安装程序的三种方法总结(推荐)

    下面小编就为大家带来一篇ubuntu下安装程序的三种方法总结(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12

最新评论