1. 程式人生 > >Docker容器中部署靜態網頁

Docker容器中部署靜態網頁

一般我們在訪問容器時需要通過容器的埠來訪問,那如何設定容器的埠對映呢?我們通過以下命令來設定:

docker run -p ip:hostPort:containerPort [--name] [-i] [-t] 映象名 [COMMAND][ARG...]

ip:表示宿主機ip
hostPort:宿主機埠號
containerPort:容器埠號

設定的方式有以下幾種:

  1. containerPort,指定容器埠號,宿主機埠隨機生成
[root@localhost ~]# docker run -p 80 --name web_test -i -t 80864d42dd23        hub.c.163.com/library/ubuntu /bin/bash
  1. hostPort:containerPort對映主機埠和容器埠
[root@localhost ~]# docker run -p 8080:80 --name web_test -i -t 80864d42dd23        hub.c.163.com/library/ubuntu /bin/bash
  • ip::containerPort設定主機的隨機埠到容器埠
[root@localhost ~]# docker run -p 0.0.0.0::80 --name web_test -i -t 80864d42dd23        hub.c.163.com/library/ubuntu /bin/bash
  • ip:hostPort:containerPort對映指定地址的指定埠到容器的指定埠
[root@localhost ~]# docker run -p 0.0.0.0:8080:80 --name web_test -i -t 80864d42dd23        hub.c.163.com/library/ubuntu /bin/bash

下面通過nginx在容器部署靜態網頁,通過以下步驟
- 建立80對映埠的互動式容器
- 安裝nginx
- 安裝文字編輯器vim
- 建立靜態網頁
- 執行nginx
- 驗證網頁
示例如下(如果安裝完ubuntu後不能安裝nginx進行apt-get update):

[[email protected] ~]# docker run -p 80 --name static_test -i -t hub.c.163.com/library/ubuntu /bin/bash
root@25fcbf6e953d:/# apt-get install -y nginx
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package nginx
root@25fcbf6e953d:/# apt-get update          
Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
**中間日誌省略。。。。。。。。。。**                                                                          
Fetched 24.9 MB in 9s (2717 kB/s)                                                                                                                                   
Reading package lists... Done
root@25fcbf6e953d:/# apt-get install -y nginx
Reading package lists... Done
Building dependency tree       
Reading state information... Done
**安裝日誌省略。。。。。。。。。。**

然後安裝vim:

root@25fcbf6e953d:/# apt-get install -y vim
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
**安裝日誌省略。。。。。。。。。。**

然後建立存放靜態檔案的目錄,並建立一個簡單的html檔案:

root@25fcbf6e953d:/# mkdir -p /var/www/html
root@25fcbf6e953d:/# 
root@25fcbf6e953d:/# cd /var/www/html/
root@25fcbf6e953d:/var/www/html# vim index.html
root@25fcbf6e953d:/var/www/html# ll  
total 8
drwxr-xr-x. 2 root root  53 Mar 13 05:02 ./
drwxr-xr-x. 3 root root  17 Mar 13 04:50 ../
-rw-r--r--. 1 root root  79 Mar 13 05:02 index.html
-rw-r--r--. 1 root root 612 Mar 13 04:51 index.nginx-debian.html
root@25fcbf6e953d:/var/www/html# cat index.html  
<html>
<body>
<h1>
        this is the first docker static file
</h1>
</body>
</html>
root@25fcbf6e953d:/var/www/html# 

找到nginx的安裝目錄,更改default的root值為剛才建立的目錄:

root@25fcbf6e953d:/var/www/html# whereis nginx
nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx
root@25fcbf6e953d:/var/www/html# cd /etc/nginx/sites-enabled/
root@25fcbf6e953d:/etc/nginx/sites-enabled# ll
total 4
drwxr-xr-x. 2 root root   20 Mar 13 04:51 ./
drwxr-xr-x. 6 root root 4096 Mar 13 04:51 ../
lrwxrwxrwx. 1 root root   34 Mar 13 04:51 default -> /etc/nginx/sites-available/default
root@25fcbf6e953d:/etc/nginx/sites-enabled# vim default 

這裡寫圖片描述

然後啟動nginx,檢視容器程序:

