2015年2月9日 星期一

docker 使用心得


查docker資訊

# docker info

下載archlinux 的docker images
# docker pull base/archlinux

查有哪些images
# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
base/archlinux      latest              dce0559daa1b        6 months ago        282.9 MB

執行容器
# docker run -t -i base/archlinux:latest bash
[root@b42626ce6a25 /]#
進入容器後,因為root家目錄是/,直接設定HOME環境變數即可 。相關issue:https://github.com/docker/docker/issues/2968
[root@b42626ce6a25 /]# HOME=/root

查docker的環境變數
# docker run -i -t bear-test env|grep HOME
HOME=/

修改Arch的 Mirrors
編輯:/etc/pacman.d/mirrorlist
去 https://www.archlinux.org/mirrorlist/ 選擇你的國家,直接貼上產生的mirrorlist,然後更新
# pacman -Sy

安裝wget
# pacman -S wget

保存目前的容器更變

在主機系統上開一個新的終端機
查看目前正在運行的容器狀態
# docker ps   # -a tag: Show all containers.不加只會顯示在跑的containers
CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS              PORTS               NAMES
b42626ce6a25        base/archlinux:latest   "bash"              17 minutes ago      Up 17 minutes                           elated_fermi

commit更變
# docker commit -m "Add wget!" -a "Bear" b42626ce6a25 bear-test

查commit messages
# docker inspect image_id | grep Comment
ex. 
# docker inspect d98d54bb671a | grep Comment
   "Comment": "Add wget!",

docker logs
# docker logs b42626ce6a25
( 會顯示你的console 畫面的紀錄 )

查docker comment message
# docker images --no-trunc -a | awk '{print $3}' | xargs docker inspect | less

保存到https://hub.docker.com ( 需先去該docker hub註冊帳號 )
# docker login 
Username: username
Password: 
Email: your@email
Login Succeeded

push
# docker push bear-test
FATA[0000] You cannot push a "root" repository. Please rename your repository in <user>/<repo> (ex: kalecgos0616/bear-test)
所以當初在命名repository名字時,要以<user>/<repo> 的格式命名

複製且重新命名docker images
# docker tag image_id username/repository_name
ex.
# docker tag d98d54bb671a kalecgos0616/bear-test

再次push到docker hub
# docker push kalecgos0616/bear-test
The push refers to a repository [kalecgos0616/bear-test] (len: 1)
Sending image list
Pushing repository kalecgos0616/bear-test (1 tags)
511136ea3c5a: Image already pushed, skipping 
9b0516337e5a: Image already pushed, skipping 
dce0559daa1b: Image already pushed, skipping 
d98d54bb671a: Image successfully pushed 
Pushing tag for rev [d98d54bb671a] on {https://cdn-registry-1.docker.io/v1/repositories/kalecgos0616/bear-test/tags/latest}

這時候你上docker hub ( https://hub.docker.com/u/kalecgos0616/ ),就會看到你剛剛推上去的docker Repository

移除images
# docker rmi base/archlinux
如果報錯
# docker rmi bear-test    
Error response from daemon: Conflict, cannot delete debdf1ba8b30 because the container 113b1d60ffe1 is using it, use -f to force
FATA[0000] Error: failed to remove one or more images
則使用-f 強制刪除
# docker rmi -f bear-test

pull docker hub上的repository
# docker pull kalecgos0616/bear-test

容器使用host的ip和port
# docker run -t -i -p <hostPort>:<containerPort> REPOSITORY:TAG bash
例:
# docker run -t -i -p 5566:80 base/archlinux:latest bash

將容器的80 port 發布到 host的5566 port,假設host 的ip 是 192.168.1.2 ,即可以 http://192.168.1.2:5566 訪問容器的網頁伺服器


centos:6.6 安裝執行screen會報錯
Cannot access '/7': No such file or directory
解法:
改使用centos:latest ( CentOS 7)

下載官方的repos
https://registry.hub.docker.com/repos/library/ 找你要下載的repos,假設要下載ubuntu最新版
# docker pull ubuntu
如要下載centos 6.6(Tag)版,可以指定Tags
# docker pull centos:6.6

參考資料:
https://wiki.archlinux.org/index.php/Docker
http://www.codedata.com.tw/social-coding/docker-layman-abc/ # 門外漢的 Docker 小試身手
http://stackoverflow.com/questions/19035358/how-to-copy-and-rename-a-docker-container # 複製且重新命名docker container

沒有留言:

張貼留言