余晖落尽暮晚霞,黄昏迟暮远山寻
本站
当前位置:网站首页 > 编程知识 > 正文

使用Dockerfile基于1.20镜像搭建Nginx微服务环境

xiyangw 2023-05-14 11:15 10 浏览 0 评论

1. 介绍

1.1 介绍

大家根福哥学会了使用Dockerfile创建Docker镜像的技巧了,那么我们现在搭建服务器环境就不需要再去下载软件的源代码了,也不需要编译安装了,更加不用操心软件和操作系统的各种兼容问题了。

使用Dockerfile基于1.20镜像搭建Nginx微服务环境

今天福哥带着大家来安装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

相关推荐

辞旧迎新,新手使用Containerd时的几点须知

相信大家在2020年岁末都被Kubernetes即将抛弃Docker的消息刷屏了。事实上作为接替Docker运行时的Containerd在早在Kubernetes1.7时就能直接与Kubelet集成使...

分布式日志系统ELK+skywalking分布式链路完整搭建流程

开头在分布式系统中,日志跟踪是一件很令程序员头疼的问题,在遇到生产问题时,如果是多节点需要打开多节点服务器去跟踪问题,如果下游也是多节点且调用多个服务,那就更麻烦,再者,如果没有分布式链路,在生产日志...

Linux用户和用户组管理

1、用户账户概述-AAA介绍AAA指的是Authentication、Authorization、Accounting,即认证、授权和审计。?认证:验证用户是否可以获得权限,是3A的第一步,即验证身份...

linux查看最后N条日志

其实很简单,只需要用到tail这个命令tail-100catalina.out输入以上命令,就能列出catalina.out的最后100行。...

解决linux系统日志时间错误的问题

今天发现一台虚拟机下的系统日志:/var/log/messages,文件时间戳不对,跟正常时间差了12个小时。按网上说的执行了servicersyslogrestart重启syslog服务,还是不...

全程软件测试(六十二):软件测试工作如何运用Linux—读书笔记

从事过软件测试的小伙们就会明白会使用Linux是多么重要的一件事,工作时需要用到,面试时会被问到,简历中需要写到。对于软件测试人员来说,不需要你多么熟练使用Linux所有命令,也不需要你对Linux...

Linux运维之为Nginx添加错误日志(error_log)配置

Nginx错误日志信息介绍配置记录Nginx的错误信息是调试Nginx服务的重要手段,属于核心功能模块(nginx_core_module)的参数,该参数名字为error_log,可以放在不同的虚机主...

Linux使用swatchdog实时监控日志文件的变化

1.前言本教程主要讲解在Linux系统中如何使用swatchdog实时监控日志文件的变化。swatchdog(SimpleWATCHDOG)是一个简单的Perl脚本,用于监视类Unix系统(比如...

syslog服务详解

背景:需求来自于一个客户想将服务器的日志转发到自己的日志服务器上,所以希望我们能提供这个转发的功能,同时还要满足syslog协议。1什么是syslog服务1.1syslog标准协议如下图这里的fa...

linux日志文件的管理、备份及日志服务器的搭建

日志文件存放目录:/var/log[root@xinglog]#cd/var/log[root@xinglog]#lsmessages:系统日志secure:登录日志———————————...

运维之日志管理简介

日志简介在运维过程中,日志是必不可少的东西,通过日志可以快速发现问题所在。日志分类日志分类,对不同的日志进行不同维度的分析。操作系统日志操作系统是基础,应用都是在其之上;操作系统日志的分析,可以反馈出...

Apache Log4j 爆核弹级漏洞,Spring Boot 默认日志框架就能完美躲过

这两天沸沸扬扬的Log4j2漏洞门事件炒得热火朝天:突发!ApacheLog4j2报核弹级漏洞。。赶紧修复!!|Java技术栈|Java|SpringBoot|Spring...

Linux服务器存在大量log日志,如何快速定位错误?

来源:blog.csdn.net/nan1996jiang/articlep/details/109550303针对大量log日志快速定位错误地方tail/head简单命令使用:附加针对大量log日志...

Linux中查看日志文件的正确姿势,求你别tail走天下了!

作为一个后端开发工程师,在Linux中查看查看文件内容是基本操作了。尤其是通常要分析日志文件排查问题,那么我们应该如何正确打开日志文件呢?对于我这种小菜鸡来说,第一反应就是cat,tail,vi(或...

分享几款常用的付费日志系统,献给迷茫的你!

概述在前一篇文章中,我们分享了几款免费的日志服务器。他们各有各的特点,但是大家有不同的需求,有时免费的服务器不能满足大家的需要,下面推荐几款付费的日志服务器。1.Nagios日志服务器Nagio...

取消回复欢迎 发表评论: