docker常用簡單命令及栗子

2020-08-14 21:08:15

docker pull 下載映象

[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
6ec8c9369e08: Pull complete 
d3cb09a117e5: Pull complete 
7ef2f1459687: Pull complete 
e4d1bf8c9482: Pull complete 
795301d236d7: Pull complete 
Digest: sha256:0e188877aa60537d1a1c6484b8c3929cfe09988145327ee47e8e91ddf6f76f5c
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

docker images 檢視原生的映象

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              8cf1bfb43ff5        5 days ago          132MB
centos              latest              831691599b88        5 weeks ago         215MB

docker search搜尋映象

[root@localhost ~]# docker search nginx
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                              Official build of Nginx.                        13524               [OK]                
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con…   1846                                    [OK]
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable of…   780                                     [OK]
linuxserver/nginx                  An Nginx container, brought to you by LinuxS…   123                                     
bitnami/nginx                      Bitnami nginx Docker Image                      87                                      [OK]
tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rtmp…   85                                      [OK]
jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ho…   73        
參數說明:
NAME: 映象倉庫源的名稱
DESCRIPTION: 映象的描述
OFFICIAL: 是否 docker 官方發佈
stars: 類似 Github 裏面的 star,表示點贊、喜歡的意思。
AUTOMATED: 自動構建。

docker tag 給映象打標籤

[root@localhost ~]# docker tag centos test	//新增test 跟centos相同
[root@localhost ~]# docker tag centos centos:test //新增centos並改變TAG
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              8cf1bfb43ff5        5 days ago          132MB
centos              latest              831691599b88        5 weeks ago         215MB
centos              test                831691599b88        5 weeks ago         215MB
test                latest              831691599b88        5 weeks ago         215MB

docker run -itd映象啓動爲容器

[root@localhost ~]# docker run -itd centos
8841b9750d83a5d734f1124066dfcec79b9531be7ead3dc71af950339e43b09e
參數說明:
-a stdin: 指定標準輸入輸出內容型別,可選 STDIN/STDOUT/STDERR 三項;
-d: 後臺執行容器,並返回容器ID;
-i: 以互動模式執行容器,通常與 -t 同時使用;
-P: 隨機埠對映,容器內部埠隨機對映到主機的埠
-p: 指定埠對映,格式爲:主機(宿主)埠:容器埠
-t: 爲容器重新分配一個僞輸入終端,通常與 -i 同時使用;
--name="nginx-lb": 爲容器指定一個名稱;
--dns 8.8.8.8: 指定容器使用的DNS伺服器,預設和宿主一致;

docker ps 檢視執行的容器

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
8841b9750d83        centos              "/bin/bash"         16 seconds ago      Up 15 seconds                           eloquent_khorana
-a選項後可以檢視所有容器,包括未執行的

docker rmi centos 刪除指定映象

[root@localhost ~]# docker rmi test
Untagged: test:latest
[root@localhost ~]# docker rmi centos
Untagged: centos:latest
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              8cf1bfb43ff5        5 days ago          132MB
centos              test                831691599b88        5 weeks ago         215MB
後面的參數可以是tag,如果是tag時,實際上是刪除該tag。當後面的參數爲映象ID時,則會徹底刪除整個映象,所有標籤也會一同刪除。