Docker_安装Nginx并运行

首先从远程拉取Nginx

[root@localhost dockerNginx]# docker pull nginx:1.17.6
1.17.6: Pulling from library/nginx
000eee12ec04: Already exists 
eb22865337de: Pull complete 
bee5d581ef8b: Pull complete 
Digest: sha256:50cf965a6e08ec5784009d0fccb380fc479826b6e0e65684d9879170a9df8566
Status: Downloaded newer image for nginx:1.17.6
docker.io/library/nginx:1.17.6

先简单的运行Nginx查看配置文件的位置

[root@localhost dockerNginx]# docker run -d nginx:1.17.6
486d59da1b63e812f656f9c7cb6be3348aa9080d59f3ade90d2604749dcb1ee3
[root@localhost dockerNginx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
486d59da1b63        nginx:1.17.6        "nginx -g 'daemon of…"   4 seconds ago       Up 3 seconds        80/tcp              fervent_perlman
[root@localhost dockerNginx]# docker exec -it 486d59da1b63 /bin/bash
root@486d59da1b63:/# cd /etc/nginx/
root@486d59da1b63:/etc/nginx# ls -l
total 36
drwxr-xr-x. 2 root root   26 Nov 23 01:12 conf.d
-rw-r--r--. 1 root root 1007 Nov 19 12:50 fastcgi_params
-rw-r--r--. 1 root root 2837 Nov 19 12:50 koi-utf
-rw-r--r--. 1 root root 2223 Nov 19 12:50 koi-win
-rw-r--r--. 1 root root 5231 Nov 19 12:50 mime.types
lrwxrwxrwx. 1 root root   22 Nov 19 12:50 modules -> /usr/lib/nginx/modules
-rw-r--r--. 1 root root  643 Nov 19 12:50 nginx.conf
-rw-r--r--. 1 root root  636 Nov 19 12:50 scgi_params
-rw-r--r--. 1 root root  664 Nov 19 12:50 uwsgi_params
-rw-r--r--. 1 root root 3610 Nov 19 12:50 win-utf

可以看到,nginx的配置文件在/etc/nginx/目录下,现在将这个文件夹拷贝到本机上

[root@localhost dockerNginx]# docker cp 486d59da1b63:/etc/nginx/ /dockerNginx/
[root@localhost dockerNginx]# ll nginx/
总用量 36
drwxr-xr-x. 2 root root   26 11月 23 09:12 conf.d
-rw-r--r--. 1 root root 1007 11月 19 20:50 fastcgi_params
-rw-r--r--. 1 root root 2837 11月 19 20:50 koi-utf
-rw-r--r--. 1 root root 2223 11月 19 20:50 koi-win
-rw-r--r--. 1 root root 5231 11月 19 20:50 mime.types
lrwxrwxrwx. 1 root root   22 11月 19 20:50 modules -> /usr/lib/nginx/modules
-rw-r--r--. 1 root root  643 11月 19 20:50 nginx.conf
-rw-r--r--. 1 root root  636 11月 19 20:50 scgi_params
-rw-r--r--. 1 root root  664 11月 19 20:50 uwsgi_params
-rw-r--r--. 1 root root 3610 11月 19 20:50 win-utf

做完这步后,可以将这个容器停止并删除了

[root@localhost nginx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
486d59da1b63        nginx:1.17.6        "nginx -g 'daemon of…"   8 minutes ago       Up 8 minutes        80/tcp              fervent_perlman
[root@localhost nginx]# docker rm -f 486d59da1b63
486d59da1b63

下面这个是正式起的容器

命令:docker run -p 80:80 --name nginx -v /dockerNginx/nginx:/etc/nginx/ -v /dockerNginx/doc:/doc -d nginx:1.17.6

命令解释

docker run 
-p 80:80                            #设置端口
--name nginx                        #设置名称
-v /dockerNginx/nginx:/etc/nginx/   #添加容器卷,将刚才我们复制过来的文件夹,绑定在新的nginx的配置目录,这样就可以在本机修改配置文件
-v /dockerNginx/doc:/doc            #容器卷,等下测试nginx访问静态资源
-d nginx:1.17.6                     #后台运行nginx
[root@localhost nginx]# docker run -p 80:80 --name nginx -v /dockerNginx/nginx:/etc/nginx/ -v /dockerNginx/doc:/doc -d nginx:1.17.6
61af4184c02fbe24cd5aa261997ce1a0260407311f42439a6fcfc23fa6c34955
[root@localhost nginx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
61af4184c02f        nginx:1.17.6        "nginx -g 'daemon of…"   7 seconds ago       Up 6 seconds        0.0.0.0:80->80/tcp   nginx

image

修改配置文件访问/doc文件夹下的静态资源

nginx配置文件

server {
    listen       80;
    server_name  192.168.253.132;
    location /doc {
        root        /;
        autoindex   on; # 表示文件夹里面的文件用列表列出来
    }
}

在doc文件夹中创建文件

[root@localhost doc]# touch hello.txt #可以省略此步
[root@localhost doc]# vi hello.txt 
[root@localhost doc]# cat hello.txt 
hello nginx

重启Nginx,访问静态文件

image

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值