文章目录

  • 安装docker
    • 阿里云镜像加速
    • 回顾HelloWorld流程
    • 底层原理
  • Docker的常用命令
    • 帮助命令
    • 镜像命令
      • docker images 查看镜像
      • docker search 搜索镜像
      • docker pull 下载镜像
      • docker rmi 删除镜像
    • 容器命令
      • docker run 新建容器并启动
      • docker ps 列出所有正在运行的容器
      • 退出容器
      • 删除容器
      • 启动和停止容器
      • 常用其他命令
        • docker run -d 后台启动容器
        • docker logs 查看日志
        • docker top 查看容器中的进程信息
        • docker inspect 查看镜像的元数据
        • docker exec、attach进入当前正在运行的容器
        • docker cp 从容器拷贝文件到主机上
    • 小结

安装docker

环境准备

  1. 需要会一点点的Linux基础
  2. CentOS 7(192.168.119.128 root root)
  3. 使用MobaXterm远程连接服务器进行操作

环境查看

# 系统内核为 3.10 以上的
[root@master ~]# uname -r
3.10.0-327.el7.x86_64
#系统版本
[root@master ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

安装

https://docs.docker.com/ docker 帮助文档

#1、卸载旧版本
$ sudo yum remove docker \docker-client \docker-client-latest \docker-common \docker-latest \docker-latest-logrotate \docker-logrotate \docker-engine#2、需要的安装包
$ sudo yum install -y yum-utils
#3、设置镜像的仓库
$ sudo yum-config-manager \--add-repo \http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo#此处使用了阿里云镜像地址#更新软件包索引 yum
yum makecache fast#4、安装docker的相关的依赖   docker-ce社区版 docker-ee企业版
#docker-ce-cli containerd.io
$ sudo yum install docker-ce #5、启动docker
$ sudo systemctl start docker#6、检测是否安装成功
$docker version#7、hello-world
$ sudo docker run hello-worldHello 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 theexecutable that produces the output you are currently reading.4. The Docker daemon streamed that output to the Docker client, which sent itto your terminal.To try something more ambitious, you can run an Ubuntu container with:$ docker run -it ubuntu bashShare 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/#8、查看下载的hello-world镜像
[root@master ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    bf756fb1ae65   12 months ago   13.3kB# /var/lib/docker docker的默认工作路径

阿里云镜像加速

  1. 登录阿里云 找到容器服务

  1. 找到镜像地址

  1. 配置使用
$sudo mkdir -p /etc/docker
$sudo tee /etc/docker/daemon.json <<-'EOF'
{"registry-mirrors": ["https://********.mirror.aliyuncs.com"]
}
EOF
$sudo systemctl daemon-reload
$sudo systemctl restart docker

回顾HelloWorld流程

run流程

底层原理

Docker是怎么工作的?

Docker是一个Client + Server 结构的系统,Docker的守护进程运行在主机上,通过Socket从客户端访问

DockerServer接收到DockerClient的指令,就会执行这个命令

Docker为什么比VM快?

  1. Docker有着比虚拟机更少的抽象层
  2. docker利用的是宿主机的内核,VM需要Guest OS

所以,新建一个容器的时候,dockers不需要向虚拟机一样重新加载一个内核操作系统,避免引导。虚拟机是加载Guest OS,而docker是利用宿主机的操作系统,省略了这个复杂的过程,秒级启动。

Docker的常用命令

帮助命令

$docker version      #docker版本信息
$docker info            #显示docker的系统信息 ,包括镜像和容器的数量
$docker 命令 --help   #帮助命令

帮助命令地址:

https://docs.docker.com/reference/

镜像命令

docker images 查看镜像

$docker images[root@master ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    bf756fb1ae65   12 months ago   13.3kB#解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATED 镜像的创建时间
SIZE 镜像大小#可选项-a, --all             Show all images (default hides intermediate images)-q, --quiet           Only show image IDs[root@master ~]# docker images -a
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    bf756fb1ae65   12 months ago   13.3kB
[root@master ~]# docker images -q
bf756fb1ae65
[root@master ~]# docker images -aq
bf756fb1ae65

docker search 搜索镜像

[root@master ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   10380     [OK]
mariadb                           MariaDB is a community-developed fork of MyS…   3848      [OK]
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   758       [OK]
percona                           Percona Server is a fork of the MySQL relati…   519       [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   87#可选项-f, --filter filter   Filter output based on conditions provided--format string   Pretty-print search using a Go template--limit int       Max number of search results (default 25)--no-trunc        Don't truncate output
#通过收藏来过滤搜索结果
[root@master ~]# docker search mysql --filter=STARS=3000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   10380     [OK]
mariadb   MariaDB is a community-developed fork of MyS…   3848      [OK]

docker pull 下载镜像

#下载镜像docker pull 镜像名[:tag]
[root@master ~]# docker pull mysql
Using default tag: latest  #如果不写tag,默认是latest
latest: Pulling from library/mysql
a076a628af6f: Pull complete  #分层下载,docker镜像的核心 联合文件系统
f6c208f3f991: Pull complete
88a9455a9165: Pull complete
406c9b8427c6: Pull complete
7c88599c0b25: Pull complete
25b5c6debdaf: Pull complete
43a5816f1617: Pull complete
69dd1fbf9190: Pull complete
5346a60dcee8: Pull complete
ef28da371fc9: Pull complete
fd04d935b852: Pull complete
050c49742ea2: Pull complete
Digest: sha256:0fd2898dc1c946b34dceaccc3b80d38b1049285c1dab70df7480de62265d6213  #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest  #真实地址#以下两条命令等价
$docker pull mysql
$docker pull docker.io/library/mysql:latest#指定版本下载
[root@master ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a076a628af6f: Already exists    #分层下载的应用,共用前版本,不需要重复下载,极大节省存储
f6c208f3f991: Already exists
88a9455a9165: Already exists
406c9b8427c6: Already exists
7c88599c0b25: Already exists
25b5c6debdaf: Already exists
43a5816f1617: Already exists
7065aaa2655f: Pull complete
b4bc531db40f: Pull complete
8c3e9d7c9815: Pull complete
fadfb9734ed2: Pull complete
Digest: sha256:e08834258fcc0efd01df358222333919df53d4a0d9b2a54da05b204b822e3b7b
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7#可选项-a, --all-tags                Download all tagged images in the repository--disable-content-trust   Skip image verification (default true)--platform string         Set platform if server is multi-platformcapable-q, --quiet                   Suppress verbose output
[root@master ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
mysql         5.7       cc8775c0fe94   3 days ago      448MB
mysql         latest    d4c3cafb11d5   3 days ago      545MB
hello-world   latest    bf756fb1ae65   12 months ago   13.3kB

docker rmi 删除镜像

[root@master ~]# docker rmi -f cc8775c0fe94  #删除指定的容器
Untagged: mysql:5.7
Untagged: mysql@sha256:e08834258fcc0efd01df358222333919df53d4a0d9b2a54da05b204b822e3b7b
Deleted: sha256:cc8775c0fe94cd6cbfc494688d09b078cda9bced9d9324d5c28bddf032344fc3
Deleted: sha256:3fc592260569f4811f8155b391da8f7788f364727fbfa1c2da9ef6e75601e7de
Deleted: sha256:adf7b6a239f88cb28c37839e5bf271a2df1dbb0ac2c08dd66d4a97ea4146f0bf
Deleted: sha256:d8e36753bf50a2d59568535a6dca412e0f82da2a556c9c74c2aefb4c92e08edb
Deleted: sha256:ad833abb146f60599d779676bf9def39f3826982d124499d120657c63c85bcae[root@master ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
mysql         latest    d4c3cafb11d5   3 days ago      545MB
hello-world   latest    bf756fb1ae65   12 months ago   13.3kB#删除总结
[root@master ~]# docker rmi -f 镜像ID  #删除指定的镜像
[root@master ~]# docker rmi -f 镜像ID 镜像ID 镜像ID #删除多个指定的镜像
[root@master ~]# docker rmi -f $(docker images -aq)  #递归删除所有的镜像

容器命令

说明:有了镜像才可以创建容器,Linux,下载一个centos镜像来测试学习

$docker pull centos

docker run 新建容器并启动

$docker run [可选参数] image#参数说明
--name="Name"    容器名字 tomcat01 tomcat02 用于区分容器
-d              后台方式运行
-i              Keep STDIN open even if not attached
-t              Allocate a pseudo-TTY
-it             使用交互方式运行,进入容器查看内容
-p              指定容器的端口  -p 8080:8080-p ip:主机端口:容器端口-p 主机端口:容器端口(常用)-p 容器端口容器端口
-P              随机指定端口
#测试,启动并进入容器
[root@master ~]# docker run -it centos /bin/bash
[root@0869e06a8954 /]#[root@0869e06a8954 /]# ls  #查看容器内的centos,基础版本,很多功能不完善
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr[root@0869e06a8954 /]# exit
exit
[root@master ~]#

docker ps 列出所有正在运行的容器

[root@master ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES[root@master ~]# docker ps -a (包括曾经启动过的容器)
CONTAINER ID   IMAGE         COMMAND       CREATED          STATUS                     PORTS     NAMES
0869e06a8954   centos        "/bin/bash"   10 minutes ago   Exited (0) 5 minutes ago             stupefied_sammet
e5c6892d58d2   hello-world   "/hello"      2 hours ago      Exited (0) 2 hours ago               sharp_hodgkin
40ba6a314d70   hello-world   "/hello"      47 hours ago     Exited (0) 47 hours ago              cool_goldwasser
8a42df72e11a   hello-world   "/hello"      47 hours ago     Exited (0) 47 hours ago              modest_galileo[root@master ~]# docker ps -aq
0869e06a8954
e5c6892d58d2
40ba6a314d70
8a42df72e11a#可选项
-a  #列出正在运行的程序 + 带出历史运行过的容器 (即所有已创建的容器)
-n=? #显示最近创建的容器 n为显示容器的数量
-q  #只显示容器的编号

退出容器

exit #直接容器停止并退出
Ctrl + P + Q  #容器不停止退出(转后台运行)[root@master ~]# docker run -it centos /bin/bash
[root@598fc7362e56 /]#                 #Ctrl + P + Q退出
[root@master ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
598fc7362e56   centos    "/bin/bash"   37 seconds ago   Up 35 seconds             optimistic_raman

删除容器

docker rm 容器ID                   #删除指定容器
docker rm -f $(docker ps -aq)    #递归删除所有容器
docker ps -a -q|xargs docker rm  #通过管道递归删除所有容器[root@master ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED          STATUS                      PORTS     NAMES
598fc7362e56   centos        "/bin/bash"   5 minutes ago    Up 5 minutes                          optimistic_raman
0869e06a8954   centos        "/bin/bash"   22 minutes ago   Exited (0) 16 minutes ago             stupefied_sammet
e5c6892d58d2   hello-world   "/hello"      2 hours ago      Exited (0) 2 hours ago                sharp_hodgkin
40ba6a314d70   hello-world   "/hello"      47 hours ago     Exited (0) 47 hours ago               cool_goldwasser
8a42df72e11a   hello-world   "/hello"      47 hours ago     Exited (0) 47 hours ago               modest_galileo[root@master ~]# docker rm e5c6892d58d2
e5c6892d58d2
[root@master ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED          STATUS                      PORTS     NAMES
598fc7362e56   centos        "/bin/bash"   5 minutes ago    Up 5 minutes                          optimistic_raman
0869e06a8954   centos        "/bin/bash"   22 minutes ago   Exited (0) 17 minutes ago             stupefied_sammet
40ba6a314d70   hello-world   "/hello"      47 hours ago     Exited (0) 47 hours ago               cool_goldwasser
8a42df72e11a   hello-world   "/hello"      47 hours ago     Exited (0) 47 hours ago               modest_galileo[root@master ~]# docker rm 598fc7362e56              #不能删除正在运行的容器
Error response from daemon: You cannot remove a running container 598fc7362e56c1052730ecfc4e8d71bd1b18c9c668bc44eb3f73dc3fc0e08e2e. Stop the container before attempting removal or force remove[root@master ~]# docker rm -f $(docker ps -aq)
598fc7362e56
0869e06a8954
40ba6a314d70
8a42df72e11a
[root@master ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES#可选项
-r   递归删除
-f   强制删除

启动和停止容器

docker start 容器ID            #启动已有的容器
docker restart 容器ID         #重启容器
docker stop 容器ID            #停止当前正在运行的容器
docker kill 容器ID            #强制停止当前容器[root@master ~]# docker run -it centos /bin/bash
[root@90c377f8d1ce /]# exit
exit
[root@master ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
90c377f8d1ce   centos    "/bin/bash"   8 seconds ago   Exited (0) 4 seconds ago             adoring_shockley[root@master ~]# docker start 90c377f8d1ce
90c377f8d1ce
[root@master ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS         PORTS     NAMES
90c377f8d1ce   centos    "/bin/bash"   32 seconds ago   Up 5 seconds             adoring_shockley[root@master ~]# docker stop 90c377f8d1ce
90c377f8d1ce
[root@master ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMESeconds ago             adoring_shockley[root@master ~]# docker start 90c377f8d1ce
90c377f8d1ce
[root@master ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS         PORTS     NAMES
90c377f8d1ce   centos    "/bin/bash"   32 seconds ago   Up 5 seconds             adoring_shockley[root@master ~]# docker stop 90c377f8d1ce
90c377f8d1ce
[root@master ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

常用其他命令

docker run -d 后台启动容器

[root@master ~]# docker run -d centos
3bd3aebee8d2b4023d3bda08d5bad12bed4fca82ebf2389d9da41ee132e04ab3
[root@master ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@master ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
3bd3aebee8d2   centos    "/bin/bash"   21 seconds ago   Exited (0) 19 seconds ago             festive_beaver
[root@master ~]##问题:docker ps,发现centos停止了#docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
#容器启动后,发现自己没有提供服务,就会立即停止,没有程序了

docker logs 查看日志

docker logs#可选项--details        Show extra details provided to logs-f, --follow         Follow log output 持续输出--since string   Show logs since timestamp (e.g.2013-01-02T13:23:37Z) or relative (e.g. 42m for 42minutes)-n, --tail string    Number of lines to show from the end of the logs(default "all")-t, --timestamps     Show timestamps--until string   Show logs before a timestamp (e.g.2013-01-02T13:23:37Z) or relative (e.g. 42m for 42minutes)[root@master ~]# docker logs -f -t --tail 10 2c3086af94fc
[root@master ~]##编写一段shell脚本
“while true;do echo testing;sleep 1;done”[root@master ~]# docker run -d centos /bin/sh -c "while true;do echo testing;sleep 1;done"
2ddb980c237bc987db5680fa9c692189fc10e555b451b5712176b873615995b3
#显示日志
-tf  #显示全部日志 并持续输出
-tf --tail number   #要显示的日志条数
[root@master ~]# docker logs -f -t --tail 10 2ddb980c237bc987db5680fa9c692189
2021-01-15T16:37:10.272894130Z testing
2021-01-15T16:37:11.279082885Z testing
2021-01-15T16:37:12.285540587Z testing
2021-01-15T16:37:13.290871360Z testing
2021-01-15T16:37:14.297086627Z testing
2021-01-15T16:37:15.302075135Z testing
2021-01-15T16:37:16.307802095Z testing
2021-01-15T16:37:17.313870344Z testing
2021-01-15T16:37:18.319958686Z testing
2021-01-15T16:37:19.325826679Z testing
2021-01-15T16:37:20.332865787Z testing
2021-01-15T16:37:21.339446458Z testing
#结果:先显示10行日志,然后每一秒输出一个日志
[root@master ~]# docker logs -t --tail 10 2ddb980c237bc987db5680fa9c692189
2021-01-15T16:41:35.834579140Z testing
2021-01-15T16:41:36.839761013Z testing
2021-01-15T16:41:37.845704491Z testing
2021-01-15T16:41:38.851319286Z testing
2021-01-15T16:41:39.857509890Z testing
2021-01-15T16:41:40.863800579Z testing
2021-01-15T16:41:41.869076335Z testing
2021-01-15T16:41:42.874575034Z testing
2021-01-15T16:41:43.881279266Z testing
2021-01-15T16:41:44.887357640Z testing
#结果:只输出十条日志

docker top 查看容器中的进程信息

#docker top 容器id
[root@master ~]# docker top 2ddb980c237b
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                14404               14383               0                   00:37               ?                   00:00:00            /bin/sh -c while true;do echo testing;sleep 1;done
root                15309               14404               0                   00:44               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-95XzVTOa-1610810606025)(C:\Users\chen\AppData\Roaming\Typora\typora-user-images\image-20210116220538297.png)]

docker inspect 查看镜像的元数据

[root@master ~]# docker inspect --helpUsage:  docker inspect [OPTIONS] NAME|ID [NAME|ID...]Return low-level information on Docker objectsOptions:-f, --format string   Format the output using the given Go template-s, --size            Display total file sizes if the type is container--type string     Return JSON for specified type[root@master ~]# docker inspect 2ddb980c2
[{"Id": "2ddb980c237bc987db5680fa9c692189fc10e555b451b5712176b873615995b3","Created": "2021-01-15T16:37:00.955116375Z","Path": "/bin/sh","Args": ["-c","while true;do echo testing;sleep 1;done"],"State": {"Status": "running","Running": true,"Paused": false,"Restarting": false,"OOMKilled": false,"Dead": false,"Pid": 14404,"ExitCode": 0,"Error": "","StartedAt": "2021-01-15T16:37:02.219072858Z","FinishedAt": "0001-01-01T00:00:00Z"},"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55","ResolvConfPath": "/var/lib/docker/containers/2ddb980c237bc987db5680fa9c692189fc10e555b451b5712176b873615995b3/resolv.conf","HostnamePath": "/var/lib/docker/containers/2ddb980c237bc987db5680fa9c692189fc10e555b451b5712176b873615995b3/hostname","HostsPath": "/var/lib/docker/containers/2ddb980c237bc987db5680fa9c692189fc10e555b451b5712176b873615995b3/hosts","LogPath": "/var/lib/docker/containers/2ddb980c237bc987db5680fa9c692189fc10e555b451b5712176b873615995b3/2ddb980c237bc987db5680fa9c692189fc10e555b451b5712176b873615995b3-json.log","Name": "/bold_curran","RestartCount": 0,"Driver": "devicemapper","Platform": "linux","MountLabel": "","ProcessLabel": "","AppArmorProfile": "","ExecIDs": null,"HostConfig": {"Binds": null,"ContainerIDFile": "","LogConfig": {"Type": "json-file","Config": {}},"NetworkMode": "default","PortBindings": {},"RestartPolicy": {"Name": "no","MaximumRetryCount": 0},"AutoRemove": false,"VolumeDriver": "","VolumesFrom": null,"CapAdd": null,"CapDrop": null,"CgroupnsMode": "host","Dns": [],"DnsOptions": [],"DnsSearch": [],"ExtraHosts": null,"GroupAdd": null,"IpcMode": "private","Cgroup": "","Links": null,"OomScoreAdj": 0,"PidMode": "","Privileged": false,"PublishAllPorts": false,"ReadonlyRootfs": false,"SecurityOpt": null,"UTSMode": "","UsernsMode": "","ShmSize": 67108864,"Runtime": "runc","ConsoleSize": [0,0],"Isolation": "","CpuShares": 0,"Memory": 0,"NanoCpus": 0,"CgroupParent": "","BlkioWeight": 0,"BlkioWeightDevice": [],"BlkioDeviceReadBps": null,"BlkioDeviceWriteBps": null,"BlkioDeviceReadIOps": null,"BlkioDeviceWriteIOps": null,"CpuPeriod": 0,"CpuQuota": 0,"CpuRealtimePeriod": 0,"CpuRealtimeRuntime": 0,"CpusetCpus": "","CpusetMems": "","Devices": [],"DeviceCgroupRules": null,"DeviceRequests": null,"KernelMemory": 0,"KernelMemoryTCP": 0,"MemoryReservation": 0,"MemorySwap": 0,"MemorySwappiness": null,"OomKillDisable": false,"PidsLimit": null,"Ulimits": null,"CpuCount": 0,"CpuPercent": 0,"IOMaximumIOps": 0,"IOMaximumBandwidth": 0,"MaskedPaths": ["/proc/asound","/proc/acpi","/proc/kcore","/proc/keys","/proc/latency_stats","/proc/timer_list","/proc/timer_stats","/proc/sched_debug","/proc/scsi","/sys/firmware"],"ReadonlyPaths": ["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]},"GraphDriver": {"Data": {"DeviceId": "39","DeviceName": "docker-253:0-101005257-53e59acc5fd0213da13f09d3e0ec58b9950515142236b63f5db54cd870fe7f5e","DeviceSize": "10737418240"},"Name": "devicemapper"},"Mounts": [],"Config": {"Hostname": "2ddb980c237b","Domainname": "","User": "","AttachStdin": false,"AttachStdout": false,"AttachStderr": false,"Tty": false,"OpenStdin": false,"StdinOnce": false,"Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd": ["/bin/sh","-c","while true;do echo testing;sleep 1;done"],"Image": "centos","Volumes": null,"WorkingDir": "","Entrypoint": null,"OnBuild": null,"Labels": {"org.label-schema.build-date": "20201204","org.label-schema.license": "GPLv2","org.label-schema.name": "CentOS Base Image","org.label-schema.schema-version": "1.0","org.label-schema.vendor": "CentOS"}},"NetworkSettings": {"Bridge": "","SandboxID": "eab0b29cd1fe9a77e9993d52c5d03682140b449471b1867e356654fb6f4c4e1e","HairpinMode": false,"LinkLocalIPv6Address": "","LinkLocalIPv6PrefixLen": 0,"Ports": {},"SandboxKey": "/var/run/docker/netns/eab0b29cd1fe","SecondaryIPAddresses": null,"SecondaryIPv6Addresses": null,"EndpointID": "78359b6fe14b709541b25e83f203d88095a4930b867d94cc8a70ca6610fb340a","Gateway": "172.17.0.1","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"IPAddress": "172.17.0.3","IPPrefixLen": 16,"IPv6Gateway": "","MacAddress": "02:42:ac:11:00:03","Networks": {"bridge": {"IPAMConfig": null,"Links": null,"Aliases": null,"NetworkID": "7cdd9d5f836f2c4f15ed71f0a8ab272f871ac56766d6a4eebcbbcc5df6697bed","EndpointID": "78359b6fe14b709541b25e83f203d88095a4930b867d94cc8a70ca6610fb340a","Gateway": "172.17.0.1","IPAddress": "172.17.0.3","IPPrefixLen": 16,"IPv6Gateway": "","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"MacAddress": "02:42:ac:11:00:03","DriverOpts": null}}}}
]

docker exec、attach进入当前正在运行的容器

#通常容器都是使用后台方式运行的,需要进入容器,需要修改一些配置#命令
$ docker exec -it 容器ID bashshell
$ docker attach 容器id#测试
[root@master ~]# docker exec -it 2ddb980c237bc /bin/bash
[root@2ddb980c237b /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@2ddb980c237b /]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 16:37 ?        00:00:01 /bin/sh -c while true;do echo testing;sleep 1;done
root       2361      0  0 17:16 pts/0    00:00:00 /bin/bash
root       2429      1  0 17:17 ?        00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root       2430   2361  0 17:17 pts/0    00:00:00 ps -ef[root@master ~]# docker attach 2ddb980c237bc
testing
testing
testing
testing
testing
执行当前的代码...#docker exec    #进入容器后开启一个新的终端,可以在里面操作(常用)
#docker attach  #进入容器正在执行的终端,不会启动新的进程

docker cp 从容器拷贝文件到主机上

docker cp 容器id:容器内路径 主机路径[root@master ~]# docker run -it centos /bin/bash
[root@0c92d62926ff /]#
[root@master ~]#
[root@master ~]# cd /home
[root@master home]# touch test.java
[root@master home]# ls
centos  test.java
[root@master home]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED              STATUS              PORTS     NAMES
0c92d62926ff   centos    "/bin/bash"   About a minute ago   Up About a minute             thirsty_franklin
[root@master home]# docker attach 0c92d62926ff
[root@0c92d62926ff /]# cd /home
[root@0c92d62926ff home]# ls
[root@0c92d62926ff home]# touch test123.java
[root@0c92d62926ff home]# exit
exit
#将文件拷贝出来到主机上
[root@master home]# docker cp 0c92d62926ff:/home/test123.java /home
[root@master home]# ls
centos  test123.java  test.java
[root@master home]##拷贝是一个手动过程,未来使用 -v卷的技术,实现自动同步 /home /home

小结

attach       Attach to a running container  #当前she17 下attach连接指定运行镜像
bui1d       Bui1d an image from a Dockerfile  #通过Dockerfile定制镜像
commit      create a new image from a container changes  #提交当前容器为新的镜像cp
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  #查看容器详细信息ki11
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      Logout 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 a71 processes within a container  #暂停容器
ps          List containers  #列出容器列表
pu11        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包[对应1oad]
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  #截取容器停止时的退出状态值

docker 的命令是十分多的,上面我们学习的那些都是常用的容器和镜像的命令,之后还会学习很多命令!

Docker安装与常用命令详解——初步拓荒相关推荐

  1. Samtools(CentOS Linux)安装及常用命令详解

    序列比对(将测序reads与已知序列信息的基因或基因组进行比对)是高通量测序数据分析中最重要的一环,无论是转录组还是重测序都是基于比对结果来进行后续各项分析的,比对结果格式比较常见的是sam和bam文 ...

  2. Samtools安装及常用命令详解

    Samtools是一个用于操作sam和bam文件的工具合集.包含有许多命令.以下是常用命令的介绍 下载安装包: http://www.htslib.org/download/ 安装依赖: yum in ...

  3. docker常用命令详解

    docker常用命令详解 本文只记录docker命令在大部分情境下的使用,如果想了解每一个选项的细节,请参考官方文档,这里只作为自己以后的备忘记录下来. 根据自己的理解,总的来说分为以下几种: Doc ...

  4. router linux命令,router os 常用命令详解

    router os 常用命令详解 发布时间:2006-04-20 01:17:30来源:红联作者:[Hl.y] 看了很多router os 的资料都是关于如何安装的,却很少见到关于router os的 ...

  5. NodeJs学习笔记002--npm常用命令详解

    npm 常用命令详解 npm是什么 npm install 安装模块 npm uninstall 卸载模块 npm update 更新模块 npm outdated 检查模块是否已经过时 npm ls ...

  6. Linux常用命令详解(两万字超长文)

    Linux常用命令详解 作为一枚程序员,熟练掌握一些常见的linux命令是很有必要的,掌握这些命令能帮助我们更好地管理Linux系统,提高工作效率,并有效地解决各种问题,为了方便自己后续查阅以及帮助不 ...

  7. Linux常用命令详解文库

     Linux常用命令详解文库.txt精神失常的疯子不可怕,可怕的是精神正常的疯子!Linux常用命令详解 来源: LUPA开源社区 发布时间: 2007-05-27 05:34 版权申明 字体: ...

  8. ceph命令系列(一):ceph-deploy/ceph/rados/rbd 常用命令详解

    ceph-deploy 常用命令详解 命令 描述 ceph-deploy new [mon-node ...] 指定node(s)为monitor,开始部署一个新的ceph集群,并且在当前目录创建ce ...

  9. Python包管理工具PIP常用命令详解

    1. PIP安装 目前Python2.7.10以上版本和Python3.3以上版本都已经自带了setuptools及pip,因此不需要额外考虑安装pip,只需要在安装的时候配置好就可以使用. 2. P ...

  10. Linux常用命令详解(最全)

    Linux命令目录 Linux命令集合 系统信息 关机 (系统的关机.重启以及登出 ) 文件和目录 文件搜索 挂载一个文件系统 磁盘空间 用户和群组 文件的权限 - 使用 "+" ...

最新文章

  1. Spring Boot 异步请求和异步调用
  2. python高精度加法_14.高精度加法
  3. SpringBoot(Thymeleaf)拼接跳转链接
  4. 一种新的穿透防火墙的数据传输技术
  5. linux SHELL脚本编程
  6. 什么是计算机网络教学反思,《计算机网络实训之常用的网络工具》教学反思
  7. 计算机科学和建筑设计结合,智能化建筑中计算机科学与技术的应用
  8. Echange 的发展史
  9. 线程的异常捕获与线程池的异常捕获
  10. 使用doxygen查看文件包含关系图
  11. spring事务传播行为与事务隔离等级
  12. flash 倒计时功能
  13. php实现ts流切片,HLS-m3u8播放列表和ts切片(2)
  14. safri 对于display:block;的元素显示不出来 其他所有浏览器均正常
  15. 面板数据熵值法-Python
  16. MBA都包括哪些课程?有哪些MBA专业书籍值得推荐?
  17. 在Hexo博客上添加可爱的Live 2D模型
  18. LAMP环境搭建之编译安装指南(php-5.3.27.tar.gz)
  19. 服务器MXNET环境配置问题
  20. 《清单革命》内容梳理随笔

热门文章

  1. 达梦数据库报网络通讯异常排查步骤
  2. 微信提现显示服务器异常,微信零钱提现为什么显示提示交易异常 解决办法是什么...
  3. vue+element实现一个excel表格下载的功能
  4. 计算机病毒的特点分类危害性,计算机病毒的分类及破坏是什么
  5. Maven的仓库(转载自Maven实战 作者许晓斌)
  6. 《Maven实战》(许晓斌)导读(读书笔记第二次读后感)
  7. python实现zip分卷压缩与解压
  8. MVVM(维基百科搜索)
  9. java英文面试常见问题归纳
  10. 在centos上更改服务器时区(美国时间、北京时间)