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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章