Docker nginx

来自linux中国网wiki
Evan讨论 | 贡献2019年10月14日 (一) 13:48的版本 (导入1个版本)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

直接拉的

只挂载web目录

#最好指定版本
#docker pull nginx
#这个才够小巧
docker pull nginx:1.16-alpine

[root@localhost npm4compose]# docker images 
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
nginx                   1.16-alpine         ef04b00b089d        4 weeks ago         20.4MB #其它的要100M

mkdir -p /data/nginx/html
docker  run  --name  nginx1.16   -p  80:80  -d  -v /data/nginx/html:/usr/share/nginx/html ef04b00b089d
目录什么的和下面一样


mkdir  /data/nginx/html -p
docker  run --name  mynginx  -p  80:80  -d  -v /data/nginx/html:/usr/share/nginx/html nginx
[root@localhost ~]# cat  /data/nginx/html/index.html
  hello
#直接访问就可以了 改web目录而已要做的事

挂载web目录 配置目录 log目录等等

在用户的家目录下创建 nginx 目录及其子目录 conf.d、conf.crt 和 html,创建 logs 目录及其子目录 nginx 和 letsencrypt:

$ mkdir -p nginx/{conf.d,conf.crt,html}
$ mkdir -p logs/{nginx,letsencrypt}

创建 nginx/nginx.conf 文件,内容如下:

