访问docker中的nginx容器部署
发布日期:2021-05-07 23:44:05 浏览次数:9 分类:精选文章

本文共 5248 字,大约阅读时间需要 17 分钟。

1搜索nginx镜像

docker search nginx

2拉取nginx镜像

docker pull nginx

 3创建容器,设置端口映射、目录映射

# 在/root目录下创建nginx目录用于存储nginx数据信息mkdir ~/nginxcd ~/nginxmkdir confcd conf# 在~/nginx/conf/下创建nginx.conf文件,粘贴下面内容vim nginx.conf
user  nginx;worker_processes  1;error_log  /var/log/nginx/error.log warn;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;}
docker run -id --name=c_nginx \-p 80:80 \-v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \-v $PWD/logs:/var/log/nginx \-v $PWD/html:/usr/share/nginx/html \nginx

参数说明:  

 -p 80:80:将容器的 80端口映射到宿主机的 80 端口。容器就相当于一个小的linux,外网想访问容器的步骤,先是访问阿里云的安全组(如果用的是阿里云服务器的话),看是否放行了80端口,然后访问linux的防火墙,看是否放行80,然后再访问mysql容器,看是否放行80。所以这里要映射一下,映射就是把它暴露了,下面的nginx啥的都一样就不再说了。

 -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf:将主机当前目录下的 /conf/nginx.conf 挂载到容器的 :/etc/nginx/nginx.conf。配置目录

-v $PWD/logs:/var/log/nginx:将主机当前目录下的 logs 目录挂载到容器的/var/log/nginx。日志目录。

二 进行反向代理的操作

docker ps

查询出你的nginx容器的id号,结果如下:

CONTAINER ID   IMAGE       COMMAND                  CREATED       STATUS       PORTS                                       NAMES3929aa630523   nginx       "/docker-entrypoint.…"   2 hours ago   Up 2 hours   0.0.0.0:80->80/tcp, :::80->80/tcp           c_nginxcf152c11389b   redis       "docker-entrypoint.s…"   2 weeks ago   Up 2 weeks   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   c_redise24660ccf0b8   mysql:5.6   "docker-entrypoint.s…"   2 weeks ago   Up 2 weeks   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp   c_mysql

复制nginx的Id。

docker exec -it 3929aa630523 bash

以交互式进入容器

root@3929aa630523:/# which nginx/usr/sbin/nginx

查找nginx位置,因为-v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \ 挂载到了/etc/nginx/nginx.conf

cd /etc/nginx/root@3929aa630523:/etc/nginx# lsconf.d		koi-utf  mime.types  nginx.conf   uwsgi_paramsfastcgi_params	koi-win  modules     scgi_params  win-utfroot@3929aa630523:/etc/nginx# cat nginx.conf user  nginx;worker_processes  1;error_log  /var/log/nginx/error.log warn;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;}

include /etc/nginx/conf.d/*.conf;就是配置了一个子配置文件在这个地址

root@3929aa630523:/etc/nginx# cd conf.d/root@3929aa630523:/etc/nginx/conf.d# lsdefault.confroot@3929aa630523:/etc/nginx/conf.d# cat default.conf server {    listen       80;    listen  [::]:80;    server_name  localhost;    #charset koi8-r;    #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 ~ \.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;    #}}

 

在这里改就行了。

但是在想要使用vim时,会出现

root@3929aa630523:/etc/nginx/conf.d# vim default.conf bash: vim: command not found

这时候就需要安装vim。

可是当你敲apt-get install vim命令时,提示:

        Reading package lists... Done

        Building dependency tree       
        Reading state information... Done
        E: Unable to locate package vim

这时候需要敲:apt-get update,这个命令的作用是:同步 /etc/apt/sources.list 和 /etc/apt/sources.list.d 中列出的源的索引,这样才能获取到最新的软件包。

root@3929aa630523:/etc/nginx/conf.d# apt-get updateGet:1 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB]Get:2 http://deb.debian.org/debian buster InRelease [121 kB]Get:3 http://security.debian.org/debian-security buster/updates/main amd64 Packages [285 kB]Get:4 http://deb.debian.org/debian buster-updates InRelease [51.9 kB]Get:5 http://deb.debian.org/debian buster/main amd64 Packages [7907 kB]Get:6 http://deb.debian.org/debian buster-updates/main amd64 Packages [10.9 kB]                       Fetched 8441 kB in 4min 51s (29.0 kB/s)                                                               Reading package lists... Done

 等更新完毕以后再敲命令:apt-get install vim命令即可。

上一篇:springCloud使用(前半部)
下一篇:nginx

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2025年03月31日 09时54分02秒