歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 我的docker常用命令清單

我的docker常用命令清單

日期:2017/3/3 12:28:41   编辑:Linux技術

我的docker命令清單

2016-5-23

一)總體上docker的命令比較簡單,但是,有些稍微復雜點的還是需要記錄一下。

需要root權限,不足的時候,報錯如下:[ec2-user@ip-172-30-0-43~]$ docker ps -a

Cannotconnect to the Docker daemon. Is the docker daemon running on thishost?1. 進入容器(exec attach命令)

先看下這兩個命令在help中的說明

attach Attach to a running containerexec Run a command in a running container

1.1 docker exec -it containerID /bin/bash

先看下常用的三個

OPTIONS 的說明:

-d, --detach=false Detachedmode: run command in the background

-i, --interactive=false Keep STDINopen even if not attached

-t, --tty=false Allocate apseudo-TTY

(參數可以連寫在一起,如-it等同-i -t

關於-i,-t參數

可以看出只用-i時,由於沒有分配偽終端,看起來像pipe執行一樣。但是執行結果、命令

返回值都可以正確獲取。

[root@ip-172-30-0-43home]# docker exec -i myTest /bin/shdate

MonMay 23 08:30:02 UTC 2016cat/etc/hostname

consul-server-1exit

[root@ip-172-30-0-43home]#1.2 docker attach containerID

附加到一個正在運行的容器上。

可以運行多次,都會附加到容器內PID=1的進程上,共享輸入輸出。

所以,退出的時候,容器也會退出。

這個命令排第一,是因為不論是開發者是運維人員,都經常有需要進入容器的訴求。

2. 刪除所有容器(rm命令)

docker rm$(docker ps -a -q)這個命令分兩步,

docker ps -a-q,以安靜模式列出所有的容器,也就是只列出隨機唯一名字(numericID)

docker rm ,依次(循環)刪除列表中的容器,

刪除正在運行的,會報錯。

docker rm -f<容器名orID> ,強制刪除3. 啟動容器(run命令)

給出一個例子

docker run --name redmine -p 9003:80 -p9023:22 -d -v /var/redmine/files:/redmine/files -v /var/redmine/mysql:/var/lib/mysql sameersbn/redmine

說明:

容器名字是redmine

綁定容器內的9003端口到主機的80端口,綁定9023端口到22端口

將容器的/var/redmine/files文件或者目錄映射到主機的/redmine/files

執行容器內的sameersbn/redmine

4. 容器的基本操作

docker stop<容器名orID>docker start<容器名orID>

docker restart<容器名orID>docker kill<容器名orID>

還有三個與暫停相關的命令pause,unpause,waitstop Stop a running container

start Start one or more stoppedcontainers

restart Restart a container

kill Kill a running container

pause Pause all processeswithin a container

unpause Unpause all processeswithin a container

wait Block until a containerstops, then print its exit code

5. 刪除所有鏡像

docker rmi$(dockerimages -q)dockerimages-q

只列出了鏡像的ID

dockerrmi

用於刪除鏡像

docker rmi$(docker images | grep none | awk '{print $3}' | sort -r)docker images | grep none | awk '{print$3}' | sort -r只列出untaggedimages,也就是鏡像名字為<None>的,取出ID,降序排列

6. 一個容器連接到另一個容器

docker run -it --name sonar -d

--linkmmysql:db tpires/sonar-serversonar容器連接到mmysql容器,並將mmysql容器重命名為db。

這樣,sonar容器就可以使用db的相關的環境變量了。

這樣在 容器sonar

內 查看 /etc/hosts文件的時候,發現增加一條記錄,映射主機名到一條IP地址。

這樣在容器sona內,就可以直接解析db了。

7. 鏡像遷移

當需要把一台機器上的鏡像遷移到另一台機器的時候,需要保存鏡像與加載鏡像。

機器1:

docker savebusybox > /home/save.tar使用scp將save.tar拷到機器2上,然後:

docker load< /home/save.tarsave Save an image(s) to a tararchive

load Load an image from a tararchive or STDIN

8. 推拉鏡像

拉取鏡像

docker pull<鏡像名:tag>如:

docker pull sameersbn/redmine:latest

docker pull 172.30.0.43:5000/myhello

docker push<鏡像名:tag>如:

docker push 172.30.0.43:5000/myhello

docker push 172.30.0.43:5000/myhello:v2

docker push 172.30.0.43:5000/myhello:latest

docker push 172.30.0.43:5000/myhello

pull Pull an image or arepository from a registry

push Push an image or arepository to a registry

tag Tag an image into arepository

9. 構建自己的鏡像

docker build-t <鏡像名> <Dockerfile路徑>如Dockerfile在當前路徑:dockerbuild -t xx/gitlab .

10. 鏡像操作

docker search <keywords>docker history <鏡像名>

docker commit <容器名or ID> <新鏡像名>保存對容器的修改docker build <標簽> <Docker-file路徑>

search Search the Docker Hub forimages

history Show the history of animage

commit Create a new image from acontainer's changes

build Build an image from aDockerfile

import Import the contents froma tarball to create a filesystem image

inspect Return low-levelinformation on a container or image

11. 容器操作(2)

文件系統相關

docker diff <容器名or ID>查看容器內文件的改變docker export -o <文件名> <容器名orID>導出文件到tar包

docker cp在容器和主機之間復制文件統計相關

docker top <容器名or ID>顯示容器內進程信息docker stats <容器名or ID>顯示容器內統計信息

docker port <容器名or ID>

顯示容器與主機端口映射信息

docker inspect <容器名or ID>顯示容器底層細節其他

docker logs-f <容器名or ID>查看運行日志docker rename<容器名> <新名字>改名

diff Inspect changes on acontainer's filesystem

export Export a container'sfilesystem as a tar archive

cp Copy files/foldersbetween a container and the local filesystem

top Display the runningprocesses of a container

stats Display a live stream ofcontainer(s) resource usage statistics

port List port mappings or aspecific mapping for the CONTAINER

inspect Return low-levelinformation on a container or image

logs Fetch the logs of acontainer

rename Rename a container

12. 查看容器的root用戶密碼(這個沒有驗證)

docker logs <容器名orID>2>&1 | grep '^User: ' | tail -n1

因為docker容器啟動時的root用戶的密碼是隨機分配的。所以,通過這種方式就可以得到redmine容器的root用戶的密碼了。

二)docker鏡像的如何命名–Registry,Repository,Image

and Tag 概念與關系

本地執行docker images的結果

大致關系如下:

Registry存儲鏡像數據,並且提供拉取和上傳鏡像的功能。Registry中鏡像是通過Repository來組織的,而每個Repository又包含了若干個Image。

Registry包含一個或多個Repository

Repository包含一個或多個Image

Image用GUID表示,有一個或多個Tag與之關聯

鏡像一般有兩種方式,一種是從官方Registry地址(DockerHub)中獲取,一般格式為

<username>/<repo_name><:tag>

另外一種則是使用私有的registry,地址大致如下

172.30.0.43:5000/library/myhello:latest

其中library可以省略,:latest也可以省略。

這兩者之前的明確區別是私有registry,以ip地址或者hostname開始,官方registry不帶ip地址。

地址中,如果tag如果不存在,會使用默認的‘DEFAULTTAG’,即latest

Copyright © Linux教程網 All Rights Reserved