almost 4 years ago

Environment:

Ubuntu 14.04

Installation

這邊直接安裝Ubuntu maintain的版本

sudo apt-get update

sudo apt-get install docker.io

(注意: 是docker.io,docker是另外的KDE3/GNOME2 package)

完成後可以下sudo docker info確定是否安裝成功

Pull Image

docker hub download pre-build image

sudo docker pull base/archlinux

check一下剛剛pull回來的image

sudo docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
base/archlinux      2014.07.03          ea234cde99e6        5 months ago        282.9 MB
base/archlinux      2014.04.01          56c61f5c2920        5 months ago        293.3 MB
base/archlinux      latest              dce0559daa1b        5 months ago        282.9 MB

Run Container

sudo docker run -t -i base/archlinux:latest bash

(-t 表示terminal -i 表示 stdin 後面接要執行的image及tag 最後才是要跑的程式)

guang@petlab-SZ77:~$ sudo docker run -t -i base/archlinux:latest bash
[root@43933e26eb35 /]#

Commit Image

每次運行container時,都是重新載入image。所以如果沒有commit,下一次運行又會是嶄新的一頁

先touch file

[root@4f2fd20777a8 /]# touch HELLO-DOCKER!
[root@4f2fd20777a8 /]# ls
HELLO-DOCKER!  bin  boot  dev  etc  home  lib  lib64  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

查看目前運行的container

sudo docker ps -a

CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS              PORTS               NAMES
4f2fd20777a8        base/archlinux:latest   bash                29 seconds ago      Up 29 seconds                           thirsty_torvalds

commit image

sudo docker commit -m "First commit!" 4f2fd20777a8 my_arch

這樣子就會把state存下來了

sudo docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
my_arch             latest              40ad47b774e1        44 seconds ago      282.9 MB
base/archlinux      2014.07.03          ea234cde99e6        5 months ago        282.9 MB
base/archlinux      2014.04.01          56c61f5c2920        5 months ago        293.3 MB
base/archlinux      latest              dce0559daa1b        5 months ago        282.9 MB

可以看到多了一個剛剛commit的image

下一次就可以直接使用已經my_arch的這個images囉

guang@petlab-SZ77:~$ sudo docker run -t -i my_arch bash
[root@b267cc485624 /]# ls
HELLO-DOCKER!  bin  boot  dev  etc  home  lib  lib64  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

Check container details

sudo docker inspect [container ID]

透過inspect可以看continer的狀態,比如說container的IP

Stop container

sudo docker stop [container ID]

若是要將container移除,一定要先stop

stop後也可用sudo docker start [container ID]重新執行

Remove container

sudo docker rm [container ID]

一次移除所有container
sudo docker ps -a -q | xargs sudo docker rm

Remove Image

sudo docker rmi [Image ID]

Reference

https://docs.docker.com/installation/ubuntulinux/#ubuntu-trusty-1404-lts-64-bit
https://docs.docker.com/articles/basics/
http://philipzheng.gitbooks.io/docker_practice/content/appendix_command/README.html

← Unix C Pipe 基本操作 Docker SSH service →
 
comments powered by Disqus