部署Docker

1、簡介python

Docker 是一個開源的應用容器引擎,讓開發者能夠打包他們的應用以及依賴包到一個可移植的容器中,而後發佈到任何流行的 Linux 機器上,也能夠實現虛擬化。容器是徹底使用沙箱機制,相互之間不會有任何接口(相似 iPhone 的 app)。幾乎沒有性能開銷,能夠很容易地在機器和數據中心中運行。mysql


Docker的理念:一個容器只運行一個服務linux

Docker官網口號包含了Build,Shipand Run Any App,Anywhere,即任何應用,均可以構建、發佈、運行於任何環境,將環境的影響因素降至最低,統一地掌控整個應用的生命週期。sql


Docker的官方文檔:http://docs.docker.com/docker


2、安裝json

一、安裝epel源
註釋:默認CentOS6.x提供的yum源裏沒有docker的安裝包,在這裏咱們藉助EPEL源。
# rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# sed -i 's@^#@@' /etc/yum.repos.d/epel.repo
# sed -i 's@mirrorlist@#mirrorlist@' /etc/yum.repos.d/epel.repo

二、安裝docker
# yum -y remove docker
# yum install docker-io 

三、啓動docker守護進程
# service docker start
# chkconfig docker on

四、檢查docker是否已經正確安裝並運行
# docker info

五、查看docker的版本
# docker -v


3、命令參數ubuntu

一、docker命令幫助參數bash

[root@localhost ~]# docker
Usage: docker [OPTIONS] COMMAND [arg...]

Commands:
    attach    Attach to a running container
    build     Build an p_w_picpath from a Dockerfile
    commit    Create a new p_w_picpath from a container's changes
    cp        Copy files/folders from a container's filesystem to the host path
    create    Create a new container
    diff      Inspect changes on a container's filesystem
    events    Get real time events from the server
    exec      Run a command in a running container
    export    Stream the contents of a container as a tar archive
    history   Show the history of an p_w_picpath
    p_w_picpaths    List p_w_picpaths
    import    Create a new filesystem p_w_picpath from the contents of a tarball
    info      Display system-wide information
    inspect   Return low-level information on a container
    kill      Kill a running container
    load      Load an p_w_picpath from a tar archive
    login     Register or log in to a Docker registry server
    logout    Log out from a Docker registry server
    logs      Fetch the logs of a container
    port      Lookup the public-facing port that is NAT-ed to PRIVATE_PORT
    pause     Pause all processes within a container
    ps        List containers
    pull      Pull an p_w_picpath or a repository from a Docker registry server
    push      Push an p_w_picpath or a repository to a Docker registry server
    restart   Restart a running container
    rm        Remove one or more containers
    rmi       Remove one or more p_w_picpaths
    run       Run a command in a new container
    save      Save an p_w_picpath to a tar archive
    search    Search for an p_w_picpath on the Docker Hub
    start     Start a stopped container
    stop      Stop a running container
    tag       Tag an p_w_picpath into a repository
    top       Lookup the running processes of a container
    unpause   Unpause a paused container
    version   Show the Docker version information
    wait      Block until a container stops, then print its exit code

二、比較經常使用命令參數服務器

查看Docker的版本信息
# docker version

在Docker Hub上搜索一個指定鏡像
# docker search

在Docker Hub上搜索一個指定鏡像並至少有10顆星
# docker search -s 10 ubuntu

從一個Docker的註冊服務器上拉取一個鏡像或一個私有倉庫
# docker pull ubuntu

查看鏡像列表
# docker p_w_picpaths

在一個新的容器中運行一個命令
# docker run

移除一個或多個鏡像
# docker rmi

移除一個或多個容器
# docker rm

附着一個運行的容器
# docker attach

運行一個命令在一個運行的容器中
# docker exec

從一個Dockerfile文件中構建一個鏡像
# docker build

查看鏡像構建歷史
# docker history

查看容器更爲詳細的配置信息
# docker inspect

保存一個鏡像對歸檔tar中
# docker save

從一個歸檔tar中加載一個鏡像
# docker load

啓動、中止、重啓一個運行的容器
# docker start| stop| restart

殺掉一個正在運行的容器
# docker kill

額外補充
進入容器命令:
docker attach:登錄到運行的容器中
docker exec:在宿主機上運行命令到容器內部,相似在打開一個容器的終端
docker nsenter:鏈接到容器,須要容器PID


4、建立ssh鏡像和鏡像打包網絡

一、從Docker Hub上下載ubuntu鏡像

[root@localhost ~]# docker pull ubuntu:14.04
[root@localhost ~]# docker p_w_picpaths
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              14.04               b7cf8f0d9e82        3 days ago          188.3 MB

二、基於鏡像建立一個容器

