Docker安装nginx并把配置文件挂载出来

1:Docker pull 安装 Nginx

  1. 使用 docker search 命令搜索存放在 Docker Hub 中的镜像docker search nginx(小知识:OFFICIAL:[ok] (代表这个镜像是官方镜像))
    在这里插入图片描述

  2. 选定需要pull到系统中的官方 Nginx 镜像(如果不选择版本,默认选择最新版(latest))

# docker pull nginx -------- nginx 为选定需要pull到系统中的官方 nginx 镜像

docker pull nginx

在这里插入图片描述

  1. 启动docker中的nginx容器
docker run --name nginx -d -p 8008:80 -v /usr/docker/nginx/html:/usr/share/nginx/html nginx

在这里插入图片描述

  1. docker 启动 nginx 加载自定义配置

一般情况下docker启动时进行配置,只要把配置文件的目录挂载出来就可以,但是nginx却是先加载一个主配置文件nginx.conf,在nginx.conf里再加载conf.d目录下的子配置文件(一般最少一个default.conf文件)。

  • docker 启动 nginx 加载自定义配置:

挂载Nginx配置与静态目录
说明 :-p表示递归创建文件夹,这里挂载是为了后面配置Nginx方便,不创建挂载后面配置Nginx需要进入容器配置比较麻烦,所以挂载到宿主机

mkdir -p /usr/docker/nginx/html
mkdir -p /etc/docker/nginx/{conf.d,logs}
# 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. 第四个 “-v”是吧nginx的日志文件也挂载出来,方便查看
docker run \
  --name nginx \
  -d -p 8008:80  \
  -v /usr/docker/nginx/html:/usr/share/nginx/html \
  -v /etc/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
  -v /etc/docker/nginx/conf.d:/etc/nginx/conf.d \
  nginx
docker run \
  --name nginx \
  -d -p 8008:80 --net host \
  -v /usr/docker/nginx/html:/usr/share/nginx/html \
  -v /etc/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
  -v /etc/docker/nginx/conf.d:/etc/nginx/conf.d \
  nginx

注意!注意!注意!:
1、此处重中之重:
启动nginx容器时 一定要加 --net host 参数 (解释:容器将不会虚拟出自己的网卡,配置自己的IP等,而是使用宿主机的IP和端口。)
个人理解:如果不加此参数,nginx相当于是代理nginx镜像的IP及端口,因为nginx镜像也是独立的虚机,贴上此图,便于理解

在这里插入图片描述
在这里插入图片描述

  1. 首先创建这些文件夹及文件
  2. 准备挂载的 nginx.conf :
vim /etc/docker/nginx/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;
   server {
       listen       80;
       server_name  localhost;

       #charset koi8-r;

       #access_log  logs/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   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;
        #}
    }
    include /etc/nginx/conf.d/*.conf;
}
  1. 准备挂载的 default.conf :
vim /etc/docker/nginx/conf.d/default.conf
server {
    listen       80;
    listen  [::]:80;
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
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;
    #}
}

  1. 这样你就可以去外面配置nginx的配置了
  2. 最后提示:有些文件是需要自己创建的好,比括号里面的这个配置文件(/etc/docker/nginx/nginx.conf),等这些文件都配置好了,重新运行创建、启动容器命令(友情提示,如果一开始运行过创建、启动容器的话,可以先把那个容器删了,因为你容器名字重了,会报错!!!)
  3. 友情提示:如果创建容器成功,启动不了,可以先把配置文件挂载的去掉。先启动一个nginx,然后把配置文件copy出来一份
  4. copy命令

docker cp 容器id:容器的文件 需要拷贝到的地方

docker cp f47d1a7f4853:/etc/nginx/nginx.conf /etc/docker/nginx/nginx.conf
docker cp f47d1a7f4853:/etc/nginx/conf.d/default.conf /etc/docker/nginx/conf.d/default.conf
  1. 成功截图
    在这里插入图片描述

2:最后总结(想了解更多就多看下)

启动一个名为nginx81(名字自己根据需求起名字,一般见名知意即可) 的容器
docker run --name nginx81 -d -p 80:80 -v /usr/docker/nginx/html:/usr/share/nginx/html nginx
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
默认容器对这个目录有可读写权限,可以通过指定ro,将权限改为只读(readonly)
docker run --name my-nginx -d -p 80:80 -v /usr/docker/nginx/html:/usr/share/nginx/html:ro -d nginx

  1. 列出运行中的容器
# 使用 docker ps 命令即可列出运行中的容器
docker ps


# 使用 docker ps -a 命令即可列出所有(包括已停止的)的容器
docker ps -a
  1. 列出已下载的镜像
# 使用 docker images 命令即可列出已下载的镜像

docker images
  1. 普通的挂载方式
# 普通的挂载方式
docker run --name mynginx2 --mount source=/var/www,target==/usr/share/nginx/html,readonly \
--mount source=/var/nginx/conf,target=/etc/nginx/conf,readonly -p 80:80 -d nginx

3:参考文章

参考文章地址

  1. https://my.oschina.net/u/3375733/blog/1591091
  2. https://blog.csdn.net/wangfei0904306/article/details/77623400?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param
  3. https://note.youdao.com/ynoteshare1/index.html?id=0d12819002db9df5127aa43b209f6f06&type=note
  • 10
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Docker安装Nginx配置SSL证书的步骤如下: 1. 准备SSL证书:根据不同的云服务商,证书文件的后缀可能有所不同。腾讯云的证书文件后缀为.crt和.key,阿里云的证书文件后缀为.pem和.key。确保证书文件正确并可用。 2. 下载最新的Nginx镜像:使用以下命令下载最新的Nginx镜像: ``` docker pull nginx ``` 3. 创建目录:创建几个目录,用于挂载Nginx容器内的配置文件和日志文件。使用以下命令创建目录: ``` mkdir -p /usr/local/nginx/{conf,html,logs,ssl} ``` 4. 启动一个Nginx临时容器:使用以下命令启动一个Nginx临时容器,并将配置文件复制到主机上: ``` docker run --name nginx-temp -d nginx docker cp nginx-temp:/etc/nginx/nginx.conf /usr/local/nginx/conf/nginx.conf docker rm -f nginx-temp ``` 5. 上传SSL证书:将SSL证书上传到服务器的指定目录,例如将证书文件复制到/usr/local/nginx/ssl目录下。 6. 修改nginx.conf配置文件:编辑/usr/local/nginx/conf/nginx.conf文件配置SSL证书的路径。确保路径是在容器内的地址,而不是主机的地址。 7. 正式启动Nginx:使用以下命令启动Nginx容器,并将挂载目录和端口映射配置好: ``` docker run --name nginx -p 80:80 -p 443:443 \ -v /usr/local/nginx/html:/usr/share/nginx/html \ -v /usr/local/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \ -v /usr/local/nginx/conf.d:/etc/nginx/conf.d \ -v /usr/local/nginx/logs:/var/log/nginx \ -v /usr/local/nginx/ssl:/etc/nginx/ssl \ --privileged=true -d --restart=always nginx ``` 8. 检查Nginx是否成功启动:使用以下命令检查Nginx容器是否成功启动: ``` docker ps ``` 以上是使用Docker安装Nginx配置SSL证书的步骤。请根据实际情况进行操作。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* *3* [docker安装nginx配置ssl证书](https://blog.csdn.net/LuoHuaX/article/details/127320361)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值