教你如何在雲伺服器上體驗Docker服務!

1. 添加Docker repository key

sh -c "wget -qO- get.docker.io/gpg | apt-key add -"

2. 添加Docker repository,並安裝Docker

sh -c "echo deb http://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"apt-get updateapt-get install lxc-docker

3. 檢查Docker是否已安裝成功

docker version

基本使用

1.安裝 ubuntu 鏡像

docker pull ubuntu

開始下載 ubuntu 鏡像, 大概需要10分鐘左右

Pulling repository ubuntu9cbaf023786c: Pulling dependent layersa9561eb1b190: Pulling dependent layers195eb90b5349: Pulling dependent layers2185fd50e2ca: Pulling dependent layersc5881f11ded9: Pulling dependent layers463ff6be4238: Pulling dependent layers3db9c44f4520: Pulling dependent layers511136ea3c5a: Download complete500cae81e00f: Downloading [=========================================> ] 58.63 MB/70.36 MB 1m6s

查看一下當前的鏡像列表

docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEubuntu 14.10 2185fd50e2ca 2 days ago 237.2 MBubuntu utopic 2185fd50e2ca 2 days ago 237.2 MBubuntu 14.04 9cbaf023786c 2 days ago 192.8 MBubuntu 14.04.1 9cbaf023786c 2 days ago 192.8 MBubuntu trusty 9cbaf023786c 2 days ago 192.8 MBubuntu latest 9cbaf023786c 2 days ago 192.8 MBubuntu 12.04.5 a9561eb1b190 2 days ago 120.2 MBubuntu precise a9561eb1b190 2 days ago 120.2 MBubuntu 12.04 a9561eb1b190 2 days ago 120.2 MBubuntu 12.10 c5881f11ded9 3 months ago 172.2 MBubuntu quantal c5881f11ded9 3 months ago 172.2 MBubuntu 13.04 463ff6be4238 3 months ago 169.4 MBubuntu raring 463ff6be4238 3 months ago 169.4 MBubuntu saucy 195eb90b5349 3 months ago 184.7 MBubuntu 13.10 195eb90b5349 3 months ago 184.7 MBubuntu 10.04 3db9c44f4520 5 months ago 183 MBubuntu lucid 3db9c44f4520 5 months ago 183 MB

執行一個命令

docker run ubuntu:12.10 ls -lh /total 68Kdrwxr-xr-x 2 root root 4.0K Oct 16 2012 bindrwxr-xr-x 2 root root 4.0K Oct 9 2012 bootdrwxr-xr-x 4 root root 340 Oct 16 15:51 devdrwxr-xr-x 44 root root 4.0K Oct 16 15:51 etcdrwxr-xr-x 2 root root 4.0K Oct 9 2012 homedrwxr-xr-x 11 root root 4.0K Oct 16 2012 libdrwxr-xr-x 2 root root 4.0K Oct 16 2012 lib64drwxr-xr-x 2 root root 4.0K Oct 16 2012 mediadrwxr-xr-x 2 root root 4.0K Oct 9 2012 mntdrwxr-xr-x 2 root root 4.0K Oct 16 2012 optdr-xr-xr-x 128 root root 0 Oct 16 15:51 procdrwx------ 2 root root 4.0K Oct 16 2012 rootdrwxr-xr-x 5 root root 4.0K Oct 16 2012 rundrwxr-xr-x 2 root root 4.0K Jun 18 21:54 sbindrwxr-xr-x 2 root root 4.0K Jun 11 2012 selinuxdrwxr-xr-x 2 root root 4.0K Oct 16 2012 srvdr-xr-xr-x 13 root root 0 Oct 16 15:51 sysdrwxrwxrwt 2 root root 4.0K Oct 16 2012 tmpdrwxr-xr-x 11 root root 4.0K Oct 16 2012 usrdrwxr-xr-x 14 root root 4.0K Oct 16 2012 var

2.登錄到 docker 裡面去

docker run -i -t ubuntu:12.10 /bin/bashroot@bb55c85e1b34:/# uname -aLinux bb55c85e1b34 3.5.0-52-generic #78~precise1-Ubuntu SMP Wed Jun 11 17:14:53 UTC 2014 x86_64 x86_64 x86_64 GNU/Linuxroot@bb55c85e1b34:/# ip addr25: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever26: eth0: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 12:6d:9c:14:76:82 brd ff:ff:ff:ff:ff:ff inet 172.17.0.9/16 scope global eth0 inet6 fe80::106d:9cff:fe14:7682/64 scope link valid_lft forever preferred_lft forever

3.安裝 openssh-server

ubuntu 默認的源在騰訊雲主機上無法訪問, 所以要把源改成騰訊雲的源, 登錄到 docker 裡面去發現裡面連 vi 編輯器都沒有, 沒辦法, 只能把主機的文件映射到 docker 裡面去, 然後 cp

