linux在docker安装nginx,linux上docker部署nginx

拉取nginx官方镜像

docker pull nginx

运行nginx镜像并进入容器

docker run -ti nginx /bin/bash

/bin/bash的只能用一下很基本的unix命令,我们ls一下发现nginx容器有以下目录

4a37348aa164

image.png

其中nginx的配置目录在/etc/nginx

这样我们就运行了一个基本的nginx容器了,但是怎么样才能外部能访问到呢,我们可以映射容器的端口到外部主机端口。

docker run -p 8888:80 -d nginx

4a37348aa164

image.png

这样我们就将外部的8888端口指向nginx容器的80端口了,OK,打开浏览器测试一下

4a37348aa164

image.png

可是我们每次要修改nginx的配置都要进入到容器内部去修改是不是太麻烦了,ok,目录也来映射一下。

先停止刚刚启动的容器

docker stop 268527ecab7c

在主机上创建目录/disk2/docker/nginx,在该目录下创建conf,logs,www三个目录,分别是干什么的也不用我多说了吧。

然后执行

docker run -p 8888:80 -v /disk2/docker/nginx/conf:/etc/nginx/conf.d -v /disk2/docker/nginx/www:/usr/share/nginx/html -v /disk2/docker/nginx/logs:/var/log/nginx -d nginx

在/disk2/docker/nginx/conf目录下新增文件default.conf,写入

server {

listen 80;

server_name localhost;

#charset koi8-r;

access_log /var/log/nginx/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;

#}

}

然后在/disk2/docker/nginx/www目录下新增文件index.html,写入

Welcome to nginx in docker!

docker!!!!

查看结果

4a37348aa164

image.png

4a37348aa164

image.png

这样就能将容器内的目录映射到外部的目录去了,修改配置或查看日志便不用进入容器中去修改了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值