Docker && 常用操作指南

2- Docker常用操作指南

 

2-1 从DockerHub上搜索你想要的Docker镜像,比如你要搜索centos这个镜像

  • name表示镜像的名称
  • description表示镜像的来源
  • stars表示有多少人赞了这个镜像
  • official表示这个镜像是否官方镜像
  • automated表示这个镜像是否自动化

 

# docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   4702                [OK]                
ansible/centos7-ansible            Ansible on Centos7                              118                                     [OK]
jdeathe/centos-ssh                 CentOS-6 6.10 x86_64 / CentOS-7 7.5.1804 x86…   99                                      [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   63                                      [OK]
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              45                                      [OK]

......

 

2-2 指定好一个Docker镜像后,从DockerHub上,下载到本地

  • docker pull centos:7.4.1708  解释:下载一个镜像名称为centos并且这个镜像的标签为7.4.1708,名称与标签必须和DockerHub上的保持一致
  • docker image ls  解释:查看镜像文件docker image ls --no-trunc
  • docker image ls --no-trunc  解释:查看这个镜像文件的完整ID编号

 

# docker pull centos:7.4.1708
7.4.1708: Pulling from library/centos
18b8eb7e7f01: Pull complete 
Digest: sha256:2a0db0b7a5163f97a12f0487c78179ab4ce53877441bba3cded5dac24fb12792
Status: Downloaded newer image for centos:7.4.1708

# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7.4.1708 d3949e34634c 5 weeks ago 197MB

# docker image ls --no-trunc
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7.4.1708 sha256:d3949e34634c9a234da0adb874d0c1e516dd9fa33801a3216b45157f5d1fbcf8 5 weeks ago 197MB

 

2-3 创建一个Docer容器并且运行起来

  • docker run --name liuqi_centos-7.4 -it centos:7.4.1708  解释:创建并且运行一个新容器,给这个新容器起个名字叫liuqi_centos-7.4,使用-it参数让容器使用交互模式,这个容器的镜像是centos:7.4.1708
  • exit  解释:退出当前liuqi_centos-7.4这个容器
  • docker container ls -a  解释:查看当前运行的容器有哪些,如果显示exited,说明这个容器是未运行状态
  • docker container start -ai liuqi_centos-7.4  解释:让这个容器重新运行起来

 

#  docker run --name liuqi_centos-7.4 -it centos:7.4.1708
[root@135860e4becd /]#

[root@135860e4becd /]# ls
anaconda-post.log etc lib64 mnt root srv usr
bin home lost+found opt run sys var
dev lib media proc sbin tmp

[root@135860e4becd /]# exit
exit

[root@master-01 ~]#

[root@master-01 ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
135860e4becd centos:7.4.1708 "/bin/bash" 14 minutes ago Exited (0) 4 minutes ago liuqi_centos-7.4

[root@master-01 ~]# docker container start -ai liuqi_centos-7.4

[root@135860e4becd /]# ls
anaconda-post.log dev home lib64 media opt root sbin sys usr
bin etc lib lost+found mnt proc run srv tmp var

 

2-4 正常停止一个容器和强制杀死一个容器进程

  • stop  解释:正常停止容器运行
  • kill  解释:非正常杀死一个容器进程

 

# docker container stop liuqi_centos-7.4
liuqi_centos-7.4

# docker container kill liuqi_centos-7.4
liuqi_centos-7.4

 

2-5 删除一个Docker镜像文件

  • docker ps -a  解释:查看要删除的这个镜像文件是否正在使用
  • docker rm liuqi_centos-7.4  解释:如果这个镜像文件正在使用中时,先要删除这个镜像文件所依赖的容器
  • docker rmi d3949e34634c  解释:被依赖的容器被删除了,再删除这个镜像文件

# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7.4.1708            d3949e34634c        5 weeks ago         197MB

[root@master-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
135860e4becd        centos:7.4.1708     "/bin/bash"         36 minutes ago      Exited (137) 4 minutes ago                       liuqi_centos-7.4

[root@master-01 ~]# docker rm liuqi_centos-7.4
liuqi_centos-7.4

[root@master-01 ~]# docker rmi d3949e34634c
Untagged: centos:7.4.1708
Untagged: centos@sha256:2a0db0b7a5163f97a12f0487c78179ab4ce53877441bba3cded5dac24fb12792
Deleted: sha256:d3949e34634c9a234da0adb874d0c1e516dd9fa33801a3216b45157f5d1fbcf8
Deleted: sha256:129b697f70e96b903f2025859be4444624b4a87394e113918a7f855e97a12ae2

 

2-6 实时查看容器中的日志内容和容器中的网络详细信息

  • docker logs -ft --tail 5 liuqi_centos-7.4  解释:实时查看liuqi_centos-7.4这个容器中最后5行的日志信息
  • docker inspect liuqi_centos-7.4  解释:查看当前容器的IP地址或容器网络详细信息

 

# docker logs -ft --tail 5 liuqi_centos-7.4
(2/4): updates/7/x86_64/primary_db                                                                     | 5.2 MB  00:00:00     
^C/4): base/7/x86_64/primary_db                  50% [===================                   ]  18 kB/s | 5.8 MB  00:05:19 ETA 
2018-09-15T23:29:22.346029677Z 
2018-09-15T23:29:22.346032533Z Exiting on user cancel
2018-09-15T23:29:23.627084760Z [root@45c08d7d2b39 /]# clear

# docker inspect liuqi_centos-7.4 | grep "IPAddress" | tail -1
"IPAddress": "172.17.0.2",

 

 

 

 

posted on 2018-09-15 21:54  惟净师兄  阅读(1862)  评论(0编辑  收藏  举报

导航