docker的一些基本指令

   最近的工作中,MySQL和MongoDB運維部分的內容比較少。主要工作內容都會頻繁接觸docker和k8s相關的內容,所以準備寫寫這塊兒的內容。

   之前的文章中,介紹過docker上部署MySQL的過程,這裡貼出來鏈接,方便回顧:

docker上部署MySQL實例

上面這篇文章中,已經講瞭docker是什麼、和虛擬機的區別、怎麼安裝以及基本的使用方法,下面我們再來看看其他方面的知識。

一些基本指令

1、查看當前機器的容器情況

    通常情況下,我們可以使用docker ps命令來查看當前機器上運行瞭哪些容器,如下:

[[email protected] ~]# docker ps 
CONTAINER ID        IMAGE                                                        COMMAND                  CREATED             STATUS              PORTS               NAMES
7a57ee5286e8        kubeguide/tomcat-app:v1                                      "catalina.sh run"        8 days ago          Up 8 days                               k8s_myweb.7c32387a_myweb-8gmc1_default_b44279ec-48b9-11eb-86e4-5254002dbd85_ec48ef90
b2ad9bea8d0d        kubeguide/tomcat-app:v1                                      "catalina.sh run"        8 days ago          Up 8 days                               k8s_myweb.7c32387a_myweb-60r22_default_b4426b29-48b9-11eb-86e4-5254002dbd85_28dd55c9
4f8f6ea37578        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           8 days ago          Up 8 days                               k8s_POD.24f70ba9_myweb-8gmc1_default_b44279ec-48b9-11eb-86e4-5254002dbd85_2d2aa42e
3d198ee60f2a        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           8 days ago          Up 8 days                               k8s_POD.24f70ba9_myweb-60r22_default_b4426b29-48b9-11eb-86e4-5254002dbd85_416acda7
94332658780b        busybox                                                      "sh -c 'tail -f /l..."   6 weeks ago         Up 6 weeks                              k8s_busybox.5057389f_volume-pod_default_13702698-2b17-11eb-86e4-5254002dbd85_a553d58b
f11f11999c42        tomcat                                                       "catalina.sh run"        6 weeks ago         Up 6 weeks                              k8s_tomcat.aeb855f2_volume-pod_default_13702698-2b17-11eb-86e4-5254002dbd85_aa729e3d
d2ec1526079f        kubeguide/redis-master                                       "redis-server /etc..."   6 weeks ago         Up 6 weeks                              k8s_redis.deb7361f_redis-php_default_f70a055b-2b18-11eb-86e4-5254002dbd85_23135019
5b83d7645b1f        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           6 weeks ago         Up 6 weeks                              k8s_POD.3fe22e5d_redis-php_default_f70a055b-2b18-11eb-86e4-5254002dbd85_c418f108
3b7634ee3eff        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           6 weeks ago         Up 6 weeks                              k8s_POD.24f70ba9_volume-pod_default_13702698-2b17-11eb-86e4-5254002dbd85_0610ed58
4784586d01e0        mysql                                                        "docker-entrypoint..."   6 weeks ago         Up 6 weeks                              k8s_mysql.16d54d16_mysql-pd7jr_default_0032bce0-2b0f-11eb-98ad-5254002dbd85_d775f414
c8acc287dc06        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           6 weeks ago         Up 6 weeks                              k8s_POD.1d520ba5_mysql-pd7jr_default_0032bce0-2b0f-11eb-98ad-5254002dbd85_94c9c30a

其他說明:

-a參數用於查看所有的容器,包括正在運行的很停止的,如果不加-a,則隻顯示正在運行的容器,

-l參數,該參數會列出來最後一次運行的容器,包括正在運行的和停止的。

-q參數,quite,隻顯示container ID

-s參數,size,顯示總的文件大小

2、下載或者拉取某個鏡像。

    可以使用docker pull或者docker run的方法,如下:

[[email protected] ~]# docker run -i -t ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
Trying to pull repository docker.io/library/ubuntu ... 
latest: Pulling from docker.io/library/ubuntu
da7391352a9b: Downloading [=>                                                 ] 588.8 kB/28.56 MB
14428a6d4bcd: Download complete 
2c2d948710f2: Download complete 

[[email protected] ~]# docker pull mysql
Using default tag: latest
Trying to pull repository docker.io/library/mysql ...

使用docker run命令拉取的鏡像,docker會自動為我們隨機命名,如果不想使用隨機命名的容器,可以使用–name參數來指定名稱,如果我們不指定鏡像的版本號,則會自動拉取最新版本的鏡像。

docker run命令還經常搭配-d參數:表示後臺運行守護式容器

也可以使用–restart=always參數用來重啟容器,當然,默認的是不重啟容器。

3、啟動、停止和重啟容器

需要註意,最後面跟的是容器的ID或者容器name

docker start containerID/name

docker stop containerID/name

docker restart containerID/name

4、查看容器內的進程

docker top命令可以查看某個容器內的所有進程。

[[email protected] ~]# docker top  k8s_mysql.16d54d16_mysql-pd7jr_default_0032bce0-2b0f-11eb-98ad-5254002dbd85_d775f414        
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                5059                5044                0                   2020                pts/5               00:00:00            /bin/bash
root                5152                5059                0                   2020                pts/5               00:00:00            mysql -uroot -px xxxx
root                13644               24879               0                   2020                pts/4               00:00:00            mysql -uroot -px xxxx
polkitd             18853               18837               0                   2020                ?                   01:31:43            mysqld
root                24153               24137               0                   2020                pts/1               00:00:00            /bin/bash
root                24376               24153               0                   2020                pts/1               00:00:00            mysql -uroot -px xxxx
root                24879               24864               0                   2020                pts/4               00:00:00            /bin/bash
root                25833               25817               0                   23:45               pts/8               00:00:00            /bin/bash
root                28493               28477               0                   2020                pts/3               00:00:00            /bin/bash
root                28609               28493               0                   2020                pts/3               00:00:00            mysql -uroot -px xxxx
root                29484               29468               0                   2020                pts/2               00:00:00            /bin/bash
root                29601               29484               0                   2020                pts/2               00:00:00            mysql -uroot -px xxxx

5、在容器內部執行命令

例如,我們在容器內部創建一個文件夾,其中,容器ID是4784586d01e0,創建data文件夾

[[email protected] ~]# docker exec -d 4784586d01e0 mkdir -p /data
[[email protected] ~]# docker exec -it 4784586d01e0 /bin/bash
[email protected]:/# ls -l
total 76
drwxr-xr-x   2 root root 4096 Nov 17 00:00 bin
drwxr-xr-x   2 root root 4096 Sep 19 21:39 boot
drwxr-xr-x   2 root root 4096 Jan  5 15:51 data
drwxr-xr-x   5 root root  360 Nov 20 09:16 dev
drwxr-xr-x   2 root root 4096 Nov 18 08:16 docker-entrypoint-initdb.d
.....

6、刪除容器

docker rm containID/name

註意,刪除容器之前,需要先將容器stop,否則刪除會報錯。

docker rm `docker -a -q`

該命令可以刪除所有的容器。

以上就是docker的一些基本指令的詳細內容,更多關於docker基本指令的資料請關註WalkonNet其它相關文章!