在使用前,想說找一下 Docker 的商業模式,找了很久都沒看到 XD 直到使用 Docker hub 時才發現,商業模式就像 github/bitbucket 一樣,想要建立 private repo 的則需要付費,此例就是可以打包自己的開發環境送到 Docker hub 上使用,免費方案有提供 1 個 private 單位哦。
回到筆記,根據官網介紹 https://docs.docker.com/installation/ubuntulinux/,依序幾個動作就完成安裝了:
$ sudo apt-get update
$ sudo apt-get install docker.io
$ sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker
$ sudo sed -i '$acomplete -F _docker docker' /etc/bash_completion.d/docker.io
開始使用:
$ sudo docker run ubuntu:12.04 echo "hello world"
Unable to find image 'ubuntu:12.04' locally
Pulling repository ubuntu
822a01ae9a15: Download complete
511136ea3c5a: Download complete
93c381d2c255: Download complete
a5208e800234: Download complete
9fccf650672f: Download complete
1186c90e2e28: Download complete
f6a1afb93adb: Download complete
hello world
此命令是說要跑一個 ubuntu 12.04 環境,在其環境執行 echo "hello world" 的意思,整個過程就是包含初始化(下載image)後,最後在執行 echo "hello world",有興趣可以再執行一次,這次就不會再去下載 image 了。
然而,上述的動作只是一次性的,舉例來說,初始環境執行 apt-get install curl 是會出錯的:
$ sudo docker run ubuntu:12.04 apt-get install curl
Reading package lists...
Building dependency tree...
Reading state information...
E: Unable to locate package curl
但只要 apt-get update 後就會正常,但不能分開執行:
$ sudo docker run ubuntu:12.04 apt-get update
Get:1 http://archive.ubuntu.com precise Release.gpg [198 B]
Get:2 http://archive.ubuntu.com precise-updates Release.gpg [198 B]
Get:3 http://archive.ubuntu.com precise-security Release.gpg [198 B]
...
Reading package lists...
$ sudo docker run ubuntu:12.04 apt-get install curl
Reading package lists...
Building dependency tree...
Reading state information...
E: Unable to locate package curl
因此,正式的使用其實是開個 terminal 進去連續動作:
$ sudo docker run -t -i ubuntu:12.04
root@431bb00c701d:/#
其中 431bb00c701d 則是此 container ID,
root@431bb00c701d:/# apt-get install wget
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package wget
root@431bb00c701d:/# apt-get update
...
root@431bb00c701d:/# apt-get install wget
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libidn11
The following NEW packages will be installed:
libidn11 wget
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 391 kB of archives.
After this operation, 966 kB of additional disk space will be used.
Do you want to continue [Y/n]?
接著按 exit 離開後,其實就跟上述單行指令操作一樣,什麼都沒留下,因此在離開之前,想要保存環境則是要進行 commit。
此時,必須額外再開一個 terminal 用 docker ps 查看目前已執行的 image 環境:
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5db62a9bf492 ubuntu:14.04 /bin/bash 11 seconds ago Up 10 seconds goofy_curie
431bb00c701d ubuntu:12.04 /bin/bash 35 seconds ago Up 34 seconds agitated_thompson
此例代表有2個 container 在運行,此例目標是 ubuntu:12.04 的 431bb00c701d ,想要儲存環境變化,就來個 commit 吧
$ sudo docker commit -m 'ubuntu 12.04 with wget' 431bb00c701d
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
<none> <none> aac0f5ce2936 9 minutes ago 137.7 MB
ubuntu 14.04 c4ff7513909d 7 days ago 213 MB
ubuntu 12.04 431bb00c701d 7 days ago 108 MB
想要有更佳的描述,就在 docker commit 時,多加一點資訊 REPOSITORY[:TAG] 吧:
$ sudo docker commit -m 'ubuntu 12.04 with wget' e7e35769932d MyUbuntu:12.04
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
MyUbuntu 12.04 d3466dce0e51 About a minute ago 108 MB
<none> <none> aac0f5ce2936 10 minutes ago 137.7 MB
ubuntu 14.04 c4ff7513909d 7 days ago 213 MB
ubuntu 12.04 431bb00c701d 7 days ago 108 MB
下次要用時:
$ sudo docker run -i -t MyUbuntu:12.04
root@600d3fee924d:/# wget
wget: missing URL
Usage: wget [OPTION]... [URL]...
Try `wget --help' for more options.
@ 2014-10-24 加映場:[Linux] Docker 使用筆記 - 常用指令 @ Ubuntu 14.04
沒有留言:
張貼留言