[root@localhost ~]# docker run -it ubuntu:14.04 /bin/bash
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
88b6a8dfae4e        ubuntu:14.04        "/bin/bash"         3 minutes ago       Up 3 minutes                            modest_yalow

三、進入容器安裝ssh服務

root@88b6a8dfae4e:/# apt-get update && apt-get install -y openssh-server
root@0af7ccfd906e:/# echo 'root:redhat' | chpasswd
root@10dbbd22172d:/# mkdir /var/run/sshd
root@10dbbd22172d:/# sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
root@10dbbd22172d:/# sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
root@10dbbd22172d:/# exit
[root@localhost ~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
88b6a8dfae4e        ubuntu:14.04        "/bin/bash"         10 minutes ago      Exited (130) 18 seconds ago                       modest_yalow

四、構建一個ssh的鏡像

[root@localhost ~]# docker commit 88b6a8dfae4e zhengyas/ubuntu:sshd
3f2225df36ff67cbda098318e83128f3965758eba3e4609a094c172b0c3b03c4
[root@localhost ~]# docker p_w_picpaths
REPOSITORY           TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
zhengyas/ubuntu      sshd                3f2225df36ff        21 seconds ago      251.1 MB
ubuntu               14.04               b7cf8f0d9e82        3 days ago          188.3 MB

五、基於新鏡像運行一個ssh容器

[root@localhost ~]# docker run -d -p 2222:22 zhengyas/ubuntu:sshd /usr/sbin/sshd -D
7ef47903cdb77ad9d98fd0dd3b102473d10ad3abea5311c030177db9ea9984c1
[root@localhost ~]# docker ps -l
CONTAINER ID        IMAGE                  COMMAND               CREATED             STATUS              PORTS                  NAMES
7ef47903cdb7        zhengyas/ubuntu:sshd   "/usr/sbin/sshd -D"   4 seconds ago       Up 4 seconds        0.0.0.0:2222->22/tcp   hungry_ritchie

六、測試ssh容器是否可以正常鏈接

[root@localhost ~]# ssh root192.168.0.104 -p 2222
ssh: Could not resolve hostname root192.168.0.104: Name or service not known
[root@localhost ~]# ssh root@192.168.0.104 -p 2222
The authenticity of host '[192.168.0.104]:2222 ([192.168.0.104]:2222)' can't be established.
RSA key fingerprint is 0e:1e:4e:67:f3:4b:5a:c4:c2:f5:7b:e7:f0:2e:14:72.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.0.104]:2222' (RSA) to the list of known hosts.
root@192.168.0.104's password: 
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.2.0-61-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