root@25fcbf6e953d:/etc/nginx/sites-enabled# nginx
root@25fcbf6e953d:/etc/nginx/sites-enabled# ps -ef 
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 04:48 ?        00:00:00 /bin/bash
root       827     1  0 05:06 ?        00:00:00 nginx: master process nginx
www-data   828   827  0 05:06 ?        00:00:00 nginx: worker process
www-data   829   827  0 05:06 ?        00:00:00 nginx: worker process
www-data   830   827  0 05:06 ?        00:00:00 nginx: worker process
www-data   831   827  0 05:06 ?        00:00:00 nginx: worker process
www-data   832   827  0 05:06 ?        00:00:00 nginx: worker process
www-data   833   827  0 05:06 ?        00:00:00 nginx: worker process
www-data   834   827  0 05:06 ?        00:00:00 nginx: worker process
www-data   835   827  0 05:06 ?        00:00:00 nginx: worker process
root       836     1  0 05:07 ?        00:00:00 ps -ef
root@25fcbf6e953d:/etc/nginx/sites-enabled# 

然後按ctrl+p,ctrl+q退出互動式容器。
使用docker ps檢視容器的埠和執行情況,也可以通過dcoker port 映象名檢視:

root@localhost ~]# docker ps
CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS              PORTS                   NAMES
25fcbf6e953d        hub.c.163.com/library/ubuntu   "/bin/bash"              18 minutes ago      Up 18 minutes       0.0.0.0:32772->80/tcp   static_test
[root@localhost ~]# docker port static_test
80/tcp -> 0.0.0.0:32772
[root@localhost ~]# 

可以看到容器的80埠對映到註解的32772埠。
通過curl訪問建立的index.html檔案:

[root@localhost ~]# curl http://127.0.0.1:32772/index.html
<html>
<body>
<h1>
        this is the first docker static file
</h1>
</body>
</html>
[root@localhost ~]#

也可以通過docker inspect 容器名來檢視容器的詳細資訊和ip
通過容器ip進行訪問:

[root@localhost ~]# docker inspect static_test
其他資訊省略
"IPAddress": "172.17.0.5",
[root@localhost ~]# curl http://172.17.0.5/index.html
<html>
<body>
<h1>
        this is the first docker static file
</h1>
</body>
</html>
[root@localhost ~]# 

當我們使用docker stop 容器名停止這個容器後,再使用docker start 容器名啟動容器後,裡面的nginx並沒有啟動。那麼我們使用docker exec 容器名 nginx來啟動:

[root@localhost ~]# docker stop static_test
static_test
[root@localhost ~]# docker ps 
CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS              PORTS                   NAMES
b4f32bbe4a34        hub.c.163.com/library/ubuntu   "/bin/bash"              41 hours ago        Up 41 hours                                 loving_brattain
d75a2d8c7822        xingguo/df_test1               "nginx -g 'daemon off"   2 days ago          Up 2 days           0.0.0.0:32770->80/tcp   df_nginx_web
959c0fc5d903        xingguo/commit_test1           "nginx -g 'daemon off"   2 days ago          Up 2 days           0.0.0.0:32769->80/tcp   nginx_test
[root@localhost ~]# docker start static_test
static_test
[root@localhost ~]# docker ps 
CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS              PORTS                   NAMES
25fcbf6e953d        hub.c.163.com/library/ubuntu   "/bin/bash"              29 minutes ago      Up 2 seconds        0.0.0.0:32773->80/tcp   static_test
b4f32bbe4a34        hub.c.163.com/library/ubuntu   "/bin/bash"              41 hours ago        Up 41 hours                                 loving_brattain
d75a2d8c7822        xingguo/df_test1               "nginx -g 'daemon off"   2 days ago          Up 2 days           0.0.0.0:32770->80/tcp   df_nginx_web
959c0fc5d903        xingguo/commit_test1           "nginx -g 'daemon off"   2 days ago          Up 2 days           0.0.0.0:32769->80/tcp   nginx_test
[root@localhost ~]# docker top static_test
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                4719                4702                0                   13:17               pts/2               00:00:00            /bin/bash
[root@localhost ~]# docker exec static_test nginx
[root@localhost ~]# docker top static_test
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                4719                4702                0                   13:17               pts/2               00:00:00            /bin/bash
root                4800                1                   0                   13:18               ?                   00:00:00            nginx: master process nginx
amandab+            4801                4800                0                   13:18               ?                   00:00:00            nginx: worker process
amandab+            4802                4800                0                   13:18               ?                   00:00:00            nginx: worker process
amandab+            4803                4800                0                   13:18               ?                   00:00:00            nginx: worker process
amandab+            4804                4800                0                   13:18               ?                   00:00:00            nginx: worker process
amandab+            4805                4800                0                   13:18               ?                   00:00:00            nginx: worker process
amandab+            4806                4800                0                   13:18               ?                   00:00:00            nginx: worker process
amandab+            4807                4800                0                   13:18               ?                   00:00:00            nginx: worker process
amandab+            4808                4800                0                   13:18               ?                   00:00:00            nginx: worker process
[root@localhost ~]#