docker pull nginx:1.18.0

启动容器,看看内部文件结构:

docker run -it nginx:1.18.0 bash

root@3beb813ce5ea:/# find / -name nginx
/usr/share/doc/nginx
/usr/share/nginx 有html目录
/usr/sbin/nginx nginx文件
/usr/lib/nginx 有modules目录
/var/log/nginx 有error.log 和 access.log
/var/cache/nginx
/etc/init.d/nginx 文件
/etc/logrotate.d/nginx
/etc/default/nginx 文件
/etc/nginx 有nginx.conf文件和modules目录,指向/usr/lib/nginx的modules目录


用自带的配置文件启动nginx:
/usr/sbin/nginx -c /etc/nginx/nginx.conf

用ps命令查看起来的进程,发现ps命令没有预装。执行cat /etc/os-release命令,可以看出当前系统是Debian系统,安装软件需要用apt-get命令。

执行apt-get update && apt-get install procps net-tools命令,安装完成后,执行ps命令,可以看到一个master process,一个worker process。再执行netstat -antlp命令,发现监听80端口。

仔细看/etc/nginx/nginx.conf文件,发现没有指定监听80端口和worker process个数的配置,再仔细看,发现引用了/etc/nginx/conf.d目录中所有以.conf结尾的文件,在/etc/nginx/conf.d目录中只有一个default.conf文件,default.conf文件中有一个server块,监听了80端口,指定worker process个数为1。执行curl 127.0.0.1:80,正常响应,nginx服务正常启动。

编写Dockerfile,内容如下:

FROM nginx:1.18.0
RUN apt-get update && apt-get install -y vim procps lsof curl wget net-tools iputils-ping telnet lrzsz \
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
WORKDIR /app
CMD /usr/sbin/nginx -c /etc/nginx/nginx.conf;touch a.txt;tail -f a.txt

执行Docker build -t my_nginx:1.18.0 -f Dockerfile .命令,构建my_nginx:1.18.0镜像。

执行docker run -d my_nginx:1.18.0命令,用默认配置文件启动nginx。登录容器,执行curl 127.0.0.1:80,正常响应,nginx服务正常启动。

执行docker run -d -p 8000:80 my_nginx:1.18.0命令,用默认配置文件启动nginx,且把宿主机的8000端口和容器的80端口绑定。在宿主机上执行curl 127.0.0.1:8000,正常响应。

自定义nginx.conf,内容为

worker_processes  5;

error_log  /app/logs/error.log;
pid        /app/nginx.pid;

events {
    use epoll;
    worker_connections  20000;
}

http {
    default_type  application/octet-stream;
    server_tokens on;

    sendfile        on;
    keepalive_timeout  65;

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

    server {
        listen       0.0.0.0:80;

        access_log  /app/logs/access.log access;    
    
	location / {
	    root   /usr/share/nginx/html;
	    index  index.html index.htm;
	}

        location /nginx_status {
           stub_status on;
           access_log  off;
           allow all;
        }
    }
}

把nginx.conf文件放在/Users/shengruikou/Desktop/nginx目录中,之后执行docker run -d -v /Users/shengruikou/Desktop/nginx:/etc/nginx -v /Users/shengruikou/Desktop/nginx/logs:/app/logs -p 8080:80 my_nginx:1.18.0命令启动容器,在宿主机执行lsof -i:8080命令,发现8080端口被监听,再执行curl 127.0.0.1:8080命令,正常响应。

至此,可根据自己的需要编辑/Users/shengruikou/Desktop/nginx目录中的nginx.conf,然后重启容器即可。

over。

相关文章:

  • 2022-12-23
  • 2021-12-27
  • 2022-12-23
  • 2021-09-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-29
  • 2022-12-23
  • 2021-07-30
  • 2022-12-23
相关资源
相似解决方案