Devops(三):Docker經常使用命令

列出鏡像列表(docker images)

[root@master docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        7 months ago        1.84kB
[root@master docker]# 

各個指標項說明::
REPOSITORY:表示鏡像的倉庫源
TAG:鏡像的標籤
IMAGE ID:鏡像ID
CREATED:鏡像建立時間
SIZE:鏡像大小php

啓動鏡像(docker run)

Docker 容許你在容器內運行應用程序, 使用 docker run 命令來在容器內運行一個應用程序。html

語法mysql

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]linux

參數說明:nginx

-a stdin: 指定標準輸入輸出內容類型,可選 STDIN/STDOUT/STDERR 三項;
-d: 後臺運行容器,並返回容器ID;
-i: 以交互模式運行容器,一般與 -t 同時使用;
-P: (將容器內部使用的網絡端口映射到咱們使用的主機上)隨機端口映射,容器內部端口隨機映射到主機的高端口
-p: 指定端口映射,格式爲:主機(宿主)端口:容器端口
-t: 爲容器從新分配一個僞輸入終端,一般與 -i 同時使用;
--name="nginx-lb": 爲容器指定一個名稱;
--dns 8.8.8.8: 指定容器使用的DNS服務器,默認和宿主一致;
--dns-search example.com: 指定容器DNS搜索域名,默認和宿主一致;
-h "mars": 指定容器的hostname;
-e username="ritchie": 設置環境變量;
--env-file=[]: 從指定文件讀入環境變量;
--cpuset="0-2" or --cpuset="0,1,2": 綁定容器到指定CPU運行;
-m :設置容器使用內存最大值;
--net="bridge": 指定容器的網絡鏈接類型,支持 bridge/host/none/container: 四種類型;
--link=[]: 添加連接到另外一個容器;
--expose=[]: 開放一個端口或一組端口;
--volume , -v: 綁定一個卷web

實例sql

使用docker鏡像nginx:latest之後臺模式啓動一個容器,並將容器命名爲mynginx。docker

docker run --name mynginx -d nginx:latest

使用鏡像nginx:latest之後臺模式啓動一個容器,並將容器的80端口映射到主機隨機端口。shell

docker run -P -d nginx:latest

使用鏡像 nginx:latest,之後臺模式啓動一個容器,將容器的 80 端口映射到主機的 80 端口,主機的目錄 /data 映射到容器的 /data。apache

docker run -p 80:80 -v /data:/data -d nginx:latest

綁定容器的 8080 端口,並將其映射到本地主機 127.0.0.1 的 80 端口上。

docker run -p 127.0.0.1:80:8080/tcp ubuntu bash

使用鏡像nginx:latest以交互模式啓動一個容器,在容器內執行/bin/bash命令。

root@master docker]# docker run -it nginx:latest /bin/bash
root@b8573233d675:/#

使用鏡像centos 以交互模式啓動一個容器,在容器內執行/bin/bash命令。

root@master docker]# docker run --name mydocker -t -i centos /bin/bash
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
d8d02d457314: Pull complete 
Digest: sha256:307835c385f656ec2e2fec602cf093224173c51119bbebd602c53c3653a3d6eb
Status: Downloaded newer image for centos:latest
[root@1d8a5ab31830 /]# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.0  11820  1896 pts/0    Ss   15:00   0:00 /bin/bash
root         15  0.0  0.0  51740  1732 pts/0    R+   15:01   0:00 ps aux
[root@1d8a5ab31830 /]# 
[root@1d8a5ab31830 /]# exit
exit
[root@master docker]# 
使用鏡像 sath89/oracle-12c,之後臺模式啓動一個名稱爲「oracle12c」的容器,將容器的 1521端口映射到主機的 1521端口,主機的目錄 /usr/local/data_temp 映射到容器的/home/oracle/data_temp。
#建立一個文件目錄,用於掛載到容器內,作oracle數據備份時數據存放的位置,保證備份數據不丟失。
mkdir /usr/local/data_temp
chmod 777 /usr/local/data_temp
docker run -d --name oracle12c -p 1521:1521 -v /usr/local/data_temp:/home/oracle/data_temp sath89/oracle-12c

