docker 安装nginx并把主要的目录和配置文件挂载出来(亲测可用)

首先你需要有一个环境配置好的服务器,docker 环境要配置好 这个很好弄网上很多这样的资料。

1 .拉取nginx镜像

docker pull nginx

2.查看下载下来的镜像

docker images

在这里插入图片描述

3.创建挂载目录

mkdir -p /data/nginx/{conf,conf.d,html,logs}

4.创建好目录之后要把两个重要的配置文件复制到刚刚对应的文件夹中

(可以把容器先简单运行起来 使用docker cp 命令 把文件拷贝出来
docker cp 容器id:/etc/nginx/nginx.conf /data/nginx/nginx.conf
docker cp 容器id:/etc/nginx/conf.d/default.conf /data/nginx/conf.d/default.conf )这一步也可以换一种实现
创建文件 然后把下面的文件编辑到创建的文件中

touch /data/nginx/nginx.conf

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;
}


在创建一个文件 把内容编辑到文件中

touch /data/nginx/conf.d/default.conf

default.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 --name nginx83 -d -p 83:80 -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  -v /data/nginx/logs:/var/log/nginx -v /data/nginx/conf.d:/etc/nginx/conf.d -d nginx

查看容器运行状态

docker ps

在这里插入图片描述
说明已经运行成功了
找一个html 页面把页面放到挂载出来的html目录中
index.html

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<h1>hello world</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>


然后重启一下nginx docker 容器

docker restart 容器id

然后在浏览器访问
0.0.0.0:83
你的主机地址:83

在这里插入图片描述
成功了!!!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值