當前位置:生活全書館 >

IT科技

> docker的基本命令

docker的基本命令

產品型號:Thinkpad E15

系統版本:centos8

docker命令教程

例1:執行一個 container並載入映象centos,執行起來這個例項後,在例項中執行 /bin/bash命令

docker常用引數:

docker的基本命令

run  執行

-i   以互動模式執行容器,通常與 -t 同時使用;

-t  為容器重新分配一個偽輸入終端,通常與 -i 同時使用;

[root@xuegod63 ~]# docker images 

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

centos    latest              196e0ce0c9fb        12 days ago         196.6 MB

[root@xuegod63 ~]# docker run -it centos:latest bash  #啟動一個例項,也就2秒就可以搞定

[root@068fd8c70344 /]# ls   #檢視例項環境

[root@f072b5ae7542 /]# cat /etc/redhat-release 

退出容器:

[root@f072b5ae7542 /]#exit

例2:在 container 中啟動一個長久執行的程序,不斷向stdin輸出 hello world 。模擬一個後臺執行的服務

docker常用引數:

-d  後臺執行容器,並返回容器ID;

-c  後面跟待完成的命令

[root@xuegod63 ~]# docker run  -d centos:latest /bin/sh -c "while true;do echo hello world; sleep 1; done"

1b3493487c4fde6eb233d59fa9ab9a204ad993cd3debbd5a9a28be6816694605

#容器的ID

從一個容器中取日誌,檢視輸出的內容,可用於後期檢查docker例項在標準輸出中彈出的錯誤資訊或正常的資訊。 

語法: docker  logs   容器例項的Name/ID  

[root@xuegod63 ~]# docker logs 1b3493487c4    #容器的ID可以寫全,也可以不寫全,只要唯一就可以了

hello world

hello world

hello world

hello world

檢視正在執行的容器:

[root@xuegod63 ~]# docker ps  #列出所有執行中容器。

也可以使用短ID或docker例項的名字檢視日誌輸出:

[root@xuegod63 ~]# docker logs 4109c3446284

或:

[root@xuegod63 ~]# docker logs flamboyant_davinci

[root@xuegod63 ~]# docker ps -a  #-a 列出所有容器(包含沉睡/退出狀態的容器);

總結:

1、docker run -it centos:latest bash  啟動一個例項 

2、docker run  -d centos:latest /bin/sh -c "while true;do echo hello world; sleep 1; done"  #後臺執行容器,並返回容器ID;

3、docker  logs   容器例項的Name/ID從一個容器中取日誌,檢視輸出的內容

4、docker ps  #列出所有執行中容器。

標籤: docker 命令
  • 文章版權屬於文章作者所有,轉載請註明 https://shqsg.com/dianzi/pvey9.html