Docker安裝與測試使用

1,Docker安裝

Docker是啥秘蛇,以及其與虛擬機的對比其做,就不介紹了,網(wǎng)上有很多資源可以學習和了解赁还。
本篇文章重點介紹Docker的安裝和測試使用妖泄。

  • Docker的安裝,分為離線安裝和在線安裝兩種方式艘策,方便不同的應(yīng)用場景使用不同的安裝方式蹈胡。
  • 關(guān)于Docker的測試使用,本文會介紹如何運行Docker容器朋蔫、如何修改Docker容器罚渐,以及如何完成Docker容器到Docker鏡像的轉(zhuǎn)化。

1.1驯妄,docker離線安裝

很簡單荷并,從官網(wǎng)下載最新的docker離線安裝包,并安裝青扔。

# wget https://download.docker.com/linux/ubuntu/dists/bionic/pool/stable/amd64/docker-ce_18.09.8~3-0~ubuntu-bionic_amd64.deb
# wget http://archive.ubuntu.com/ubuntu/pool/main/libt/libtool/libltdl7_2.4.6-0.1_amd64.deb

# sudo dpkg -i libltdl7_2.4.6-0.1_amd64.deb
# sudo dpkg -i docker-ce-cli_18.09.9_3-0_ubuntu-bionic_amd64.deb
# sudo dpkg -i containerd.io_1.2.6-3_amd64.deb
# sudo dpkg -i docker-ce_18.09.8_3-0_ubuntu-bionic_amd64.deb

1.2源织,docker在線安裝

通過apt-get安裝

# cat /etc/issue
Ubuntu 14.04.5 LTS \n \l
# uname -r
4.4.0-81-generic
# sudo apt-get update # 更新系統(tǒng)資源列表
# apt-get -y install docker.io  # 安裝docker
# docker -v # 查看docker版本信息
Docker version 1.6.2, build 7c8fca2

通過daocloud安裝docker

# curl -sSL https://get.daocloud.io/docker | sh
# docker -v
Docker version 17.06.0-ce, build 02c1d87

通過阿里云的鏡像倉庫安裝docker

# curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet | sh -
# docker -v
Docker version 17.05.0-ce, build 89658be

1.3翩伪,執(zhí)行docker,驗證docker是否安裝成功

# docker
Usage:  docker COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -D, --debug              Enable debug mode
      --help               Print usage
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

1.4谈息,用戶權(quán)限修改

安裝成功之后缘屹,需要使用root用戶才能執(zhí)行docker命令。
如果希望用非root用戶執(zhí)行docker命令侠仇,可以執(zhí)行以下語句轻姿。
注意:執(zhí)行完成后,需要退出再登陸才能生效傅瞻。

# sudo usermod -aG docker $your-user

2踢代,術(shù)語介紹

Docker有倉庫Repositories、鏡像Images和容器Container三大核心嗅骄,三者可以互相轉(zhuǎn)化胳挎。

  • Docker,平臺工具溺森、守護進程
  • Image慕爬,鏡像,包括應(yīng)用及系統(tǒng)的只讀數(shù)據(jù)包
  • Container屏积,容器医窿,虛擬機的運行實例
  • Registry,倉庫登記炊林,管理鏡像文件
  • Docker hub姥卢,Docker官方公有倉庫

三大核心的關(guān)系,如下圖所示:

docker三大核心.png

3渣聚,創(chuàng)建鏡像

Docker鏡像的創(chuàng)建方式有兩種独榴,分別是:

  • 基于現(xiàn)有的鏡像修改
  • 直接創(chuàng)建新鏡像

本文介紹基于已有的鏡像修改的方式,一共分為五步奕枝。

3.1棺榔,第一步,下載官網(wǎng)的基礎(chǔ)鏡像

從官網(wǎng)上pull鏡像hello-world隘道,并運行

root@apple:/home/kevin/docker# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b04784fba78d: Pull complete 
Digest: sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

從官網(wǎng)上pull鏡像ubuntu症歇,并以交互方式運行

root@apple:/home/kevin/docker# docker run -it ubuntu bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
e0a742c2abfd: Pull complete 
486cb8339a27: Pull complete 
dc6f0d824617: Pull complete 
4f7a5649a30e: Pull complete 
672363445ad2: Pull complete 
Digest: sha256:84c334414e2bfdcae99509a6add166bbb4fa4041dc3fa6af08046a66fed3005f
Status: Downloaded newer image for ubuntu:latest
root@93a1b9d39683:/#
root@93a1b9d39683:/#
root@93a1b9d39683:/#
root@93a1b9d39683:/#
root@93a1b9d39683:/#

3.2,第二步谭梗,在CONTAINER中執(zhí)行軟件的安裝

安裝Linux基礎(chǔ)軟件

在第一步的基礎(chǔ)上忘晤,在容器里面,用root用戶執(zhí)行Linux基礎(chǔ)軟件的安裝默辨。

