
1.Dockerfile文件
# This my first nginx Dockerfile # Version 1.0 # Base images 基础镜像 FROM centos:centos7 #MAINTAINER 维护者信息 MAINTAINER fendo 2312892206@qq.com #ADD 获取url中的文件,放在当前目录下 ADD http://nginx.org/download/nginx-1.14.0.tar.gz . #RUN 执行以下命令 RUN yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel RUN useradd -M -s /sbin/nologin nginx RUN tar -zxvf nginx-1.14.0.tar.gz RUN mkdir -p /usr/local/nginx RUN cd nginx-1.14.0 && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install RUN ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ #EXPOSE 映射端口 EXPOSE 80 #CMD 运行以下命令 CMD ["nginx"]
2、执行构建镜像命令
[root@swarm01 docker]# docker build --rm --tag centos_nginx:centos7 .
3、查看镜像是否安装构建成功
[root@swarm01 docker]# docker images
4、启动容器
可以通过以下两种方式启动容器:
不挂载文件: docker run -i -t -d -p 192.168.182.110:80:80 centos_nginx /bin/bash 挂载文件: docker run \ --name centos_nginx \ -d -p 80:80 \ -v /home/nginx/html:/usr/share/nginx/html \ -v /home/nginx/log:/var/log/nginx \ -v /home/nginx/nginx.conf:/usr/local/nginx/nginx.conf:ro \ -v /home/nginx/conf.d:/usr/local/nginx/conf.d \ nginx
注意:
(1)第一个"-v",是项目位置,把项目放到挂载到的目录下即可;
(2)第二个"-v",是挂载的主配置文件"nginx.conf",注意"nginx.conf"文件内有一行"include /etc/nginx/conf.d/*.conf;",这个include指向了子配置文件的路径,此处注意include后所跟的路径一定不要出错。
(3)第三个"-v",把docker内子配置文件的路径也挂载了出来,注意要与(2)中include指向路径一致
(4)重点强调一下,nginx.conf是挂载了一个文件(docker是不推荐这样用的),conf.d挂载的是一个目录
启动成功之后如下:
5.在系统中创建Nginx配置文件
通过上面的命令启动之后,会在/home/nginx目录下多出以下文件
这些文件就是与Docker容器共享的配置文件,在nginx.conf目录下创建Nginx配置文件nginx.conf,内容如下:
user%20%20root;%20%20 worker_processes%20%201;%20%20 error_log%20%20/var/log/nginx/error.log%20warn;%20%20 pid%20%20%20%20%20%20%20%20/var/run/nginx.pid;%20%20 %20%20 events%20{%20%20 %20%20%20%20worker_connections%20%201024;%20%20 }%20%20 %20%20 http%20{%20%20 %20%20%20%20include%20%20%20%20%20%20%20/etc/nginx/mime.types;%20%20 %20%20%20%20default_type%20%20application/octet-stream;%20%20 %20%20 %20%20%20%20log_format%20%20main%20%20'$remote_addr%20-%20$remote_user%20[$time_local]%20"$request"%20'%20%20 %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20'$status%20$body_bytes_sent%20"$http_referer"%20'%20%20 %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20'"$http_user_agent"%20"$http_x_forwarded_for"';%20%20 %20%20%20%20access_log%20%20/var/log/nginx/access.log%20%20main;%20%20 %20%20 %20%20%20%20sendfile%20%20%20%20%20%20%20%20on;%20%20 %20%20%20%20#tcp_nopush%20%20%20%20%20on;%20%20 %20%20 %20%20%20%20keepalive_timeout%20%2065;%20%20 %20%20%20%20autoindex%20%20on;%20%20 %20%20%20%20#gzip%20%20on;%20%20 %20%20%20%20include%20/etc/nginx/conf.d/*.conf;%20%20 %20%20%20%20client_max_body_size%20100M;%20%20 %20%20%20%20client_header_buffer_size%20%20%20%20128k;%20%20 %20%20%20%20large_client_header_buffers%20%204%20%20128k;%20%20 }%20
然后在conf.d目录下创建default.conf文件,内容如下:
server%20{%20%20 %20%20%20%20listen%20%20%20%20%20%20%2080;%20%20 %20%20%20%20server_name%20%20localhost;%20%20 %20%20 %20%20%20%20#charset%20koi8-r;%20%20 %20%20%20%20#access_log%20%20/var/log/nginx/log/host.access.log%20%20main;%20%20 %20%20 %20%20%20%20location%20/%20{%20%20 %20%20%20%20%20%20%20%20root%20%20%20/home/nginx/html/fendo;%20%20 %20%20%20%20%20%20%20%20index%20%20index.html%20index.htm;%20%20 %20%20%20%20%20%20%20%20autoindex%20%20on;%20%20 %20%20%20%20%20%20%20%20try_files%20$uri%20/index/index/page.html;%20%20 %20%20%20%20%20%20%20%20#try_files%20$uri%20/index/map/page.html;%20%20 %20%20%20%20}%20%20 %20%20 %20%20%20%20#error_page%20%20404%20%20%20%20%20%20%20%20%20%20%20%20%20%20/404.html;%20%20 %20%20 %20%20%20%20#%20redirect%20server%20error%20pages%20to%20the%20static%20page%20/50x.html%20%20 %20%20%20%20#%20%20 %20%20%20%20error_page%20%20%20500%20502%20503%20504%20%20/50x.html;%20%20 %20%20%20%20location%20=%20/50x.html%20{%20%20 %20%20%20%20%20%20%20%20root%20%20%20/usr/share/nginx/html;%20%20 %20%20%20%20}%20%20 %20%20 %20%20%20%20#%20proxy%20the%20PHP%20scripts%20to%20Apache%20listening%20on%20127.0.0.1:80%20%20 %20%20%20%20#%20%20 %20%20%20%20#location%20~%20\.php$%20{%20%20 %20%20%20%20#%20%20%20%20proxy_pass%20%20%20http://127.0.0.1;%20%20 %20%20%20%20#}%20%20 %20%20 %20%20%20%20#%20pass%20the%20PHP%20scripts%20to%20FastCGI%20server%20listening%20on%20127.0.0.1:9000%20%20 %20%20%20%20#%20%20 %20%20%20%20#location%20~%20\.php$%20{%20%20 %20%20%20%20#%20%20%20%20root%20%20%20%20%20%20%20%20%20%20%20html;%20%20 %20%20%20%20#%20%20%20%20fastcgi_pass%20%20%20127.0.0.1:9000;%20%20 %20%20%20%20#%20%20%20%20fastcgi_index%20%20index.php;%20%20 %20%20%20%20#%20%20%20%20fastcgi_param%20%20SCRIPT_FILENAME%20%20/scripts$fastcgi_script_name;%20%20 %20%20%20%20#%20%20%20%20include%20%20%20%20%20%20%20%20fastcgi_params;%20%20 %20%20%20%20#}%20%20 %20%20 %20%20%20%20#%20deny%20access%20to%20.htaccess%20files,%20if%20Apache's%20document%20root%20%20 %20%20%20%20#%20concurs%20with%20nginx's%20one%20%20 %20%20%20%20#%20%20 %20%20%20%20#location%20~%20/\.ht%20{%20%20 %20%20%20%20#%20%20%20%20deny%20%20all;%20%20 %20%20%20%20#}%20%20 }
然后在/home/nginx/fendo目录下创建Index.html文件,内容为:
<!DOCTYPE%20html> <html> <head> <title>Welcome%20to%20nginx!%20This%20is%20a%20demo!</title> <style> %20%20%20%20body%20{ %20%20%20%20%20%20%20%20width:%2035em; %20%20%20%20%20%20%20%20margin:%200%20auto; %20%20%20%20%20%20%20%20font-family:%20Tahoma,%20Verdana,%20Arial,%20sans-serif; %20%20%20%20} </style> </head> <body> <h1>***********************************%20Welcome%20to%20nginx!%20Fendo%20***********************************</h1> <p>If%20you%20see%20this%20page,%20the%20nginx%20web%20server%20is%20successfully%20installed%20and working.%20Further%20configuration%20is%20required.</p> <p>For%20online%20documentation%20and%20support%20please%20refer%20to <a%20href="http://nginx.org/">nginx.org</a>.<br/> Commercial%20support%20is%20available%20at <a%20href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank%20you%20for%20using%20nginx.</em></p> </body> </html>
修改好之后,重启下Nginx
[root@swarm01%20conf.d]#%20docker%20restart%20centos_nginx centos_nginx
6.测试Nginx
访问http://192.168.182.110/fendo/效果如下:
7.一些错误
7.1、访问 curl http://192.168.182.110/fendo/出现拒绝连接,解决方法就是进入容器 docker exec -i -t centos_nginx /bin/sh接着在容器里面执行(直接输入即可)nginx 启动Nginx。
7.2、如过挂载Nginx文件时报错,基本就是路径的问题。
人吐槽 | 人点赞 |
发表评论