下面的命令把主機的 /etc 映射到 docker 的/opt/etc 目錄, 並且運行 bash

docker run -i -t -v /etc:/opt/etc ubuntu:12.10 /bin/bash

進入 docker 後把主機的 sources.list 文件 copy 過去, 在 docker 裡面執行下面的命令

cp /etc/apt/sources.list /etc/apt/sources.list.bakcp /opt/etc/apt/sources.list /etc/apt/sources.list

然後再執行 apt 命令安裝就沒有問題了

apt-get install -y vim openssh-server

設置個 ROOT 密碼

root@fc9fe30c21bc:/# passwdEnter new UNIX password:Retype new UNIX password:passwd: password updated successfully

查看下 docker 的ip

root@fc9fe30c21bc:/# ip addr43: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever44: eth0: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether e2:b9:5a:f8:d5:a1 brd ff:ff:ff:ff:ff:ff inet 172.17.0.15/16 scope global eth0 inet6 fe80::e0b9:5aff:fef8:d5a1/64 scope link valid_lft forever preferred_lft forever

啟動 ssh

root@7e3adf35663f:/# /etc/init.d/ssh restart

從主機上登錄

ssh root@172.17.0.15The authenticity of host 172.17.0.15 (172.17.0.15) cant be established.ECDSA key fingerprint is cf:19:d7:73:02:7e:58:90:08:79:8c:f0:85:2c:a2:9e.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added 172.17.0.15 (ECDSA) to the list of known hosts.root@172.17.0.15s password:Welcome to Ubuntu 12.10 (GNU/Linux 3.5.0-52-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 theindividual files in /usr/share/doc/*/copyright.Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted byapplicable law.root@fc9fe30c21bc:~#

4.保存對docker的修改

  • 查看下 CONTAINER ID

    docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESfc9fe30c21bc ubuntu:12.10 "/bin/bash" 11 minutes ago Up 11 minutes evil_turing

  • 保存 上一步的 CONTAINER ID 是 fc9fe30c21bc 但是不用全部輸入只要保證不重複就可以了

    docker commit fc9f y109/ubuntue099241dfc1fb31d950b0be4de2fa7a78510022737fe8cc81a913bdd4a244a95

  • 確認 鏡像列表裡面有剛剛保存的鏡像

    docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEy109/ubuntu latest e099241dfc1f 14 seconds ago 316.2 MBubuntu 14.10 2185fd50e2ca 2 days ago 237.2 MBubuntu utopic 2185fd50e2ca 2 days ago 237.2 MBubuntu trusty 9cbaf023786c 2 days ago 192.8 MBubuntu 14.04.1 9cbaf023786c 2 days ago 192.8 MBubuntu 14.04 9cbaf023786c 2 days ago 192.8 MBubuntu latest 9cbaf023786c 2 days ago 192.8 MBubuntu 12.04 a9561eb1b190 2 days ago 120.2 MBubuntu 12.04.5 a9561eb1b190 2 days ago 120.2 MBubuntu precise a9561eb1b190 2 days ago 120.2 MBubuntu quantal c5881f11ded9 3 months ago 172.2 MBubuntu 12.10 c5881f11ded9 3 months ago 172.2 MBubuntu raring 463ff6be4238 3 months ago 169.4 MBubuntu 13.04 463ff6be4238 3 months ago 169.4 MBubuntu 13.10 195eb90b5349 3 months ago 184.7 MBubuntu saucy 195eb90b5349 3 months ago 184.7 MBubuntu 10.04 3db9c44f4520 5 months ago 183 MBubuntu lucid 3db9c44f4520 5 months ago 183 MB

  • 提交

    docker push y109/ubuntuThe push refers to a repository [y109/ubuntu] (len: 1)Sending image list

    首次提交會要求登錄

    Please login prior to push:Username: y109Password:Email: XXXX@qq.comLogin SucceededThe push refers to a repository [y109/ubuntu] (len: 1)Sending image listPushing repository y109/ubuntu (1 tags)511136ea3c5a: Image already pushed, skippingbac448df371d: Image already pushed, skippingdfaad36d8984: Image already pushed, skipping5796a7edb16b: Image already pushed, skippingc5881f11ded9: Image already pushed, skippinge099241dfc1f: Pushing [===========> ] 32.94 MB/147.3 MB 1m10se099241dfc1f: Image successfully pushed

本人是在馬化騰家的雲上做的實驗,系統為ubuntu完美使用。
推薦閱讀:

Say Hello To TensorFlow
安裝並使用docker@Windows——第二章(使用docker)
乾貨 | 解密監控寶Docker監控實現原理
優雅地使用docker
Docker基本操作

TAG:Docker | Linux運維 | 雲服務 |