root@93a1b9d39683:/home# apt-get update      
Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]                                         
Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Get:3 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
Get:4 http://security.ubuntu.com/ubuntu xenial-security/universe Sources [42.0 kB]
Get:5 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [384 kB]
Get:6 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
Get:7 http://archive.ubuntu.com/ubuntu xenial/universe Sources [9802 kB]
Get:8 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [12.8 kB]                                                                                
Get:9 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [178 kB]                                                                                   
Get:10 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [2931 B]                                                                                
Get:11 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB]                                                                                               
Get:12 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 Packages [14.1 kB]                                                                                         
Get:13 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [9827 kB]                                                                                           
Get:14 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [176 kB]                                                                                          
Get:15 http://archive.ubuntu.com/ubuntu xenial-updates/universe Sources [207 kB]                                                                                           
Get:16 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [742 kB]                                                                                        
Get:17 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [13.3 kB]                                                                                 
Get:18 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [640 kB]                                                                                    
Get:19 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [16.8 kB]                                                                                 
Get:20 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [4930 B]                                                                                      
Get:21 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [6237 B]                                                                                  
Fetched 24.2 MB in 12min 34s (32.1 kB/s)                                                                                                                                   
Reading package lists... Done

root@93a1b9d39683:/home# apt-get install vim 
root@93a1b9d39683:/home# apt-get install curl
root@93a1b9d39683:/home# apt-get install openssh-server 
root@93a1b9d39683:/home# apt-get install python 

# 安裝ifconfig德频、netstat
root@93a1b9d39683:/home# apt-get install net-tools

# 安裝ping
root@93a1b9d39683:/home# apt-get install iputils-ping

退出CONTAINER

  • ctrl+d,退出容器且關(guān)閉缩幸,docker ps查看容器已退出運行(Exited)
  • ctrl+p壹置,ctrl+q竞思,退出容器但不關(guān)閉,docker ps查看容器依然處于Up狀態(tài)

3.3钞护,第三步盖喷,將CONTAINER保存為IMAGE

查看容器列表

root@apple:/home/kevin/docker# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
966bd52b72da        ubuntu              "/bin/sh -c 'while..."   3 hours ago         Exited (137) 2 hours ago                       stupefied_knuth
568e5204fff3        ubuntu              "/bin/sh -c 'while..."   4 hours ago         Exited (137) 2 hours ago                       kind_khorana
00f561d97811        ubuntu              "/bin/echo hello w..."   4 hours ago         Exited (0) 4 hours ago                         nifty_mcnulty
93a1b9d39683        ubuntu              "bash"                   4 hours ago         Up 3 hours                                     zealous_noether
abdc084f9821        hello-world         "/hello"                 4 hours ago         Exited (0) 3 hours ago                         sleepy_clarke
51097978fdc6        hello-world         "/hello"                 4 hours ago         Exited (0) 3 hours ago                         vibrant_panini

查看鏡像列表

root@apple:/home/kevin/docker# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              14f60031763d        3 days ago          120MB
hello-world         latest              1815c82652c0        5 weeks ago         1.84kB

保存容器到鏡像

root@apple:/home/kevin/docker# docker commit 93a1b9d39683 learn/visual_init:v1
sha256:56a4eab7dc5b9c1b0dca010e36f4e792963a213ad4b362ab4662602eb66f0216

==============
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]Create a new image from a container's changes

  -a, --author=""     Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  -m, --message=""    Commit message
  -p, --pause=true    Pause container during commit
==============

查看鏡像列表

root@apple:/home/kevin/docker# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
learn/visual_init   v1                  56a4eab7dc5b        39 seconds ago      321MB
ubuntu              latest              14f60031763d        3 days ago          120MB
hello-world         latest              1815c82652c0        5 weeks ago         1.84kB

附,容器的常用操作

root@apple:/home/kevin/docker# docker attach $CONTAINER_ID #連接一個已存在的docker容器
root@apple:/home/kevin/docker# docker stop $CONTAINER_ID #停止docker容器
root@apple:/home/kevin/docker# docker start $CONTAINER_ID #啟動docker容器
root@apple:/home/kevin/docker# docker restart $CONTAINER_ID #重啟docker容器
root@apple:/home/kevin/docker# docker kill $CONTAINER_ID #強制關(guān)閉docker容器
root@apple:/home/kevin/docker# docker logs $CONTAINER_ID #查看docker容器運行日志难咕,確保正常運行
root@apple:/home/kevin/docker# docker inspect $CONTAINER_ID #查看container的屬性课梳,比如ip等等
root@apple:/home/kevin/docker# docker rm $CONTAINER_ID      # 刪除容器

3.4,第四步余佃,將IMAGE上傳到倉庫暮刃,或者保存為本地離線文件

push image到阿里云的私有鏡像倉庫

