1. 介绍
1.1 介绍
大家根福哥学会了使用Dockerfile创建Docker镜像的技巧了,那么我们现在搭建服务器环境就不需要再去下载软件的源代码了,也不需要编译安装了,更加不用操心软件和操作系统的各种兼容问题了。
今天福哥带着大家来安装nginx的环境,Nginx常用作web服务的出口服务器,代理各种应用服务器的功能集中到一起发布出去。
2. 镜像
2.1 tag
Nginx的标签有很多,福哥选择的是1.20版本。
2.2 结构
照例我们先把镜像拉取下来,然后启动一个容器,看看里面都有什么,弄明白了自己才好捣鼓啊!
2.2.1 拉取镜像
docker pull nginx:1.20
2.2.2 启动临时容器
docker run -tid --name nginx1.20 -h nginx1.20 nginx:1.20
2.2.3 切入临时容器
docker exec -ti nginx1.20 /bin/bash
2.2.4 查看结构
进入容器后,全局查找一番,发现原来nginx的配置文件路径在/etc/nginx/nginx.conf。
[root@dev ~]# docker exec -ti nginx1.20 /bin/bash
root@nginx1:/# find / -iname "*nginx*"
/etc/rc5.d/S01nginx
/etc/rc6.d/K01nginx
/etc/logrotate.d/nginx
/etc/init.d/nginx-debug
/etc/init.d/nginx
/etc/default/nginx-debug
/etc/default/nginx
/etc/systemd/system/multi-user.target.wants/nginx.service
/etc/rc0.d/K01nginx
/etc/rc1.d/K01nginx
/etc/rc2.d/S01nginx
/etc/rc3.d/S01nginx
/etc/rc4.d/S01nginx
/etc/nginx
/etc/nginx/nginx.conf
/usr/lib/nginx
/usr/sbin/nginx-debug
/usr/sbin/nginx
/usr/share/doc/nginx-module-xslt
/usr/share/doc/nginx-module-geoip
/usr/share/doc/nginx-module-image-filter
/usr/share/doc/nginx
/usr/share/doc/nginx-module-njs
/usr/share/nginx
/var/lib/dpkg/info/nginx-module-image-filter.list
/var/lib/dpkg/info/nginx-module-image-filter.md5sums
/var/lib/dpkg/info/nginx.postinst
/var/lib/dpkg/info/nginx-module-geoip.list
/var/lib/dpkg/info/nginx.md5sums
/var/lib/dpkg/info/nginx-module-njs.list
/var/lib/dpkg/info/nginx-module-njs.postinst
/var/lib/dpkg/info/nginx-module-image-filter.postinst
/var/lib/dpkg/info/nginx-module-geoip.postinst
/var/lib/dpkg/info/nginx-module-xslt.list
/var/lib/dpkg/info/nginx-module-njs.md5sums
/var/lib/dpkg/info/nginx.list
/var/lib/dpkg/info/nginx-module-xslt.md5sums
/var/lib/dpkg/info/nginx-module-xslt.postinst
/var/lib/dpkg/info/nginx.postrm
/var/lib/dpkg/info/nginx.prerm
/var/lib/dpkg/info/nginx-module-geoip.md5sums
/var/lib/dpkg/info/nginx.preinst
/var/lib/dpkg/info/nginx.conffiles
/var/lib/systemd/deb-systemd-helper-enabled/multi-user.target.wants/nginx.service
/var/lib/systemd/deb-systemd-helper-enabled/nginx.service.dsh-also
/var/lib/systemd/deb-systemd-helper-enabled/nginx-debug.service.dsh-also
/var/cache/nginx
/var/log/nginx
/lib/systemd/system/nginx.service
/lib/systemd/system/nginx-debug.service
/run/nginx.pid
find: '/proc/1/map_files': Operation not permitted
find: '/proc/30/map_files': Operation not permitted
find: '/proc/31/map_files': Operation not permitted
find: '/proc/32/map_files': Operation not permitted
find: '/proc/37/map_files': Operation not permitted
使用curl测试一下,可用看到nginx默认的首页。
2.2.5 停止临时容器
docker stop nginx1.20
2.2.6 删除临时容器
docker rm nginx1.20
3. Dockerfile
最后福哥把前面的设置命令整理到一起写成Dockerfile,这样大家就可以通过Dockerfile安装环境了。
3.1 nginx.conf
默认的nginx.conf是这样的,可用看出具体的配置文件在/etc/nginx/conf.d/下面。
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
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;
}
3.2 default.conf
进入conf.d目录下面发现只有一个default.conf配置文件,默认default.conf的内容是这样的。
server {
listen 80;
listen [::]:80;
server_name localhost;
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.phpnbsp;{
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.phpnbsp;{
# 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;
#}
}
可以看到默认的配置文件里面以及给出了apache版本的php环境以及fpm版本的php环境接入nginx的示例配置方法。
3.3 Dockerfile
创建Dockerfile,因为Nginx是配合其他应用服务使用的,所以这里福哥没有做什么调整,咱们以后再结合项目进行具体的定制调整吧。
FROM nginx:1.20
MAINTAINER Andy Bogate
MAINTAINER tongfu@tongfu.net
MAINTAINER https://tongfu.net/dockerfile
MAINTAINER 2021/5/24
MAINTAINER v1.0.0
EXPOSE 80 443
4. 总结
今天福哥带着大家使用Dockerfile搭建了Nginx微服务环境了,可以发现使用Dockerfile方式搭建环境我们真的只需要关心我们需要关心的部分,繁琐的编译参数、依赖库、环境参数等等一系列的问题基础镜像都给我们解决好了。
下一课,福哥会带着搭建学习使用Dockerfile搭建Nginx+PHP环境,敬请期待~~
https://m.tongfu.net/home/35/blog/513309.html