复制代码
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  2048;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout    65;
    client_max_body_size 10M;

    include /etc/nginx/conf.d/*.conf;
}
复制代码
然后创建 nginx/conf.d/default.conf 文件,内容如下:

复制代码
upstream web{
    server myweb:3000;
}
server {
    listen      80;
    listen      [::]:80;
    server_name filterinto.com www.filterinto.com;

    location ^~ /.well-known/acme-challenge/ {
        default_type "text/plain";
        root /usr/share/nginx/html;
    }
    location = /.well-known/acme-challenge/ {
        return 404;
    }
    location / {
        proxy_pass http://web;
    }
}
复制代码
其中 /.well-known/acme-challenge/ 目录是 certbot 工具在生成证书时创建的。接下来创建文件 nginx/html/index.html 文件,内容如下:

复制代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Let's Encrypt First Time Cert Issue Site</title>
</head>
<body>
    <h1>Hello HTTPS!</h1>
    <p>
        Just used for the very first time SSL certificates are issued by Let's Encrypt's
        certbot.
    </p>
</body>
</html>
复制代码
这个页面也是 certbot 在生成证书时需要用到的。最后让我们启动容器(在用户的家目录下执行下面的命令):

复制代码
$ docker run -d \
    -p 80:80 \
    -v $(pwd)/nginx/conf.d:/etc/nginx/conf.d:ro \
    -v $(pwd)/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
    -v $(pwd)/logs/nginx:/var/log/nginx \
    -v $(pwd)/nginx/html:/usr/share/nginx/html \
    --restart=always \
    --name=gateway \
    --network=webnet \
    nginx:1.14

为 docker 中的 nginx 配置 https

https://docs.docker.com/samples/library/nginx/


https://www.nginx.com/blog/deploying-nginx-nginx-plus-docker/ 有意思Docker部署(五):Nginx and 反向代理配置

build

#
# Dockerfile for building Nginx images
# 
# https://github.com/evan886/docker-lnmp.git
#

FROM centos:centos7
MAINTAINER evan <[email protected]>

ENV TZ "Asia/Shanghai"

#Yum
RUN yum -y update && \
    yum install -y gcc automake autoconf libtool make gcc-c++ vixie-cron  wget zlib  file openssl-devel sharutils zip  bash vim cyrus-sasl-devel libmemcached libmemcached-devel libyaml libyaml-devel unzip libvpx-devel openssl-devel ImageMagick-devel  autoconf  tar gcc libxml2-devel gd-devel libmcrypt-devel libmcrypt mcrypt mhash libmcrypt libmcrypt-devel libxml2 libxml2-devel bzip2 bzip2-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype-devel bison libtool-ltdl-devel net-tools && \
    yum clean all

#Nginx
RUN mkdir -p /data/apps/nginx && cd /tmp && \
  wget http://nginx.org/download/nginx-1.11.5.tar.gz && \
  tar xzf nginx-1.11.5.tar.gz && \
  cd /tmp/nginx-1.11.5 && \
  ./configure \
   --prefix=/data/apps/nginx  &&  \
   make -j2    &&  make install
    # make install
    #--prefix=/data/apps/nginx \
    #--with-http_ssl_module --with-http_sub_module --with-http_dav_module --with-http_flv_module \
    #--with-http_gzip_static_module --with-http_stub_status_module --with-debug && \
    #make  -j2   && \
    #make install

#配置nginx
#ENV HTTP_PHP_CONFIG \\\n\\\t#php\\\n\\\tlocation ~ \\\\.php$ {\\\n\\\t\\\troot    html;\\\n\\\t\\\tfastcgi_pass   php7:9000;\\\n\\\t\\\tfastcgi_index    index.php;\\\n\\\t\\\tfastcgi_param  SCRIPT_FILENAME    /usr/local/nginx/html\$fastcgi_script_name;\\\n\\\t\\\tinclude    fastcgi_params;\\\n\\\t}\\\n\\\n\\\t

#RUN sed -i -e "s@# deny access to .htaccess files, if Apache@${HTTP_PHP_CONFIG}# deny access to .htaccess files, if Apache@" /usr/local/nginx/conf/nginx.conf


EXPOSE 80 443

#启动nginx
ENTRYPOINT ["/data/apps/nginx/sbin/nginx", "-g", "daemon off;"]


docker build --tag evan886/centos-nginx:v1 -f nginx/Dockerfile .


docker run --name nginx -p 80:80 -v /data/apps/nginx/html/:/data/apps/nginx/html/ -d  -it 9a4ccef0484d

登入容器:
docker exec -it jenkins /bin/bash


docker push evan886/centos-nginx:v1





on debian

mkdir nginx
cd nginx

# 改国内源
vi  sources.list 
deb http://mirrors.cloud.tencent.com/debian stretch main contrib non-free
deb http://mirrors.cloud.tencent.com/debian stretch-updates main contrib non-free
#deb http://mirrors.cloud.tencent.com/debian stretch-backports main contrib non-free
#deb http://mirrors.cloud.tencent.com/debian stretch-proposed-updates main contrib non-free
deb-src http://mirrors.cloud.tencent.com/debian stretch main contrib non-free
deb-src http://mirrors.cloud.tencent.com/debian stretch-updates main contrib non-free
#deb-src http://mirrors.cloud.tencent.com/debian stretch-backports main contrib non-free
#deb-src http://mirrors.cloud.tencent.com/debian stretch-proposed-updates main contrib non-free


wget -c https://nginx.org/download/nginx-1.12.2.tar.gz

vi Dockerfile

#For Debian 9## 指定镜像
FROM debian:stretch-slim
# 指定管理员
MAINTAINER  linuxsa.org
# 执行命令安装编译库文件
RUN rm  -f /etc/apt/sources.list  
ADD  sources.list /etc/apt/sources.list

RUN apt-get clean all && apt update && apt install -y gcc gcc g++ make openssl libssl-dev  libpcre3 libpcre3-dev 
#RUN yum install -y gcc gcc-c++ make openssl-devel pcre-devel

# RUN apt-get update && apt-get install -y --no-install-recommends g++ gcc libc6-dev make && rm -rf /var/lib/apt/lists*
# 添加解压nginx包到/tmp目录下
RUN mkdir -p /tmp
ADD nginx-1.12.2.tar.gz /tmp
# 不用什么tar 解压 ,进入目录进行编译安装
RUN cd /tmp/nginx-1.12.2 && ./configure --prefix=/usr/local/nginx --without-http_gzip_module  && make -j 2 && make install

# 删除容器内置配置文件
RUN rm -f /usr/local/nginx/conf/nginx.conf 
# 复制本地配置文件到容器内
COPY nginx.conf /usr/local/nginx/conf
# 声明暴露端口
EXPOSE 80
# 启动容器Nginx服务,指定全局命令daemon off保证服务在前台运行不会关闭
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]


vi nginx.conf #这个要优化一下
user  root; 
worker_processes  auto; 

error_log  logs/error.log  info;

pid        logs/nginx.pid; 


events {
    use epoll; 
}

http {

    include       mime.types;
    default_type  application/octet-stream;

    log_format  main '$upstream_addr $remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log logs/access.log main;
    sendfile        on;
    keepalive_timeout  65;

# 动静分离
    server {
        listen 80;
        server_name localhost;

# 动态请求转发给tomcat处理
    location / {
        proxy_pass http://www.example.com;
    }
# 静态资源请求交给nginx处理
        location ~ \.(html|css|js|jpg|png|gif)$ {
            root /opt/webapps/ROOT;
        }
    }
}


域名配置

当然  ngignx 配置文件要 -v 

docker run –name=nginx -p 80:80 -v /nginx/conf.d:/etc/nginx/conf.d -d nginx

server {
    listen       80;
    server_name www.aaa.com自己域名;
    location / {
       proxy_pass http://宿主机ip:容器对外的端口号; #就可以mediawiki的喽
      }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

或者作dns A 母机
打开你的域名即可看到效果,当然你得先把域名解析到宿主机的ip上面。

docker搭建nginx实现反代 配置多域名以及多端口号

Troubleshooting

An image does not exist locally

[root@localhost ~]# docker push evan886/centos-nginx:v1
The push refers to repository [docker.io/evan886/centos-nginx]
An image does not exist locally with the tag: evan886/centos-nginx

我写错了 应该是 
docker push evan886/nginx:v1

docker run --name nginx -p 80:80 -v /var/www/html:/usr/local/nginx/html -d  -it imagesid

docker run --name nginx -p 80:80 -v /var/www/html:/usr/local/nginx/html  -d -it evan886/nginx:v1

docker nginx 503

打开503 404 什么的 在对应目录创建个html页面就好了

nginx make 少了&&

mkdir -p /data/apps/nginx && cd /tmp && \
  wget http://nginx.org/download/nginx-1.11.5.tar.gz && \
  tar xzf nginx-1.11.5.tar.gz && \
  cd /tmp/nginx-1.11.5 && \
  ./configure    --prefix=/data/apps/nginx  && \ #一开始这里少了&&
   make -j 2  &&  make install

Docker pull 出现的 http: TLS handshake timeout

http://www.cnblogs.com/wozixiaoyao/p/6059780.html

mysql err

WARNING: The following packages cannot be authenticated!
  mysql-community-client mysql-client mysql-community-server mysql-server
E: There were unauthenticated packages and -y was used without --allow-unauthenticated

更新源


进入容器后,首先更新一下镜像源,这样下载速度会快很多,输入以下命令:

 

set -ex \ && sed -i '[email protected]@mirrors.aliyun.com@' /etc/apt/sources.list
set -ex \ && sed -i '[email protected]@mirrors.aliyun.com@' /etc/apt/sources.list
apt-get update


参考

Compile Nginx From Source on Ubuntu

Nginx 1.15.X Linux Debian/Ubuntu 源码编译安装 LNMP之Nginx

源码编译安装Nginx

Docker多容器部署LNMP环境

Docker基础-使用Dockerfile创建nginx镜像

Nginx 容器教程

https://docs.docker.com/samples/library/nginx/


dockerfile debian:stretch-

Docker容器的Nginx实践

Deploying NGINX and NGINX Plus on Docker

docker运行nginx为什么要使用 nginx -g 'daemon off;'

docker 使用入门,创建一个Nginx服务器

Docker(三)----Dockerfile搭建Nginx环境与文件挂载

Docker nginx安装与配置挂载

使用docker部署nginx容器

docker之Dockerfile实践用dockerfile构建nginx环境


docker 部署简单nginx服务器


使用dockerfile构建nginx镜像

Docker部署nginx并修改配置文件

Docker 通过Nginx镜像部署Vue项目

docker搭建nginx实现反代 配置多域名以及多端口号

为 docker 中的 nginx 配置 https

docker中使用nginx容器代理其他容器

docker下nginx反向代理和负载均衡配置