部分内容原文地址:

CSDN:wuzhangweiss:Docker中运行nginx并挂载本地目录到镜像中



1.Docker中运行nginx

直接一条命令:

docker run -d -p 8020:80 --name nginx --net=host -v /etc/localtime:/etc/localtime -v /root/software/nginx/html:/usr/share/nginx/html -v /root/software/nginx/conf:/etc/nginx -v /root/software/nginx/logs:/var/log/nginx nginx

需注意点:

  • 日志位置:/var/log/nginx/
  • 配置文件位置:/etc/nginx/
  • 项目位置:/usr/share/nginx/html

直接拉取的docker hub的nginx镜像,相关目录内容如上,到时候若需要挂载到宿主机相对应目录时,要记得将nginx.conf中相关路径对应对。

如项目位置,nginx.cong中对应的位置应该是/usr/share/nginx/html,访问路径时,先访问容器内的路径,然后根据容器内的路径在去挂载目录寻找文件。

2.配置文件

2.1 nginx.conf

因为有时候,nginx用户缺少权限,将user修改为root,提升权限。

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;
}

2.2 default.conf

server {
listen 80;
server_name localhost; #charset koi8-r;
#access_log /var/log/nginx/host.access.log main; location / {
root /usr/share/nginx/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 /usr/share/nginx/html;
} }

3、docker的镜像可以挂什么卷

docker run --name nginx01 -p 80:80 -v /everything/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /everything/nginx/html:/usr/share/nginx/html --restart always -d nginx

docker创建nginx的命令,从何地能看到nginx.conf挂载什么卷,目录结构等。

dockerfile提供的hub.docker:

ENV NGINX\_VERSION 1.7.4  

\# All our runtime and build dependencies, in alphabetical order (to ease maintenance)
RUN buildDeps=" \\
ca-certificates \\ curl \\ gcc \\ libc-dev-bin \\ libc6-dev \\ libexpat1-dev \\ libfontconfig1-dev \\ libfreetype6-dev \\ libgd-dev \\ libgd2-dev \\ libgeoip-dev \\ libice-dev \\ libjbig-dev \\ libjpeg8-dev \\ liblzma-dev \\ libpcre3-dev \\ libperl-dev \\ libpng12-dev \\ libpthread-stubs0-dev \\ libsm-dev \\ libssl-dev \\ libssl-dev \\ libtiff5-dev \\ libvpx-dev \\ libx11-dev \\ libxau-dev \\ libxcb1-dev \\ libxdmcp-dev \\ libxml2-dev \\ libxpm-dev \\ libxslt1-dev \\ libxt-dev \\ linux-libc-dev \\ make \\ manpages-dev \\ x11proto-core-dev \\ x11proto-input-dev \\ x11proto-kb-dev \\ xtrans-dev \\ zlib1g-dev \\ "; \\
apt-get update && apt-get install \-y \--no-install-recommends $buildDeps && rm \-rf /var/lib/apt/lists/\* \\
&& curl \-SL "http://nginx.org/download/nginx-$NGINX\_VERSION.tar.gz" \-o nginx.tar.gz \\
&& curl \-SL "http://nginx.org/download/nginx-$NGINX\_VERSION.tar.gz.asc" \-o nginx.tar.gz.asc \\
&& gpg \--verify nginx.tar.gz.asc \\
&& mkdir \-p /usr/src/nginx \\
&& tar \-xvf nginx.tar.gz \-C /usr/src/nginx \--strip-components=1 \\
&& rm nginx.tar.gz\* \\
&& cd /usr/src/nginx \\
&& ./configure \\
\--user=www-data \\
\--group=www-data \\
\--prefix=/usr/local/nginx \\
\--conf-path=/etc/nginx.conf \\
\--http-log-path=/proc/self/fd/1 \\
\--error-log-path=/proc/self/fd/2 \\
\--with-http\_addition\_module \\
\--with-http\_auth\_request\_module \\
\--with-http\_dav\_module \\
\--with-http\_geoip\_module \\
\--with-http\_gzip\_static\_module \\
\--with-http\_image\_filter\_module \\
\--with-http\_perl\_module \\
\--with-http\_realip\_module \\
\--with-http\_spdy\_module \\
\--with-http\_ssl\_module \\
\--with-http\_stub\_status\_module \\
\--with-http\_sub\_module \\
\--with-http\_xslt\_module \\
\--with-ipv6 \\
\--with-mail \\
\--with-mail\_ssl\_module \\
\--with-pcre-jit \\
&& make \-j"$(nproc)" \\
&& make install \\
&& cd / \\
&& rm \-r /usr/src/nginx \\
&& chown \-R www-data:www-data /usr/local/nginx \\
&& { \\
echo; \\
echo '# stay in the foreground so Docker has a process to track'; \\
echo 'daemon off;'; \\
} \>> /etc/nginx.conf \\
&& apt-get purge \-y \--auto-remove $buildDeps ENV PATH /usr/local/nginx/sbin:$PATH
WORKDIR /usr/local/nginx/html \# TODO USER www-data EXPOSE 80
CMD \["nginx"\]