備註:oracle12c是名稱,-p是端口映射,-v是將宿主機的/usr/local/data_temp 目錄映射到容器內的 /home/oracle/data_temp內。

列出容器列表(docker ps )

語法
docker ps [OPTIONS]
OPTIONS說明:
-a :顯示全部的容器,包括未運行的。
-f :根據條件過濾顯示的內容。
--format :指定返回值的模板文件。
-l :顯示最近建立的容器。
-n :列出最近建立的n個容器。
--no-trunc :不截斷輸出。
-q :靜默模式,只顯示容器編號。
-s :顯示總的文件大小。

root@master docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
32098924b2e0        hello-world         "/hello"            6 minutes ago       Exited (0) 6 minutes ago hopeful_northcutt b07906c995f3 hello-world "/hello" 7 minutes ago Exited (0) 7 minutes ago keen_goldstine [root@master docker]# docker ps -a -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 32098924b2e0 hello-world "/hello" 8 minutes ago Exited (0) 8 minutes ago hopeful_northcutt

查詢最後一次建立的容器

[root@master docker]# docker run hello-world

Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/  For more examples and ideas, visit: https://docs.docker.com/get-started/  [root@master docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 99d7fbb94565 hello-world "/hello" 3 seconds ago Exited (0) 3 seconds ago gracious_panini 1d8a5ab31830 centos "/bin/bash" 34 minutes ago Exited (137) 4 minutes ago mydocker [root@master docker]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 99d7fbb94565 hello-world "/hello" 21 seconds ago Exited (0) 21 seconds ago gracious_panini [root@master docker]#

進入容器方法

1、attach(不建議使用)

注意:此方法當exit退出容器後,進程也就結束了,容器就關閉了。不建議使用

