Docker中运行nginx

时间:2023-11-21 22:22:08


部分内容原文地址:

****: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"\]