root@7ef47903cdb7:~# ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:ac:11:00:03  
          inet addr:172.17.0.3  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe11:3/64 Scope:Link
          UP BROADCAST RUNNING  MTU:1500  Metric:1
          RX packets:47 errors:0 dropped:0 overruns:0 frame:0
          TX packets:40 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:5638 (5.6 KB)  TX bytes:6521 (6.5 KB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

七、鏡像持久化,俗稱鏡像打包

鏡像打包(Save)

[root@localhost ~]# docker save zhengyas/ubuntu > /root/sshd.tar

鏡像導入(Load)

實驗模擬
一、刪除此sshd容器
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND               CREATED             STATUS              PORTS                  NAMES
7ef47903cdb7        zhengyas/ubuntu:sshd   "/usr/sbin/sshd -D"   14 minutes ago      Up 14 minutes       0.0.0.0:2222->22/tcp   hungry_ritchie      
[root@localhost ~]# docker stop 7ef47903cdb7
7ef47903cdb7
[root@localhost ~]# docker rm 7ef47903cdb7
7ef47903cdb7

二、刪除sshd鏡像
[root@localhost ~]# docker rmi zhengyas/ubuntu:sshd
Untagged: zhengyas/ubuntu:sshd
Deleted: 3f2225df36ff67cbda098318e83128f3965758eba3e4609a094c172b0c3b03c4
[root@localhost ~]# docker p_w_picpaths
REPOSITORY           TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu               14.04               b7cf8f0d9e82        3 days ago          188.3 MB

三、導入打包的鏡像
[root@localhost ~]# docker load < /root/sshd.tar 
[root@localhost ~]# docker p_w_picpaths
REPOSITORY           TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
zhengyas/ubuntu      sshd                3f2225df36ff        18 minutes ago      251.1 MB
ubuntu               14.04               b7cf8f0d9e82        3 days ago          188.3 MB


5、基於Dockerfile來建立mysql鏡像

一、建立Dockerfile文件

[root@localhost ~]# mkdir mysql_ubuntu
[root@localhost ~]# cd mysql_ubuntu/
[root@localhost mysql_ubuntu]# cat Dockerfile 
FROM ubuntu:14.04

RUN apt-get update

RUN apt-get -y install mysql-client mysql-server
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf

ADD ./startup.sh /opt/startup.sh

EXPOSE 3306

CMD ["/bin/bash", "/opt/startup.sh"]

二、建立mysql服務啓動腳本文件

[root@localhost mysql_ubuntu]# cat startup.sh 
#!/bin/bash

if [ ! -f /var/lib/mysql/ibdata1 ]; then

    mysql_install_db

    /usr/bin/mysqld_safe &
    sleep 10s

    echo "GRANT ALL ON *.* TO admin@'%' IDENTIFIED BY 'changeme' WITH GRANT OPTION; FLUSH PRIVILEGES" | mysql

    killall mysqld
    sleep 10s
fi

/usr/bin/mysqld_safe

三、構建mysql鏡像

# docker build -t zhengys/mysql .

四、查看鏡像

[root@localhost ~]# docker p_w_picpaths
REPOSITORY           TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
zhengys/mysql        latest              f58add96ecb7        About a minute ago   338.9 MB

六、基於新鏡像建立mysql容器

[root@localhost ~]# mkdir /data/mysql -p
[root@localhost ~]# docker run -d -p 3306:3306 -v /data/mysql:/var/lib/mysql zhengys/mysql
0112ba90e4a30a13e4f3af26f4a5bcd73e91ae3afa881a36fadd34cd953d0ada
[root@localhost ~]# docker ps -l
CONTAINER ID        IMAGE                  COMMAND                CREATED             STATUS              PORTS                    NAMES
0112ba90e4a3        zhengys/mysql:latest   "/bin/bash /opt/star   4 seconds ago       Up 3 seconds        0.0.0.0:3306->3306/tcp   reverent_hawking    
[root@localhost ~]# ll /data/mysql/
total 28680
-rw-rw----. 1 103  106 18874368 Apr 25 17:46 ibdata1
-rw-rw----. 1 103  106  5242880 Apr 25 19:09 ib_logfile0
-rw-rw----. 1 103  106  5242880 Apr 25 17:45 ib_logfile1
drwx------. 2 103 root     4096 Apr 25 17:45 mysql
drwx------. 2 103  106     4096 Apr 25 17:45 performance_schema

七、測試mysql容器

[root@localhost ~]# mysql -uadmin -p123456 -h192.168.0.104 -P 3306 -e 'show databases'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+

或者提供一個登錄mysql客戶端腳本

run-client.sh

#!/bin/sh

TAG="mysql"

CONTAINER_ID=$(docker ps | grep $TAG | awk '{print $1}')

IP=$(docker inspect $CONTAINER_ID | python -c 'import json,sys;obj=json.load(sys.stdin);print obj[0]["NetworkSettings"]["IPAddress"]')

mysql -u admin -p -h $IP

6、簡化Docker和lxc

一、Lxc和Docker結構圖


wKioL1U9BUajTl37AAE26DMffs4988.jpg

wKioL1U9BpSBgkkKAAGUiL-TQ78751.jpg


Linux = linux內核 + 用戶空間(Lxc)

Lxc(Linux Container):linux容器 = Cgroup + Namespaces

Docker集裝箱 = Lxc + p_w_picpaths


lxc功能包括資源管理和隔離機制。

資源管理:經過cgroup限制cpu和內存的使用

隔離機制:用戶空間namespace都是獨立的


LXC包集成了這些linux內核機制提供了一個用戶空間容器對象,便是針對某一應用提供資源隔離和控制輕量級虛擬系統。


Docker對container的使用基本是創建在lxc基礎之上的,然而lxc存在的問題是難以移動-難以經過標準化模板製做、重建、複製和移動container。


LXC依賴namespace來實現隔離性的。

讓每一個容器都有自已的命名空間,確保不一樣容器之間不會相互影響,讓每一個容器成爲擁有自已進程和網絡空間的虛擬環境,都成爲一個獨立運行的單位。

此外,lxc由內核cgroup來對各個容器(進程)使用的系統資源作嚴格的限制。


  • 算算時間,學習Docker也有半個月時間了,到如今爲止給個人第一感受仍然是不習慣,或許是用傳統虛擬化用習慣了,或許是自已對Docker研究過於膚淺,或許自已根本沒有入門等等一些緣由,在沒有接觸到Docker以前自已玩過lxc,使用起來特別順手,網上都說Docker自動化了lxc的管理過程,可以自動在線下載相應的發行版本rootfs

  • Docker的火熱程度,使咱們作IT的不得不去深刻研究、學習


好吧!今天就先到這裏,後續會繼續更你們聊聊Docker技術.

相關文章
相關標籤/搜索