[root@master docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
1d8a5ab31830        centos              "/bin/bash"         4 minutes ago       Exited (0) 2 minutes ago                        mydocker
32098924b2e0        hello-world         "/hello"            16 minutes ago      Exited (0) 16 minutes ago                       hopeful_northcutt
b07906c995f3        hello-world         "/hello"            17 minutes ago      Exited (0) 17 minutes ago                       keen_goldstine
[root@master docker]# docker start 1d8a5ab31830  #再次將容器啓動
1d8a5ab31830
[root@master docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
1d8a5ab31830        centos              "/bin/bash"         5 minutes ago       Up 6 seconds                                    mydocker
32098924b2e0        hello-world         "/hello"            16 minutes ago      Exited (0) 16 minutes ago                       hopeful_northcutt
b07906c995f3        hello-world         "/hello"            18 minutes ago      Exited (0) 18 minutes ago                       keen_goldstine
[root@master docker]# docker attach 1d8a5ab31830
[root@1d8a5ab31830 /]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
[root@1d8a5ab31830 /]# exit
exit
[root@master docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
1d8a5ab31830        centos              "/bin/bash"         8 minutes ago       Exited (0) 3 seconds ago                        mydocker
32098924b2e0        hello-world         "/hello"            19 minutes ago      Exited (0) 19 minutes ago                       hopeful_northcutt
b07906c995f3        hello-world         "/hello"            20 minutes ago      Exited (0) 20 minutes ago                       keen_goldstine
[root@master docker]# 

attach總結:一個容器指定一個進程,這個容器退出了。他的生成周期也就結束了,也就退出了。

2、exec

注意:此方法當exit退出容器後,進程不結束,容器也不關閉。

docker exec -it 1d8a5ab31830 /bin/bash #其中 1d8a5ab31830 爲容器ID號
docker exec :在運行的容器中執行命令
OPTIONS說明:
-d :分離模式: 在後臺運行
-i :即便沒有附加也保持STDIN 打開
-t :分配一個僞終端

[root@master docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
1d8a5ab31830        centos              "/bin/bash"         8 minutes ago       Exited (0) 3 seconds ago                        mydocker
32098924b2e0        hello-world         "/hello"            19 minutes ago      Exited (0) 19 minutes ago                       hopeful_northcutt
b07906c995f3        hello-world         "/hello"            20 minutes ago      Exited (0) 20 minutes ago                       keen_goldstine
[root@master docker]# docker exec -it 1d8a5ab31830 /bin/bash
Error response from daemon: Container 1d8a5ab31830dc16e3129a6e123a5c16bc10989aeced01a3be62d236ee76cb0d is not running
[root@master docker]# docker start 1d8a5ab31830
1d8a5ab31830
[root@master docker]# docker exec -it 1d8a5ab31830 /bin/bash
[root@1d8a5ab31830 /]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
[root@1d8a5ab31830 /]# exit
exit
[root@master docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
1d8a5ab31830        centos              "/bin/bash"         10 minutes ago      Up 23 seconds                                   mydocker
32098924b2e0        hello-world         "/hello"            21 minutes ago      Exited (0) 21 minutes ago                       hopeful_northcutt
b07906c995f3        hello-world         "/hello"            23 minutes ago      Exited (0) 23 minutes ago                       keen_goldstine
[root@master docker]# 

3、nsenter(推薦使用這個命令進入容器)

注意:當退出容器後,還能讓容器再後臺運行。推薦使用這個命令進入容器。

[root@master docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
1d8a5ab31830        centos              "/bin/bash"         10 minutes ago      Up 23 seconds                                   mydocker
32098924b2e0        hello-world         "/hello"            21 minutes ago      Exited (0) 21 minutes ago                       hopeful_northcutt
b07906c995f3        hello-world         "/hello"            23 minutes ago      Exited (0) 23 minutes ago                       keen_goldstine
[root@master docker]# docker stop 1d8a5ab31830
1d8a5ab31830
[root@master docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
1d8a5ab31830        centos              "/bin/bash"         13 minutes ago      Exited (137) 4 seconds ago                       mydocker
32098924b2e0        hello-world         "/hello"            24 minutes ago      Exited (0) 24 minutes ago                        hopeful_northcutt
b07906c995f3        hello-world         "/hello"            26 minutes ago      Exited (0) 26 minutes ago                        keen_goldstine
[root@master docker]# docker start 1d8a5ab31830     #要先啓動容器,才能進入容器裏邊
1d8a5ab31830
[root@master docker]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
1d8a5ab31830        centos              "/bin/bash"         13 minutes ago      Up 5 seconds                            mydocker
[root@master docker]# docker inspect --format "{{.State.Pid}}" 1d8a5ab31830     #查看容器的進程ID
2841
[root@master docker]# nsenter -t 2841 -u -i -n     #進入容器
[root@1d8a5ab31830 docker]# 
[root@1d8a5ab31830 docker]# exit                   #退出容器,進程還在
logout
[root@master docker]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
1d8a5ab31830        centos              "/bin/bash"         15 minutes ago      Up About a minute                       mydocker
[root@master docker]# 

能夠寫一個腳本方便進容器:

[root@master docker]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
1d8a5ab31830        centos              "/bin/bash"         21 minutes ago      Up 7 minutes                            mydocker
[root@master docker]# echo '' > shelldocker.sh
[root@master docker]#
[root@master docker]# vi shelldocker.sh 
#!/bin/bash
PID=$(docker inspect --format "{{.State.Pid}}" $1)
nsenter -t $PID -u -i -n
~
"shelldocker.sh" 3L, 88C written
[root@master docker]#
[root@master docker]# ls
shelldocker.sh
[root@master docker]# chmod +x shelldocker.sh 
[root@master docker]#
[root@master docker]# ./shelldocker.sh 1d8a5ab31830
[root@1d8a5ab31830 docker]# exit
logout
[root@master docker]# 

查找鏡像(docker search)

使用 docker search 命令來從 Docker Hub 網站來搜索鏡像

docker search nginx(查找鏡像)

[root@master docker]# docker search nginx
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                             Official build of Nginx.                        11852               [OK]                
jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   1641                                    [OK]
richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   735                                     [OK]
linuxserver/nginx                 An Nginx container, brought to you by LinuxS…   73                                      
bitnami/nginx                     Bitnami nginx Docker Image                      70                                      [OK]
tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp…   51                                      [OK]
jc21/nginx-proxy-manager          Docker container for managing Nginx proxy ho…   23                                      
nginxdemos/hello                  NGINX webserver that serves a simple page co…   23                                      [OK]
nginx/nginx-ingress               NGINX Ingress Controller for Kubernetes         20                                      
jlesage/nginx-proxy-manager       Docker container for Nginx Proxy Manager        20                                      [OK]
schmunk42/nginx-redirect          A very simple container to redirect HTTP tra…   17                                      [OK]
crunchgeek/nginx-pagespeed        Nginx with PageSpeed + GEO IP + VTS + more_s…   13                                      
blacklabelops/nginx               Dockerized Nginx Reverse Proxy Server.          12                                      [OK]
centos/nginx-18-centos7           Platform for running nginx 1.8 or building n…   11                                      
centos/nginx-112-centos7          Platform for running nginx 1.12 or building …   10                                      
nginxinc/nginx-unprivileged       Unprivileged NGINX Dockerfiles                  9                                       
sophos/nginx-vts-exporter         Simple server that scrapes Nginx vts stats a…   5                                       [OK]
nginx/nginx-prometheus-exporter   NGINX Prometheus Exporter                       5                                       
1science/nginx                    Nginx Docker images that include Consul Temp…   5                                       [OK]
mailu/nginx                       Mailu nginx frontend                            3                                       [OK]
pebbletech/nginx-proxy            nginx-proxy sets up a container running ngin…   2                                       [OK]
travix/nginx                      NGinx reverse proxy                             2                                       [OK]
centos/nginx-110-centos7          Platform for running nginx 1.10 or building …   0                                       
ansibleplaybookbundle/nginx-apb   An APB to deploy NGINX                          0                                       [OK]
wodby/nginx                       Generic nginx                                   0                                       [OK]

拉取鏡像(docker pull )

docker pull nginx(拉取鏡像)

[root@master docker]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
1ab2bdfe9778: Pull complete 
a17e64cfe253: Pull complete 
e1288088c7a8: Pull complete 
Digest: sha256:53ddb41e46de3d63376579acf46f9a41a8d7de33645db47a486de9769201fec9
Status: Downloaded newer image for nginx:latest
[root@master docker]# 

從容器建立一個新的鏡像(docker commit )

語法
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
OPTIONS說明:
-a :提交的鏡像做者;
-c :使用Dockerfile指令來建立鏡像;
-m :提交時的說明文字;
-p :在commit時,將容器暫停。

實例

將容器a404c6c174a2 保存爲新的鏡像,並添加提交人信息和說明信息。

[root@master docker]# docker commit -a "runoob.com(提交的鏡像做者)" -m "my apache(提交是的說明文字)" a404c6c174a2  mymysql:v1 
sha256:37af1236adef1544e8886be23010b66577647a40bc02c0885a6600b33ee28057
[root@master docker]# docker images mymysql:v1
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mymysql             v1                  37af1236adef        15 seconds ago      329 MB

推送鏡像(docker push)

TODO....

暫時參考《經過容器提交鏡像(docker commit)以及推送鏡像(docker push)筆記

 

容器內部的標準輸出(docker logs)

docker logs [ID或者名字]    能夠查看容器內部的標準輸出

$ docker logs 353e6c82b2fc
$ docker logs -f 353e6c82b2fc
-f:讓 dokcer logs 像使用 tail -f 同樣來輸出容器內部的標準輸出。
docker logs [ID或者名字]
[root@master docker]# docker logs 1d8a5ab31830
[root@1d8a5ab31830 /]#  ps aux
bash: 平ps: command not found
[root@1d8a5ab31830 /]# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.0  11820  1896 pts/0    Ss   15:00   0:00 /bin/bash
root         15  0.0  0.0  51740  1732 pts/0    R+   15:01   0:00 ps aux
[root@1d8a5ab31830 /]# 
[root@1d8a5ab31830 /]# exit
exit
[root@1d8a5ab31830 /]# echo PATH
PATH
[root@1d8a5ab31830 /]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
[root@1d8a5ab31830 /]# exit
exit
[root@1d8a5ab31830 /]# [root@master docker]# 

docker logs -f [ID或者名字]

root@master docker]# docker logs -f 1d8a5ab31830
[root@1d8a5ab31830 /]#  ps aux
bash: 平ps: command not found
[root@1d8a5ab31830 /]# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.0  11820  1896 pts/0    Ss   15:00   0:00 /bin/bash
root         15  0.0  0.0  51740  1732 pts/0    R+   15:01   0:00 ps aux
[root@1d8a5ab31830 /]# 
[root@1d8a5ab31830 /]# exit
exit
[root@1d8a5ab31830 /]# echo PATH
PATH
[root@1d8a5ab31830 /]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
[root@1d8a5ab31830 /]# exit
exit
[root@1d8a5ab31830 /]# 

中止應用容器(docker stop)

$ docker stop [cid|names] (能夠使用cid和容器名)

[root@master docker]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
1d8a5ab31830        centos              "/bin/bash"         29 minutes ago      Up 15 minutes                           mydocker
[root@master docker]# docker stop mydocker
mydocker
[root@master docker]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@master docker]# 

 移除應用容器(docker rm)

$ docker rm [CID|NAMES] 能夠使用cid和容器名)

$ docker rm -f CID|NAMES

[root@master docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                            PORTS               NAMES
1d8a5ab31830        centos              "/bin/bash"         31 minutes ago      Exited (137) About a minute ago                       mydocker
32098924b2e0        hello-world         "/hello"            42 minutes ago      Exited (0) 42 minutes ago                             hopeful_northcutt
b07906c995f3        hello-world         "/hello"            44 minutes ago      Exited (0) 44 minutes ago                             keen_goldstine
[root@master docker]# docker rm hopeful_northcutt
hopeful_northcutt
[root@master docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                            PORTS               NAMES
1d8a5ab31830        centos              "/bin/bash"         31 minutes ago      Exited (137) About a minute ago                       mydocker
b07906c995f3        hello-world         "/hello"            44 minutes ago      Exited (0) 44 minutes ago                             keen_goldstine
[root@master docker]# docker rm b07906c995f3
b07906c995f3
[root@master docker]# docker ps -a          
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
1d8a5ab31830        centos              "/bin/bash"         32 minutes ago      Exited (137) 2 minutes ago                       mydocker
[root@master docker]# 

Docker刪除容器與鏡像(不建議使用,使用要注意)

中止全部的container,這樣纔可以刪除其中的images:

docker stop $(docker ps -a -q)
docker kill $(docker ps -a -q)

若是想要刪除全部container的話再加一個指令:

docker rm $(docker ps -a -q)

刪除images,經過image的id來指定刪除誰

docker rmi <image id>

想要刪除untagged images,也就是那些id爲的image的話能夠用

docker rmi $(docker images | grep "^<none>" | awk "{print $3}")

要刪除所有image的話

docker rmi $(docker images -q)

強制刪除所有image的話

docker rmi -f $(docker images -q)

 

參考:《二、CentOS7 Docker 安裝以及基本使用,連接:https://blog.csdn.net/weixin_41515615/article/details/81349688》

相關文章
相關標籤/搜索