Docker-CE用nginx:latest镜像部署web项目

Posted LiuJun2Son

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker-CE用nginx:latest镜像部署web项目相关的知识,希望对你有一定的参考价值。

1.加载本地的nginx镜像

docker load --input nginx.tar

[root@VM_0_6_centos images-bak]# ls
centos8.tar  nginx.tar 
[root@VM_0_6_centos images-bak]# 

[root@VM_0_6_centos images-bak]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx18             v1                  828b659f7a77        10 days ago         487MB
centos              latest              0d120b6ccaa8        3 weeks ago         215MB


[root@VM_0_6_centos images-bak]# docker load --input nginx.tar
Loaded image: nginx:latest


[root@VM_0_6_centos images-bak]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              d589942fb7ff        7 days ago          487MB
nginx18             v1                  828b659f7a77        10 days ago         487MB
centos              latest              0d120b6ccaa8        3 weeks ago         215MB
[root@VM_0_6_centos images-bak]# 

2.编写启动 nginx:latest 镜像脚本

1.新建一个 run_nginx.sh 脚本文件


docker rm -f  nginx
docker run \\
  --name nginx \\
  --restart=always \\
  -p 8090:80 \\
  -p 9090:9090 \\
  -v /data/nginx/conf/nginx.conf:/usr/local/webserver/nginx/conf/nginx.conf \\
  -v /data/nginx/conf.d:/usr/local/webserver/nginx/conf.d \\
  -v /data/nginx/html:/usr/local/webserver/nginx/html \\
  -e "MINIO_ACCESS_KEY=admin" \\
  -e "MINIO_SECRET_KEY=123456" \\
  -d nginx:latest
  

2.该脚本文件添加执行权限

chmod +x run_nginx.sh 

3.准备部署的资源

下面三个资源文件 都会 映射 到镜像中对应的文件

/data/nginx/conf/nginx.conf // nginx 的配置文件
/data/nginx/conf.d // web网站的 nginx 的配置
/data/nginx/html  // 部署网页的存放文件夹

/data/nginx/conf/nginx.conf

#user  nobody;
worker_processes  8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_rlimit_nofile 102400;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
daemon off;

events 
    worker_connections  102400;
    multi_accept on;



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  logs/access.log  main;

    sendfile        on;
    tcp_nopush     on;
	underscores_in_headers on;
	
    # 打开文件缓存  
    open_file_cache max=102400 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 1;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    # gzip
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;

    # http header buffer
    client_max_body_size 50m;
    client_body_buffer_size 128k;
    large_client_header_buffers 4 4k;
    client_header_buffer_size 4k;

    server 
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / 
            root   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   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;

    #    location / 
    #        root   html;
    #        index  index.html index.htm;
    #    
    #


    # HTTPS server
    #
    #server 
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / 
    #        root   html;
    #        index  index.html index.htm;
    #    
    #
    
    # load other website config( make by liujun ) 
    include  /usr/local/webserver/nginx/conf.d/*.conf;
    # include  /data/nginx/conf.d/*.conf;


/data/nginx/conf.d/test.conf

server 
    listen       9090;
    server_name  localhost;
    #charset koi8-r;
    #access_log  logs/host.access.log  main;

    # 1.入口
    location / 
        root   /usr/local/webserver/nginx/html/vueTest;
        # vue工程用的路由是history模式
        try_files $uri $uri/ /index.html;
        index  index.html index.htm;
    


    # 4.静态资源使用缓存
    #    ( ~ 表示匹配 URI 时是字母大小写敏感的; ~* 大小写不敏感; \\. 是转译.)
    location ~* \\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ 
	root /usr/local/webserver/nginx/html/vueTest;
        # vue工程用的路由是history模式
        try_files $uri $uri/ /index.html;
        index  index.html index.htm;
        # 缓存时间
        expires  30d;
    

    

    #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   html;
    


/data/nginx/html

[root@VM_0_6_centos nginx]# cd html/
[root@VM_0_6_centos html]# ls
50x.html  index.html  vueTest
[root@VM_0_6_centos html]# cd vueTest/
[root@VM_0_6_centos vueTest]# ls
index.html
[root@VM_0_6_centos vueTest]# 

4.执行运行 nginx:latest 镜像的脚本

./run-nginx.sh

[root@VM_0_6_centos images-bak]# ls
centos8.tar  nginx.tar  run-nginx.sh
[root@VM_0_6_centos images-bak]# ./run-nginx.sh
Error: No such container: nginx
8a3689224822b86929d3db89a0e212895382bd528f98d65358357a57755783df
[root@VM_0_6_centos images-bak]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                          NAMES
8a3689224822        nginx:latest        "/bin/sh -c /usr/loc…"   3 seconds ago       Up 3 seconds        0.0.0.0:9090->9090/tcp, 0.0.0.0:8090->80/tcp   nginx
[root@VM_0_6_centos images-bak]# 

5.测试容器中的 nginx 服务器

测试 8090 端口是否映射 到80 端口

[root@VM_0_6_centos images-bak]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                          NAMES
8a3689224822        nginx:latest        "/bin/sh -c /usr/loc…"   3 minutes ago       Up 3 minutes        0.0.0.0:9090->9090/tcp, 0.0.0.0:8090->80/tcp   nginx
[root@VM_0_6_centos images-bak]# curl localhost:8090
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body 
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

测试 9090 端口是否映射 到 9090 端口

[root@VM_0_6_centos images-bak]# curl localhost:9090
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title> title </title>
 <!-- Styles -->
<style>
#chartdiv 
  width: 100%;
  height: 500px;


    </style>
</head>
<body>
<!-- HTML -->
<div id="chartdiv">11111111111112</div>

<!-- Chart code -->
<script>


); // end am4core.ready()
</script>
</body>
</html>

以上是关于Docker-CE用nginx:latest镜像部署web项目的主要内容,如果未能解决你的问题,请参考以下文章

编写Dockerfile来构建nginx:latest镜像

编写Dockerfile来构建nginx:latest镜像

docker生成nginx镜像

如何删除无效的

docker上传保存本地镜像

docker 安装nginx