3.Docker 的常用命令

概況

  • 如何獲得docker命令的幫助
  • docker 的常用命令

Docker命令使用幫助

Docker Client
Docker 命令行參考手冊(cè)

Docker 管理命令

命令 描述
dockerd Launch the Docker daemon
info Display system-wide information
inspect Return low-level information on a container or image
version Show the Docker version information

Docker Image 命令

命令 描述
build Build an image from a Dockerfile
commit Create a new image from a container’s changes
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
load Load an image from a tar archive or STDIN
rmi Remove one or more images
save Save images to a tar archive
tag Tag an image into a repository

Docker Container 命令

命令 描述
attach Attach to a running container
cp Copy files/folders from a container to a HOSTDIR or to STDOUT
create Create a new container
diff Inspect changes on a container’s filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container’s filesystem as a tar archive
kill Kill a running container
logs Fetch the logs of a container
pause Pause all processes within a container
port List port mappings or a specific mapping for the container
ps List containers
rename Rename a container
restart Restart a running container
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop a running container
top Display the running processes of a container
unpause Unpause all processes within a container
update Update configuration of one or more containers
wait Block until a container stops, then print its exit code

Docker Registry 命令

命令 描述
login Register or log in to a Docker registry
logout Log out from a Docker registry
pull Pull an image or a repository from a Docker registry
push Push an image or a repository to a Docker registry
search Search the Docker Hub for images

網(wǎng)絡(luò)和連通性命令
Docker 的network 一般為:null,host,bridgeDocker啟動(dòng)容器的時(shí)候嗡呼,默認(rèn)使用 bridge 網(wǎng)絡(luò)

命令 描述
network connect Connect a container to a network
network create Create a new network
network disconnect Disconnect a container from a network
network inspect Display information about a network
network ls Lists all the networks the Engine daemon knows about
network rm Removes one or more networks

共享數(shù)據(jù)卷命令

共享數(shù)據(jù)卷命令咪奖,主要作用是能夠讓不同的容器可以共享數(shù)據(jù)而克。默認(rèn)情況下笛厦,Docker為每個(gè)容器生成一個(gè)隨機(jī)名字的目錄來存放容器的數(shù)據(jù),這樣就不能實(shí)現(xiàn)容器之間的共享蝶缀。使用 volume 命令,可以指定容器的數(shù)據(jù)目錄薄货,這樣就可以做到多個(gè)容器之間數(shù)據(jù)共享

命令 描述
volume create Creates a new volume where containers can consume and store data
volume inspect Display information about a volume
volume ls Lists all the volumes Docker knows about
volume rm Remove one or more volumes

常用命令

[root@localhost ~]# docker help
Usage: docker [OPTIONS] COMMAND [arg...]
       docker [ --help | -v | --version ]

A self-sufficient runtime for containers.

Options:

  --config=~/.docker              Location of client config files
  -D, --debug                     Enable debug mode
  -H, --host=[]                   Daemon socket(s) to connect to
  -h, --help                      Print usage
  -l, --log-level=info            Set the logging level
  --tls                           Use TLS; implied by --tlsverify
  --tlscacert=~/.docker/ca.pem    Trust certs signed only by this CA
  --tlscert=~/.docker/cert.pem    Path to TLS certificate file
  --tlskey=~/.docker/key.pem      Path to TLS key file
  --tlsverify                     Use TLS and verify the remote
  -v, --version                   Print version information and quit

