社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  docker

Docker 命令基础及进阶

Nice_N • 4 年前 • 160 次点击  

帮助命令

docker version :查看Docker 的版本信息
docker info:查看Docker 的具体详细信息

[root@bigdata01 ~]# docker version
Client: Docker Engine - Community
 Version:           19.03.12
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        48a66213fe
 Built:             Mon Jun 22 15:46:54 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.12
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.10
  Git commit:       48a66213fe
  Built:            Mon Jun 22 15:45:28 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

镜像命令

docker images

docker images:列出本地主机上的镜像

[root@bigdata01 ~]# docker images --help

Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]

List images

Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print images using a Go template
      --no-trunc        Don't truncate output
  -q, --quiet           Only show numeric IDs
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

OPTIONS说明:

  • -a :列出本地所有的镜像(含中间映像层)
  • -q :只显示镜像ID。
  • –digests :显示镜像的摘要信息
  • –no-trunc :显示完整的镜像信息
  • -qa:列出本地所有镜像只显示镜像ID

docker search

含义:从https://hub.docker.com找寻镜像,真正拉取的时候从配置的镜像仓库拉取
使用: docker search [OPTIONS] 镜像名字

OPTIONS说明:

  • –no-trunc : 显示完整的镜像描述
  • -s : 列出收藏数不小于指定值的镜像。
  • –automated : 只列出 automated build类型的镜像;

docker pull

docker pull 某个XXX镜像名字
docker pull 镜像名字[:TAG]


docker rmi

docker rmi 某个XXX镜像名字ID
删除单个:docker rmi -f 镜像ID
删除多个:docker rmi -f 镜像名1:TAG 镜像名2:TAG
删除全部:docker rmi -f $(docker images -qa)


容器命令

docker pull

有镜像才能创建容器,这是根本前提(下载一个CentOS镜像演示)
docker pull centos

docker run

新建并启动容器:docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

 OPTIONS说明(常用):有些是一个减号,有些是两个减号
 
--name="容器新名字":


    
 为容器指定一个名称;
-d: 后台运行容器,并返回容器ID,也即启动守护式容器;
-i:以交互模式运行容器,通常与 -t 同时使用;
-t:为容器重新分配一个伪输入终端,通常与 -i 同时使用;
-P: 随机端口映射;
-p: 指定端口映射,有以下四种格式
      ip:hostPort:containerPort
      ip::containerPort
      hostPort:containerPort
      containerPort
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
#使用镜像centos:latest以交互模式启动一个容器,在容器内执行/bin/bash命令。
docker run -it centos /bin/bash  
  • 1
  • 2

创建并运行一个容器,并且进入该容器里面

[root@bigdata01 ~]# docker run -it centos
[root@1e4dff5a3236 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@1e4dff5a3236 /]#
  • 1
  • 2
  • 3
  • 4

docker ps

docker ps [OPTIONS]:列出当前所有正在运行的容器

OPTIONS说明(常用):
 
-a :列出当前所有正在运行的容器+历史上运行过的
-l :显示最近创建的容器。
-n:显示最近n个创建的容器。
-q :静默模式,只显示容器编号。
--no-trunc :不截断输出。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

退出容器:

  • exit 容器停止退出
  • ctrl+P+Q 容器不停止退出

启动容器:docker start 容器ID或者容器名

重启容器:docker restart 容器ID或者容器名

停止容器:docker stop 容器ID或者容器名

强制停止容器:docker kill 容器ID或者容器名

删除已停止的容器:docker rm 容器ID

一次性删除多个容器: docker rm -f $(docker ps -a -q)


启动守护式容器

启动守护式容器 docker run -d 容器名

使用镜像centos:latest以后台模式启动一个容器

docker run -d centos
  • 1

问题 :然后docker ps -a 进行查看, 会发现容器已经退出,很重要的要说明的一点: Docker容器后台运行,就必须有一个前台进程.容器运行的命令如果不是那些一直挂起的命令(比如运行top,tail),就是会自动退出的。

这个是docker的机制问题,比如你的web容器,我们以nginx为例,正常情况下,我们配置启动服务只需要启动响应的service即可。例如 service nginx start 但是,这样做,nginx为后台进程模式运行,就导致docker前台没有运行的应用,这样的容器后台启动后,会立即自杀因为他觉得他没事可做了.所以, 最佳的解决方案是,将你要运行的程序以前台进程的形式运行


查看容器日志

docker logs -f -t --tail 容器ID:查看容器日志

Optons:

  • -t 是加入时间戳
  • -f 跟随最新的日志打印
  • –tail 数字 显示最后多少条

docker top 容器ID :查看容器内运行的进程

