Docker中运行nginx并挂载本地目录到镜像中的方法

1 从hup上pull镜像

docker pull nginx

2 创建将要挂载的目录

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

3 先要有配置文件才能启动容器

3.1 vim /data/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 65;

  #gzip on;

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

3.2 vim /data/nginx/conf.d/default.conf

server {
  listen    80;
  server_name localhost; 

  #charset koi8-r;
  #access_log /var/log/nginx/log/host.access.log main; 

  location / {
    root  /data/nginx/html;
    # root  /usr/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;
  #}
}

4 启动容器

#将容器中nginx的80端口映射到本地的81端口
docker run --name nginx81 -d -p 81:80 -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/logs:/var/log/nginx -v /data/nginx/conf.d:/etc/nginx/conf.d -d nginx:latest

5 查看启动的容器

[root@dc01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fa56f865bd26 nginx:latest "nginx -g 'daemon of…" 4 weeks ago Up 3 seconds 0.0.0.0:80->80/tcp vigilant_swirles
[root@dc01 ~]# 

6 网页访问nginx

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

(0)

相关推荐

  • Docker创建一个Nginx服务器的方法步骤

    运行环境: MAC Docker 版本: Docker version 17.12.0-ce, build c97c6d6 一.启动Nginx 服务器 启动Nginx 服务器,并进入模拟终端 docker run -p 8080:80 --name nginx_web -it nginx /bin/bash 二.了解Nginx 镜像的配置文件位置 日志文件位置:/var/log/nginx 配置文件位置: /etc/nginx 资源存放的位置: /usr/share/nginx/html 上面的

  • nginx 代理服务器配置双向证书验证的方法

    生成证书链 用脚本生成一个根证书, 一个中间证书(intermediate), 三个客户端证书. 脚本来源于(有修改) https://stackoverflow.com/questions/26759550/how-to-create-own-self-signed-root-certificate-and-intermediate-ca-to-be-importe 中间证书的域名为 localhost. #!/bin/bash -x set -e for C in `echo root-ca

  • shell脚本之nginx自动化脚本

    这个脚本,可以满足nginx开启,停止和重启的操作 #!/bin/bash . /etc/init.d/functions function usage() { echo $"usage:$0 {start|stop|restart}" exit 1 } function start() { /usr/local/nginx/sbin/nginx sleep 1 if [ `netstat -antlpe | grep nginx | wc -l` -ge 0 ];then actio

  • 为docker中的nginx配置https的方法步骤

    没有 https 加持的网站会逐渐地被浏览器标记为不安全的,所以为网站添加 https 已经变得刻不容缓.对于商业网站来说,花钱购买 SSL/TLS 证书并不是什么问题.但对于个人用户来说,如果能有免费的 SSL/TLS 证书可用将会是非常幸福的事情!Let's Encrypt 就是一个提供免费 SSL/TLS 证书的网站,由于其证书期限只有三个月,所以需要我们用自动化的方式去更新证书.本文将介绍如何为通过 docker 运行的 nginx 中的站点添加 https 支持,并自动完成证书的更新.

  • nginx代理服务器配置双向证书验证的方法

    生成证书链 用脚本生成一个根证书, 一个中间证书(intermediate), 三个客户端证书. 中间证书的域名为 localhost. #!/bin/bash -x set -e for C in `echo root-ca intermediate`; do mkdir $C cd $C mkdir certs crl newcerts private cd .. echo 1000 > $C/serial touch $C/index.txt $C/index.txt.attr echo

  • Django uwsgi Nginx 的生产环境部署详解

    配置生产环境 #setting.py 文件中 DEBUG = False # 生产环境 # 允许访问的域名,域名前加一个点表示允许访问该域名下的子域名,比如 www.zmrenwu.com. # test.zmrenwu.com 等二级域名同样允许访问.如果不加前面的点则只允许访问 zmrenwu.com ALLOWED_HOSTS = ["127.0.0.1",".blogzjl.site"] 创建Python虚拟环境 安装 virtualenv sudo pip

  • CentOS 7.3配置Nginx虚拟主机的方法步骤

    实验环境 一台最小化安装的CentOS 7.3虚拟机 配置基本环境 1. 安装nginx yum install -y epel-* yum isntall -y nginx vim 2. 建立虚机主机的站点根目录 mkdir /var/wwwroot mkdir /var/wwwroot/site1 mkdir /var/wwwroot/site2 echo -e "site1" >> /var/wwwroot/site1/index.html echo -e "

  • PHP-FPM和Nginx的通信机制详解

    PHP-FPM 介绍 CGI 协议与 FastCGI 协议 每种动态语言( PHP,Python 等)的代码文件需要通过对应的解析器才能被服务器识别,而 CGI 协议就是用来使解释器与服务器可以互相通信.PHP 文件在服务器上的解析需要用到 PHP 解释器,再加上对应的 CGI 协议,从而使服务器可以解析到 PHP 文件. 由于 CGI 的机制是每处理一个请求需要 fork 一个 CGI 进程,请求结束再kill掉这个进程,在实际应用上比较浪费资源,于是就出现了CGI 的改良版本 FastCGI

  • 利用Nginx反向代理解决跨域问题详解

    问题 在之前的分享的跨域资源共享的文章中,有提到要注意跨域时,如果要发送Cookie,Access-Control-Allow-Origin就不能设为*,必须指定明确的.与请求网页一致的域名.在此次项目开发中与他人协作中就遇到此类问题. 解决思路 一般来说,与后台利用CORS跨域资源共享将Access-Control-Allow-Origin设置为访问的域名即可,这个需要后台的配合,且有些浏览器是不支持的. 基于与合作方后台的配合,利用nginx方向代理来满足浏览器的同源策略来实现跨域 实现方法

  • 浅谈docker运行nginx为什么要使用daemon off

    很开心啊,遇到这个问题,就要讲讲docker容器的进程原理了,基本上了解过docker的人都清楚docker的几个隔离方式,那么进程同样是进行隔离. 问题 1.docker容器跑着为啥会挂掉? docker 容器默认会把容器内部第一个进程,也就是pid=1的程序作为docker容器是否正在运行的依据,如果docker 容器pid挂了,那么docker容器便会直接退出. 2.docker run的时候把command最为容器内部命令,如果你使用nginx,那么nginx程序将后台运行,这个时候ng

随机推荐