Commands:
    attach    Attach to a running container
    build     Build an image from a Dockerfile
    commit    Create a new image from a container's changes
    cp        Copy files/folders between a container and the local filesystem
    create    Create a new container
    diff      Inspect changes on a container's filesystem
    events    Get real time events from the server
    exec      Run a command in a running container
    export    Export a container's filesystem as a tar archive
    history   Show the history of an image
    images    List images
    import    Import the contents from a tarball to create a filesystem image
    info      Display system-wide information
    inspect   Return low-level information on a container, image or task
    kill      Kill one or more running containers
    load      Load an image from a tar archive or STDIN
    login     Log in to a Docker registry.
    logout    Log out from a Docker registry.
    logs      Fetch the logs of a container
    network   Manage Docker networks
    node      Manage Docker Swarm nodes
    pause     Pause all processes within one or more containers
    port      List port mappings or a specific mapping for the container
    ps        List containers
    pull      Pull an image or a repository from a registry
    push      Push an image or a repository to a registry
    rename    Rename a container
    restart   Restart a container
    rm        Remove one or more containers
    rmi       Remove one or more images
    run       Run a command in a new container
    save      Save one or more images to a tar archive (streamed to STDOUT by default)
    search    Search the Docker Hub for images
    service   Manage Docker services
    start     Start one or more stopped containers
    stats     Display a live stream of container(s) resource usage statistics
    stop      Stop one or more running containers
    swarm     Manage Docker Swarm
    tag       Tag an image into a repository
    top       Display the running processes of a container
    unpause   Unpause all processes within one or more containers
    update    Update configuration of one or more containers
    version   Show the Docker version information
    volume    Manage Docker volumes
    wait      Block until a container stops, then print its exit code

Run 'docker COMMAND --help' for more information on a command.
[root@localhost ~]# docker version
Client:
 Version:      1.12.2
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   bb80604
 Built:
 OS/Arch:      linux/amd64

Server:
 Version:      1.12.2
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   bb80604
 Built:
 OS/Arch:      linux/amd64
[root@localhost ~]# docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 2
Server Version: 1.12.2
Storage Driver: devicemapper
 Pool Name: docker-253:0-67182400-pool
 Pool Blocksize: 65.54 kB
 Base Device Size: 10.74 GB
 Backing Filesystem: xfs
 Data file: /dev/loop0
 Metadata file: /dev/loop1
 Data Space Used: 472.6 MB
 Data Space Total: 107.4 GB
 Data Space Available: 17.06 GB
 Metadata Space Used: 1.184 MB
 Metadata Space Total: 2.147 GB
 Metadata Space Available: 2.146 GB
 Thin Pool Minimum Free Space: 10.74 GB
 Udev Sync Supported: true
 Deferred Removal Enabled: false
 Deferred Deletion Enabled: false
 Deferred Deleted Device Count: 0
 Data loop file: /var/lib/docker/devicemapper/devicemapper/data
 WARNING: Usage of loopback devices is strongly discouraged for production use. Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.
 Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
 Library Version: 1.02.107-RHEL7 (2016-06-09)
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: null bridge overlay host
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Security Options: seccomp
Kernel Version: 3.10.0-327.36.2.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.522 GiB
Name: localhost.localdomain
ID: VFSO:QWR6:ICKT:QGXY:UQXT:5VKN:L6E3:7JBE:OBVO:SQFK:PU2V:IBMS
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Insecure Registries:
 127.0.0.0/8

實(shí)際示例流程:

  1. 從默認(rèn)的倉庫拉取mysql鏡像
  2. 從國內(nèi)第三方時(shí)速云拉取wordpress鏡像
  3. 查看本地有哪些鏡像
  4. 運(yùn)行mysql鏡像生成名字叫做mysql2的容器翁都,映射主機(jī)端口是13306,root密碼是123456
  5. 查看本地創(chuàng)建了哪些容器
  6. 交互方式進(jìn)入mysql2容器的shell環(huán)境菲驴,創(chuàng)建一個(gè)數(shù)據(jù)庫testdb
[root@localhost ~]# docker pull mysql
[root@localhost ~]# docker pull index.tenxcloud.com/huangqg/wordpress
[root@localhost ~]# docker images
REPOSITORY                              TAG                 IMAGE ID            CREATED             SIZE
mysql                                   latest              eda6a4884645        2 days ago          383.4 MB
hello-world                             latest              c54a2cc56cbb        3 months ago        1.848 kB
index.tenxcloud.com/huangqg/wordpress   latest              88bc02275ad7        12 months ago       485.5 MB

[root@localhost ~]# docker run -d -p 13306:3306  --name mysql2 -v /opt/mydata/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:latest
01ff7db79a2bba578f4cef7024cd1e02bfec1084478e7835a79aeed317278b03
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
01ff7db79a2b        mysql:latest        "docker-entrypoint.sh"   29 seconds ago      Up 27 seconds       0.0.0.0:13306->3306/tcp   mysql2
[root@localhost ~]# docker exec -it mysql2 /bin/bash
root@01ff7db79a2b:/# mysql --version
mysql  Ver 14.14 Distrib 5.7.16, for Linux (x86_64) using  EditLine wrapper
root@01ff7db79a2b:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database testdb;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)