[root@bigdata01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
1e4dff5a3236        centos              "/bin/bash"         7 minutes ago       Up 7 minutes                            silly_euclid
[root@bigdata01 ~]# docker top 1e4dff5a3236
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                5614                5598                0                   15:34               pts/0               00:00:00            /bin/bash
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

docker inspect 容器ID:查看容器内部细节(以Json串的形式显示出来)

[root@bigdata01 ~]# docker inspect 1e4dff5a3236
[
    {
        "Id": "1e4dff5a323662d920befbcc8fd4971ce071931cb760ac1ef3ab854ee259aec3",
        "Created": "2020-08-09T07:34:25.94095475Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 5614,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-08-09T07:34:26.245232765Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        。。。。。。。。。。。。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

进入正在运行的容器并以命令行交互

docker exec -it 容器ID bashShell

重新进入docker attach 容器ID

[root@bigdata01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
1e4dff5a3236        centos              "/bin/bash"         12 minutes ago      Up 12 minutes                           silly_euclid
[root@bigdata01 ~]# docker exec -it 1e4dff5a3236 /bin/bash
[root@1e4dff5a3236 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@1e4dff5a3236 /]# exit
exit
[root@bigdata01 ~]# docker attach 1e4dff5a3236
[root@1e4dff5a3236 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

上述两个区别:

  • attach 直接进入容器启动命令的终端,不会启动新的进程
  • exec 是在容器中打开新的终端,并且可以启动新的进程

从容器内拷贝文件到主机上:docker cp 容器ID:容器内路径 目的主机路径

[root@bigdata01 ~]# docker exec -it 85b4cb80e68e /bin/bash
[root@85b4cb80e68e /]# cd /tmp/
[root@85b4cb80e68e tmp]# ls -al
total 8
drwxrwxrwt 7 root root  145 Jun 11 02:35 .
drwxr-xr-x 1 root root   18 Aug  9 07:48 ..
drwxrwxrwt 2 root root    6 Jun 11 02:35 .ICE-unix
drwxrwxrwt 2 root root    6 Jun 11 02:35 .Test-unix
drwxrwxrwt 2 root root    6 Jun 11 02:35 .X11-unix
drwxrwxrwt 2 root root    6 Jun 11 02:35 .XIM-unix
drwxrwxrwt 2 root root    6 Jun 11 02:35 .font-unix
-rwx------ 1 root root 1379 Jun 11 02:35 ks-script-585nin8f
-rwx------ 1 root root  671 Jun 11 02:35 ks-script-z6zw_bhq
[root@85b4cb80e68e tmp]# exit
exit
[root@bigdata01 ~]# docker cp 85b4cb80e68e:/tmp/ks-script-585nin8f /root
[root@bigdata01 ~]# cd /root/
[root@bigdata01 ~]# ls
app  change.sh  Could  data  ks-script-585nin8f  lib  logs  script  shell  software  sourcecode  tmp
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

Docker镜像

UnionFS

UnionFS(联合文件系统):Union文件系统(UnionFS)是一种分层、轻量级并且高性能的文件系统,它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下(unite several directories into a single virtual filesystem)。Union 文件系统是 Docker 镜像的基础。镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。

特性:一次同时加载多个文件系统,但从外面看起来,只能看到一个文件系统,联合加载会把各层文件系统叠加起来,这样最终的文件系统会包含所有底层的文件和目录
在这里插入图片描述

Docker镜像加载原理:

docker的镜像实际上由一层一层的文件系统组成,这种层级的文件系统UnionFS。
bootfs(boot file system)主要包含bootloader和kernel, bootloader主要是引导加载kernel, Linux刚启动时会加载bootfs文件系统,在Docker镜像的最底层是bootfs。这一层与我们典型的Linux/Unix系统是一样的,包含boot加载器和内核。当boot加载完成之后整个内核就都在内存中了,此时内存的使用权已由bootfs转交给内核,此时系统也会卸载bootfs。

rootfs (root file system) ,在bootfs之上。包含的就是典型 Linux 系统中的 /dev, /proc, /bin, /etc 等标准目录和文件。rootfs就是各种不同的操作系统发行版,比如Ubuntu,Centos等等。
在这里插入图片描述

平时我们安装进虚拟机的CentOS都是好几个G,为什么docker这里才200M??

对于一个精简的OS,rootfs可以很小,只需要包括最基本的命令、工具和程序库就可以了,因为底层直接用Host的kernel,自己只需要提供 rootfs 就行了。由此可见对于不同的linux发行版, bootfs基本是一致的, rootfs会有差别, 因此不同的发行版可以公用bootfs。

分层镜像:以我们的pull为例,在下载的过程中我们可以看到docker的镜像好像是在一层一层的在下载

特点 :Docker镜像都是只读的,当容器启动时,一个新的可写层被加载到镜像的顶部。这一层通常被称作“容器层”,“容器层”之下的都叫“镜像层”。


Docker镜像commit操作

docker commit提交容器副本使之成为一个新的镜像
语法:docker commit -m=“提交的描述信息” -a=“作者” 容器ID 要创建的目标镜像名:[标签名]
docker run -it -p 8080:8080 tomcat

Option:

  • -p 主机端口:docker容器端口(小写p)
  • -P 随机分配端口(大写P)
  • i:交互
  • t:终端

故意删除上一步镜像生产tomcat容器的文档

启动我们的新镜像并和原来的对比

# 查看docker进程
[root@bigdata01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
af1a3dbdb0bd        tomcat              "catalina.sh run"   36 minutes ago      Up 36 minutes       0.0.0.0:8088->8080/tcp   cranky_meninsky
  • 1
  • 2
  • 3
  • 4
# 进入docker交互式命令行
[


    
root@bigdata01 ~]# docker exec -it af1a3dbdb0bd /bin/bash
root@af1a3dbdb0bd:/usr/local/tomcat# ls
BUILDING.txt     LICENSE  README.md      RUNNING.txt  conf  logs            temp     webapps.dist
CONTRIBUTING.md  NOTICE   RELEASE-NOTES  bin          lib   native-jni-lib  webapps  work

root@af1a3dbdb0bd:/usr/local/tomcat# cd webapps
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
# 移动docs文件夹
root@af1a3dbdb0bd:/usr/local/tomcat/webapps# mv docs/ docs.bak
[root@bigdata01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
af1a3dbdb0bd        tomcat              "catalina.sh run"   41 minutes ago      Up 41 minutes       0.0.0.0:8088->8080/tcp   cranky_meninsky
  • 1
  • 2
  • 3
  • 4
  • 5
# 提交操作
[root@bigdata01 ~]# docker commit -a "nice" -m "mytomcat without docs" af1a3dbdb0bd nice/mytomcat
sha256:f183c09f98b7edcaa041394ae9348a4c9d6dcfcf3221c35769213324e1e7cbbf
  • 1
  • 2
  • 3
# 查看镜像列表
[root@bigdata01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nice/mytomcat       latest              f183c09f98b7        4 seconds ago       652MB
tomcat              latest              2ae23eb477aa        3 days ago          647MB
nginx               latest              08393e824c32        4 days ago          132MB
centos              latest              831691599b88        7 weeks ago         215MB
hello-world         latest              bf756fb1ae65        7 months ago        13.3kB
[root@bigdata01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
af1a3dbdb0bd        tomcat              "catalina.sh run"   42 minutes ago      Up 42 minutes       0.0.0.0:8088->8080/tcp   cranky_meninsky
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
# 停止原来的tomcat
[root@bigdata01 ~]# docker stop af1a3dbdb0bd

af1a3dbdb0bd
  • 1
  • 2
  • 3
  • 4
# 查看镜像
[root@bigdata01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nice/mytomcat       latest              f183c09f98b7        58 seconds ago      652MB
tomcat              latest              2ae23eb477aa        3 days ago          647MB
nginx               latest              08393e824c32        4 days ago          132MB
centos              latest              831691599b88        7 weeks ago         215MB
hello-world         latest              bf756fb1ae65        7 months ago        13.3kB
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
# 运行新的镜像
[root@bigdata01 ~]# docker run -it -p 8080:8080 f183c09f98b7
  • 1
  • 2

Docker常用命令汇总

Command 释义 含义
attach Attach to a running container 当前 shell 下 attach 连接指定运行镜像
build Build an image from a Dockerfile 通过 Dockerfile 定制镜像
commit Create a new image from a container changes 提交当前容器为新的镜像
cp Copy files/folders from the containers filesystem to the host path 从容器中拷贝指定文件或者目录到宿主机中
create Create a new container 创建一个新的容器,同 run,但不启动容器
diff Inspect changes on a container’s filesystem 查看 docker 容器变化
events Get real time events from the server 从 docker 服务获取容器实时事件
exec Run a command in an existing container 在已存在的容器上运行命令
export Stream the contents of a container as a tar archive 导出容器的内容流作为一个 tar 归档文件[对应 import ]
history Show the history of an image 展示一个镜像形成历史
images List images 列出系统当前镜像
import Create a new filesystem image from the contents of a tarball 从tar包中的内容创建一个新的文件系统映像[对应export]
info Display system-wide information 显示系统相关信息
inspect Return low-level information on a container 查看容器详细信息
kill Kill a running container kill 指定 docker 容器
load Load an image from a tar archive 从一个 tar 包中加载一个镜像[对应 save]
login Register or Login to the docker registry server 注册或者登陆一个 docker 源服务器
logout Log out from a Docker registry server 从当前 Docker registry 退出
logs Fetch the logs of a container 输出当前容器日志信息
port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT 查看映射端口对应的容器内部源端口
pause Pause all processes within a container 暂停容器
ps List containers 列出容器列表
pull Pull an image or a repository from the docker registry server 从docker镜像源服务器拉取指定镜像或者库镜像
push Push an image or a repository to the docker registry server 推送指定镜像或者库镜像至docker源服务器
restart Restart a running container 重启运行的容器
rm Remove one or more containers 移除一个或者多个容器
rmi Remove one or more images 移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]
run Run a command in a new container 创建一个新的容器并运行一个命令
save Save an image to a tar archive 保存一个镜像为一个 tar 包[对应 load]
search Search for an image on the Docker Hub 在 docker hub 中搜索镜像
start Start a stopped containers 启动容器
stop Stop a running containers 停止容器
tag Tag an image into a repository 给源中镜像打标签
top Lookup the running processes of a container 查看容器中运行的进程信息
unpause Unpause a paused container 取消暂停容器
version Show the docker version information 查看 docker 版本号
wait Block until a container stops, then print its exit code 截取容器停止时的退出状态值
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/72345
 
160 次点击