# docker login --username=670590744@qq.com registry.cn-beijing.aliyuncs.com
# docker tag 56a4eab7dc5b registry.cn-beijing.aliyuncs.com/zhangsp/aiwen:visual_init
# docker push registry.cn-beijing.aliyuncs.com/zhangsp/aiwen:visual_init

上傳成功怖亭,如下圖所示

push docker image to ali cloud.png

保存docker image為本地文件搀擂,然后疟位,可以在其他機器上還原使用

root@apple:/home/kevin# docker images
REPOSITORY                                       TAG                 IMAGE ID            CREATED             SIZE
learn/visual_init                                v1                  56a4eab7dc5b        3 minutes ago       321MB
registry.cn-beijing.aliyuncs.com/zhangsp/aiwen   visual_init         56a4eab7dc5b        3 minutes ago       321MB
ubuntu                                           latest              14f60031763d        3 days ago          120MB
hello-world                                      latest              1815c82652c0        5 weeks ago         1.84kB

# 將docker image保存為離線的本地文件偏灿,執(zhí)行docker save image_name > ./save.tar 或者 docker save -o filepath image_name
root@apple:/home/kevin/docker# docker save -o visual_init.tar 56a4eab7dc5b
root@apple:/home/kevin/docker# ll
total 344956
drwxrwxr-x  2 kevin kevin      4096 Jul 24 08:14 ./
drwxr-xr-x 14 kevin kevin      4096 Jul 24 03:07 ../
-rw-rw-r--  1 kevin kevin  19182004 May  5 13:03 docker-engine_17.05.0~ce-0~ubuntu-trusty_amd64.deb
-rw-rw-r--  1 kevin kevin     38292 Feb  7  2016 libltdl7_2.4.6-0.1_amd64.deb
-rw-------  1 root           root           333993472 Jul 24 08:13 visual_init.tar

3.5,第五步国撵,在其他機器上還原IMAGE測試使用

  • 加載一個docker鏡像文件怀伦,執(zhí)行docker load -i filepath 或者 docker load < ./save.tar
  • 執(zhí)行docker pull 或者 docker run柳琢,運行倉庫的image鏡像
aiwen@703:~$ docker pull registry.cn-beijing.aliyuncs.com/zhangsp/aiwen:visual_init
visual_init: Pulling from zhangsp/aiwen
e0a742c2abfd: Pull complete 
486cb8339a27: Pull complete 
dc6f0d824617: Pull complete 
4f7a5649a30e: Pull complete 
672363445ad2: Pull complete 
6f2c8900b1ea: Pull complete 
Digest: sha256:faf4284d0980958df3c10c49efcc09a041dc894edd54e29304fb7fbe97c674dc
Status: Downloaded newer image for registry.cn-beijing.aliyuncs.com/zhangsp/aiwen:visual_init
  • 備注:導(dǎo)入IMAGE之后坏瘩,可以通過docker tag命令對IMAGE打標簽
kevin@orange:~/docker$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
<none>              <none>              293e064b491b        24 hours ago        321.2 MB
kevin@orange:~/docker$ docker tag 293e064b491b aiwen/learn:v1
kevin@orange:~/docker$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
aiwen/learn         v1                  293e064b491b        24 hours ago        321.2 MB

4盅抚,參考頁面

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市倔矾,隨后出現(xiàn)的幾起案子妄均,更是在濱河造成了極大的恐慌,老刑警劉巖哪自,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件丛晦,死亡現(xiàn)場離奇詭異,居然都是意外死亡提陶,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門匹层,熙熙樓的掌柜王于貴愁眉苦臉地迎上來隙笆,“玉大人,你說我怎么就攤上這事升筏〕湃幔” “怎么了?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵您访,是天一觀的道長铅忿。 經(jīng)常有香客問我,道長灵汪,這世上最難降的妖魔是什么檀训? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任柑潦,我火速辦了婚禮,結(jié)果婚禮上峻凫,老公的妹妹穿的比我還像新娘渗鬼。我一直安慰自己,他們只是感情好荧琼,可當我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布譬胎。 她就那樣靜靜地躺著,像睡著了一般命锄。 火紅的嫁衣襯著肌膚如雪堰乔。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天脐恩,我揣著相機與錄音镐侯,去河邊找鬼。 笑死被盈,一個胖子當著我的面吹牛析孽,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播只怎,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼袜瞬,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了身堡?” 一聲冷哼從身側(cè)響起邓尤,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎贴谎,沒想到半個月后汞扎,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡擅这,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年澈魄,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片仲翎。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡痹扇,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出溯香,到底是詐尸還是另有隱情鲫构,我是刑警寧澤,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布玫坛,位于F島的核電站结笨,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜炕吸,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一伐憾、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧算途,春花似錦塞耕、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至廓脆,卻和暖如春筛谚,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背停忿。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工驾讲, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人席赂。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓吮铭,卻偏偏與公主長得像,于是被迫代替她去往敵國和親颅停。 傳聞我的和親對象是個殘疾皇子谓晌,可洞房花燭夜當晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容