Doocker ubuntu 16.04 學習總結(二)-使用docker搭建web服務器

在容器中部署靜態網站

設置容器的端口映射

我們還沒有 ubuntu 的鏡像

查找 ubuntu 的鏡像

$ docker search ubuntu
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ubuntu                                                 Ubuntu is a Debian-based Linux operating sys…   9405                [OK]                
dorowu/ubuntu-desktop-lxde-vnc                         Docker image to provide HTML5 VNC interface …   292                                     [OK]
rastasheep/ubuntu-sshd                                 Dockerized SSH service, built on top of offi…   211                                     [OK]
consol/ubuntu-xfce-vnc                                 Ubuntu container with "headless" VNC session…   173                                     [OK]
ubuntu-upstart                                         Upstart is an event-based replacement for th…   97                  [OK]                
ansible/ubuntu14.04-ansible                            Ubuntu 14.04 LTS with ansible                   96                                      [OK]
neurodebian                                            NeuroDebian provides neuroscience research s…   56                  [OK]                
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          50                                      [OK]
ubuntu-debootstrap                                     debootstrap --variant=minbase --components=m…   40                  [OK]                
nuagebec/ubuntu                                        Simple always updated Ubuntu docker images w…   23                                      [OK]
i386/ubuntu                                            Ubuntu is a Debian-based Linux operating sys…   17                                      
1and1internet/ubuntu-16-apache-php-7.0                 ubuntu-16-apache-php-7.0                        13                                      [OK]
ppc64le/ubuntu                                         Ubuntu is a Debian-based Linux operating sys…   12                                      
eclipse/ubuntu_jdk8                                    Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, …   9                                       [OK]
darksheer/ubuntu                                       Base Ubuntu Image -- Updated hourly             5                                       [OK]
codenvy/ubuntu_jdk8                                    Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, …   5                                       [OK]
pivotaldata/ubuntu                                     A quick freshening-up of the base Ubuntu doc…   2                                       
smartentry/ubuntu                                      ubuntu with smartentry                          1                                       [OK]
paasmule/bosh-tools-ubuntu                             Ubuntu based bosh-cli                           1                                       [OK]
1and1internet/ubuntu-16-sshd                           ubuntu-16-sshd                                  1                                       [OK]
1and1internet/ubuntu-16-php-7.1                        ubuntu-16-php-7.1                               1                                       [OK]
ossobv/ubuntu                                          Custom ubuntu image from scratch (based on o…   0                                       
pivotaldata/ubuntu-gpdb-dev                            Ubuntu images for GPDB development              0                                       
1and1internet/ubuntu-16-healthcheck                    ubuntu-16-healthcheck                           0                                       [OK]
1and1internet/ubuntu-16-rspec                          ubuntu-16-rspec                                 0                                       [OK]

安裝 ubuntu 的鏡像

$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
898c46f3b1a1: Pull complete 
63366dfa0a50: Pull complete 
041d4cd74a92: Pull complete 
6e1bee0f8701: Pull complete 
Digest: sha256:017eef0b616011647b269b5c65826e2e2ebddbe5d1f8c1e56b3599fb14fabec8
Status: Downloaded newer image for ubuntu:latest

創建一個 container

$ docker run -p 80 --name ubuntu_web -i -t ubuntu /bin/bash
root@8c9885237635:/# 

端口映射使用docker run -P -p;
-P 將容器暴露的所有端口進行映射
-p 只暴露指定的端口

Nginx 部署流程

1. 創建80 端口的交互式容器
2. 安裝 Nginx
3. 安裝文本編輯器vim
4. 創建靜態頁面
5. 修改 Nginx配置文件
6. 運行Nginx
7. 驗證網站訪問

  1. 創建80 端口的交互式容器
$ docker run -p 80 --name ubuntu_web -i -t ubuntu /bin/bash
root@8c9885237635:/# 

  1. 安裝 Nginx
    先更新源,在安裝 nginx
root@8c9885237635:/#  apt-get update
root@8c9885237635:/# apt-get install -y nginx
  1. 安裝文本編輯器vim
# apt-get install -y vim

  1. 創建靜態頁面
root@8c9885237635:/# cd /var/www/html/                        
root@8c9885237635:/var/www/html# vim index.html
  1. 修改 Nginx配置文件
    修改 root 爲 /var/www/html
root@8c9885237635:/var/www/html# vi /etc/nginx/sites-enabled/default 
  1. 運行Nginx
root@8c9885237635:~# nginx
root@8c9885237635:~#  { CTRL P, CTRL Q} 退出後臺運行
  1. 驗證網站訪問
    觀察映射已經成功, 而且docker 在後臺運行
 docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                   NAMES
8c9885237635        ubuntu              "/bin/bash"         2 hours ago         Up 2 hours          0.0.0.0:32768->80/tcp   ubuntu_web

curl http://127.0.0.1:32768 // 訪問這個端口,成功
// 利用容器的地址來查看
$ docker inspect ubuntu_web
"IPAddress": "172.17.0.2",
$ curl http://172.17.0.2  // 返回內容一樣

關閉後 重新啓動 nginx 服務

~$ docker stop ubuntu_web
~$ docker start ubuntu_web
~$ docker top ubuntu_web 
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                14777               14746               0                   18:10               pts/0               00:00:00            /bin/bash
~$ docker exec ubuntu_web nginx
~$ docker top ubuntu_web 
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                14777               14746               0                   18:10               pts/0               00:00:00            /bin/bash
root                15017               14777               0                   18:11               ?                   00:00:00            nginx: master process nginx
www-data            15018               15017               0                   18:11               ?                   00:00:00            nginx: worker process
www-data            15019               15017               0                   18:11               ?                   00:00:00            nginx: worker process
www-data            15020               15017               0                   18:11               ?                   00:00:00            nginx: worker process
www-data            15021               15017               0                   18:11               ?                   00:00:00            nginx: worker process
// nginx 服務已經啓動
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章