Docker的使用

calong發表於2020-08-23
  1. docker version: docker版本
  2. docker info: docker資訊
  3. docker [command] –help: 獲取命令幫助

1. Docker映象命令

docker images

$ docker images
REPOSITORY    #倉庫
TAG            #標籤
IMAGE ID    #映象
CREATED        #建立時間
SIZE        #大小

1. -a --all        #列出所有映象
2. -q --quiet        #只顯示映象ID

docker search

$ docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9869                [OK]
mariadb                           MariaDB is a community-developed fork of MyS…   3605                [OK]
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   721                                     [OK]

1. --filter=[NAME,STARS...]=... #過濾搜尋

docker pull name[:tag(預設位latest)]

$ docker pull mysql:5.7
5.7: Pulling from library/mysql
bf5952930446: Already exists                                                                                                8254623a9871: Already exists                                                                                                938e3e06dac4: Already exists                                                                                                ea28ebf28884: Already exists                                                                                                f3cef38785c2: Already exists                                                                                                894f9792565a: Already exists                                                                                                1d8a57523420: Already exists                                                                                                5f09bf1d31c1: Pull complete                                                                                                 1b6ff254abe7: Pull complete                                                                                                 74310a0bf42d: Pull complete                                                                                                 d398726627fd: Pull complete                                                                                                 Digest: sha256:da58f943b94721d46e87d5de208dc07302a8b13e638cd1d24285d222376d6d84
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

docker rmi -f 容器ID[:tag] [容器ID…/$(docker) images -aq]

$ docker rmi mysql:5.7
Untagged: mysql:5.7
Untagged: mysql@sha256:da58f943b94721d46e87d5de208dc07302a8b13e638cd1d24285d222376d6d84
Deleted: sha256:718a6da099d82183c064a964523c0deca80619cb033aadd15854771fe592a480
Deleted: sha256:058d93ef2bfb943ba6a19d8b679c702be96e34337901da9e1a07ad62b772bf3d
Deleted: sha256:7bca77783fcf15499a0386127dd7d5c679328a21b6566c8be861ba424ac13e49
Deleted: sha256:183d05512fa88dfa8c17abb9b6f09a79922d9e9ee001a33ef34d1bc094bf8f9f
Deleted: sha256:165805124136fdee738ed19021a522bb53de75c2ca9b6ca87076f51c27385fd7

Docker Commit 提交映象

PS C:\Users\Administrator> docker commit -a="calong" -m="Commit Test" b3a59f64071f tomcal:1.0
sha256:64fdb91ddae709b57ef7dd4f9c8b29ec88f1e0e08039a8d49d07fc595654a8bb
PS C:\Users\Administrator> docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                          NAMES
b3a59f64071f        tomcat              "/bin/bash"         2 minutes ago       Up 2 minutes        0.0.0.0:80->80/tcp, 8080/tcp   upbeat_meitner
PS C:\Users\Administrator> docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tomcal              1.0                 64fdb91ddae7        11 seconds ago      652MB
nginx               latest              4bb46517cac3        9 days ago          133MB
node                latest              784e696f5060        10 days ago         943MB
centos              latest              0d120b6ccaa8        12 days ago         215MB
tomcat              latest              2ae23eb477aa        2 weeks ago         647MB
redis               latest              1319b1eaa0b7        2 weeks ago         104MB
mysql               latest              0d64f46acfd1        2 weeks ago         544MB

2. Docker容器命令

docker run [可選引數] image
–name=“Name” 指定容器名稱
-d 以後臺的方式執行
-p [主機埠:][容器埠] 指定埠/隨機埠

#啟動並執行
$ docker run -it centos /bin/bash
[root@4f73b5364975 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
#退出容器
[root@4f73b5364975 /]# exit
exit

docker ps 列出當前正在執行的容器
-a #列出所有
-n=? #列出最近建立的容器
-q #只顯示容器的編號

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
e1fb88fd4773        centos              "/bin/bash"         9 seconds ago       Up 9 seconds                            jolly_stonebraker

docker rm 刪除容器

$ docker rm -f $(docker ps -aq)
e1fb88fd4773
15f9b0644bca
9774e741f39d
4f73b5364975

3. 其它常用命令

docker start 容器ID 啟動容器
docker restart 容器ID 重啟容器
docker stop 容器ID 停止當前正在執行的容器
docker kill 容器ID 強制停止當前容器

docker run [-d] [-v] 映象名稱 啟動容器
-d: 後臺啟動
-v: 掛載外部目錄

docker logs [可選引數]
-t #時間戳
-f #輸出日誌
–tail 10 #顯示10條日誌

docker inspect 容器ID 檢視映象的後設資料

docker exec [=it] 容器ID 進入正在執行的容器並開啟新的終端
docker attach 容器ID 進入正在執行的容器並開啟正在執行的終端

$ docker run -it centos /bin/bash
[root@962ca12ae7e8 /]#
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
962ca12ae7e8        centos              "/bin/bash"         17 seconds ago      Up 19 seconds                           nervous_keller
$ docker exec -it 962ca12ae7e8 /bin/bash
[root@962ca12ae7e8 /]#

docker cp 容器ID:檔案路徑 目標目錄 從容器中複製檔案

4. Docker Machine 命令

  • 檢視docker虛擬機器的IP地址:docker-machine ip [default]
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章