主機(jī)(Win10中文版)訪問虛擬機(jī)MySQL的 Docker 容器 mysql2
看到了 testdb 的存在

D:\>mysql -h 192.168.1.161 -P 13306 -uroot -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database testdb;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末荐吵,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子赊瞬,更是在濱河造成了極大的恐慌先煎,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,470評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件巧涧,死亡現(xiàn)場離奇詭異薯蝎,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)谤绳,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門占锯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來袒哥,“玉大人,你說我怎么就攤上這事消略”こ疲” “怎么了?”我有些...
    開封第一講書人閱讀 162,577評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵艺演,是天一觀的道長却紧。 經(jīng)常有香客問我,道長胎撤,這世上最難降的妖魔是什么晓殊? 我笑而不...
    開封第一講書人閱讀 58,176評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮伤提,結(jié)果婚禮上巫俺,老公的妹妹穿的比我還像新娘。我一直安慰自己肿男,他們只是感情好介汹,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評(píng)論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著次伶,像睡著了一般痴昧。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上冠王,一...
    開封第一講書人閱讀 51,155評(píng)論 1 299
  • 那天赶撰,我揣著相機(jī)與錄音,去河邊找鬼柱彻。 笑死豪娜,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的哟楷。 我是一名探鬼主播瘤载,決...
    沈念sama閱讀 40,041評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼卖擅!你這毒婦竟也來了鸣奔?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,903評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤惩阶,失蹤者是張志新(化名)和其女友劉穎挎狸,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體断楷,經(jīng)...
    沈念sama閱讀 45,319評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡锨匆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了冬筒。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片恐锣。...
    茶點(diǎn)故事閱讀 39,703評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡茅主,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出土榴,到底是詐尸還是另有隱情诀姚,我是刑警寧澤,帶...
    沈念sama閱讀 35,417評(píng)論 5 343
  • 正文 年R本政府宣布鞭衩,位于F島的核電站学搜,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏论衍。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評(píng)論 3 325
  • 文/蒙蒙 一聚磺、第九天 我趴在偏房一處隱蔽的房頂上張望坯台。 院中可真熱鬧,春花似錦瘫寝、人聲如沸蜒蕾。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽咪啡。三九已至,卻和暖如春暮屡,著一層夾襖步出監(jiān)牢的瞬間撤摸,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評(píng)論 1 269
  • 我被黑心中介騙來泰國打工褒纲, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留准夷,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,711評(píng)論 2 368
  • 正文 我出身青樓莺掠,卻偏偏與公主長得像衫嵌,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子彻秆,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評(píng)論 2 353

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

  • 轉(zhuǎn)載自 http://blog.opskumu.com/docker.html 一楔绞、Docker 簡介 Docke...
    極客圈閱讀 10,499評(píng)論 0 120
  • 摘要:繼續(xù)docker的學(xué)習(xí)之旅,今天練習(xí)一些常用的命令: 一唇兑、鏡像相關(guān) 1.1 列出本機(jī)所有鏡像 docker ...
    暖夏未眠丶閱讀 732評(píng)論 0 0
  • 0x01 核心概念 Docker鏡像類似于虛擬機(jī)鏡像酒朵,可以理解為一個(gè)面向Docker引擎的只讀模板,包含了文件系統(tǒng)...
    閑云逸心閱讀 4,714評(píng)論 0 9
  • Docker — 云時(shí)代的程序分發(fā)方式 要說最近一年云計(jì)算業(yè)界有什么大事件幔亥?Google Compute Engi...
    ahohoho閱讀 15,530評(píng)論 15 147
  • 今天我吃的可多了耻讽,我吃的有火龍果,而且是紅心的帕棉,我還喝了一碗很健康的米糊针肥,我還喝了果汁饼记,最重要的是我吃了媽媽親手做...
    王佑滋閱讀 139評(píng)論 0 0