Docker中运行nginx的更多相关文章

  1. Docker中运行nginx并挂载本地目录到镜像中

    1.1 从hup上pull镜像1.2 创建将要挂载的目录1.3 先要有配置文件才能启动容器1.3.1 vim /data/nginx/conf/nginx.conf1.3.2 vim /data/ng ...

  2. 滚动 docker 中的 nginx 日志

    Nginx 自己没有处理日志的滚动问题,它把这个球踢给了使用者.一般情况下,你可以使用 logrotate 工具来完成这个任务,或者如果你愿意,你可以写各式各样的脚本完成同样的任务.本文笔者介绍如何滚 ...

  3. Docker中运行MySQL5.7并挂载宿主机目录到镜像

    原文:Docker中运行MySQL5.7并挂载宿主机目录到镜像 1.1 拉取mysql镜像 docker pull mysql:5.7 1.2 创建用于挂载的目录 mkdir -p /data/mys ...

  4. docker 运行jenkins及vue项目与springboot项目(五.jenkins打包springboot服务且在docker中运行)

    docker 运行jenkins及vue项目与springboot项目: 一.安装docker 二.docker运行jenkins为自动打包运行做准备 三.jenkins的使用及自动打包vue项目 四 ...

  5. 在docker中运行ASP.NET Core Web API应用程序

    本文是一篇指导快速演练的文章,将介绍在docker中运行一个ASP.NET Core Web API应用程序的基本步骤,在介绍的过程中,也会对docker的使用进行一些简单的描述.对于.NET Cor ...

  6. .NET Core Web 应用部署到 Docker 中运行

    环境介绍 : 虚拟机:VirtualBox 5.1.6 系 统:Ubuntu 16.04.1 LTS 系统准备完成后可以使用 sudo apt-get udpate 和 sudo apt-get up ...

  7. docker中运行ASP.NET Core Web API

    在docker中运行ASP.NET Core Web API应用程序 本文是一篇指导快速演练的文章,将介绍在docker中运行一个ASP.NET Core Web API应用程序的基本步骤,在介绍的过 ...

  8. 在Docker中运行torch版的neural style

    相关的代码都在Github上,请参见我的Github,https://github.com/lijingpeng/deep-learning-notes 敬请多多关注哈~~~ 在Docker中运行to ...

  9. ASP.NET Core 网站在Docker中运行

    Docker作为新一代的虚拟化方式,未来肯定会得到广泛的应用,传统虚拟机的部署方式要保证开发环境.测试环境.UAT环境.生产环境的依赖一致性,需要大量的运维人力,使用Docker我们可以实现一次部署, ...

随机推荐

  1. Linux内核源码分析之setup_arch (三)

    1. 前言 在 Linux内核源码分析之setup_arch (二) 中介绍了当前启动阶段的内存分配函数memblock_alloc,该内存分配函数在本篇将要介绍paging_init中用于页表和内存 ...

  2. windows使用sdelete安全的删除文件

    SDelete是一款来自于微软Sysinternals[1]的应用.使用SDelete可以安全的删除现有文件,以及安全地擦除磁盘的未分配部分中存在的数据(包括已经删除或加密的文件).SDelete使用 ...

  3. vue-element Form表单验证(表单验证没错却一直提示错误)

    在使用element-UI 的表单时,发生一个验证错误,例如已输入值但求验证纠错:       代码如下所示: <el-form :model="correction" :i ...

  4. 网易163 docker镜像

    $ sudo echo "DOCKER_OPTS=\"--registry-mirror=http://hub-mirror.c.163.com\"" > ...

  5. 如何根据不同业务场景调节 HPA 扩缩容灵敏度

    背景 在 K8s 1.18 之前,HPA 扩容是无法调整灵敏度的: 对于缩容,由 kube-controller-manager 的 --horizontal-pod-autoscaler-downs ...

  6. 手写实现JDK的动态代理

    Person接口 package com.zhoucong.proxy.jdk; public interface Person { // 寻找真爱 void findlove(); } 人物实现类 ...

  7. Lesson_strange_words2

    cap 大写字母 mechanical 机械的,力学的 optical 光学的,视觉的 charge 电荷,负载 couple 耦合的,联接的,成对的 charge-coupled device 电荷 ...

  8. Windows软件Everything的配置

    Everything配置 Everything是一款Windows下的搜索软件,怎么安装应该不难.这里说一下个人使用的两个习惯. 主要就两点,一个是快捷键,一个是搜索路径 1. 快捷键 配置快捷键,点 ...

  9. Spring Security OAuth2.0认证授权四:分布式系统认证授权

    Spring Security OAuth2.0认证授权系列文章 Spring Security OAuth2.0认证授权一:框架搭建和认证测试 Spring Security OAuth2.0认证授 ...

  10. Linux应急响应--入侵排查

    1.入侵者可能会删除机器的日志信息,可以查看日志信息是否还存在或者是否被清空,相关命令示例: ll -h /var/log/*  系统日志一般都存在/var/log下常用的系统日志如下:核心启动日志: ...