Docker 常用命令

docker用起来还是很方便的,而且命令也容易,常用的命令如下:
docker run
docker ps
docker inspect
docker attach
….

docker run运行一个容器

Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...] [flags]
[root@ubuntu:~]# docker run -it ubuntu /bin/bash
[root@c32ec2b3f49a:/]# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  
srv  sys  tmp  usr  var 
[root@c32ec2b3f49a:/]#   
  -i 为容器打开标准输出
  -t 给打开的容器运行一个伪tty终端

docker ps 列出容器

[root@ubuntu:~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
c32ec2b3f49a        ubuntu              "/bin/bash"         2 minutes ago       Up 2 minutes                            hopeful_bose
4f078216cfef        ubuntu              "/bin/bash"         47 hours ago        Up 47 hours                             competent_sammet
41432143ef92        ubuntu              "/bin/bash"         47 hours ago        Up 47 hours                             hardcore_payne
929860f00fa4        ubuntu              "/bin/bash"         47 hours ago        Up 47 hours                             competent_perlman
d2170af637a4        centos              "/bin/bash"         47 hours ago        Up 47 hours                             epic_stallman
3ac68df6d5de        ubuntu              "/bin/bash"         2 days ago          Up 2 days                               blissful_northcutt
8b9e5cc28561        ubuntu              "/bin/bash"         2 days ago          Up 2 days                               cranky_stallman

[root@ubuntu:~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
c32ec2b3f49a        ubuntu              "/bin/bash"         2 minutes ago       Up 2 minutes                            hopeful_bose

 -a 列出所有的容器
 -l 最新创建的容器

docker inspect列出镜像信息

Usage:  docker inspect [OPTIONS] NAME|ID [NAME|ID...] [flags]
[root@ubuntu:~]# docker inspect c32ec2b3f49a
            hopeful_bose
[
{
    "Id": "c32ec2b3f49a5774bef44f88c00c1303d760c142fc0b528ad33c61208cf97725",
    "Created": "2018-04-15T14:01:11.956052589Z",
    "Path": "/bin/bash",
    "Args": [],
    "State": {
        "Status": "running",
        "Running": true,
        "Paused": false,
        "Restarting": false,
        "OOMKilled": false,
        "Dead": false,
        "Pid": 6180,
        "ExitCode": 0,
        "Error": "",
        "StartedAt": "2018-04-15T14:01:12.390885359Z",
        "FinishedAt": "0001-01-01T00:00:00Z"

...
...

docker attach进入后台运行的容器

Usage:  docker attach [OPTIONS] CONTAINER [flags]
[root@ubuntu:~]# docker run -it ubuntu /bin/bash
[root@40fe9f37c774:/]#
[root@40fe9f37c774:/]# root@ubuntu:~#
[root@ubuntu:~]#

注意:在刚创建的容器里,"Ctrl+d"直接退出并结束进程,使用"Ctrl+P+Q"(大写)退出不结束进程

[root@ubuntu:~]# docker attach 40fe9f37c774
[root@40fe9f37c774:/]#
[root@40fe9f37c774:/]#

docker exec 命令

Usage:  docker exec [OPTIONS] CONTAINER COMMAND [ARG...] [flags]
[root@ubuntu:~]# docker run -itd ubuntu
cd92f0ec9d4018f24ee4a06edd17f38d382dbed174bc578825d72e42219f6653
[root@ubuntu:~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                  PORTS               NAMES
cd92f0ec9d40        ubuntu              "/bin/bash"              16 seconds ago      Up 16 seconds                                   upbeat_blackwell

[root@ubuntu:~]# docker exec -it cd92f0ec9d40 bash
root@cd92f0ec9d40:/# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
[root@cd92f0ec9d40:/]# exit
[root@ubuntu:~]# docker exec -it cd92f0ec9d40 bash
[root@cd92f0ec9d40:/]#
可以看到使用了exec命令后,exit退出容器也不会停止

列出所有容器
[root@ubuntu:~]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
cd92f0ec9d40        ubuntu              "/bin/bash"              39 minutes ago      Up 39 minu

docker export导出容器快照

[root@ubuntu:~]# docker export cd92f0ec9d40 >/tmp/ubuntu.tar 
[root@ubuntu:~]# ls /tmp/
ubuntu.tar            unity_support_test.0

docker import导入容器快照

[root@ubuntu:~]# cat /tmp/ubuntu.tar |docker import - test/ubuntu:v1.0
sha256:0ccd5ef276a3b175b14d9b031fba85bbdf44e8d5819a62d49564333398f0e9dc
[root@ubuntu:~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
test/ubuntu         v1.0                072cd43a3a6a        About a minute ago   85.8MB

docker rm 删除容器

[root@ubuntu:~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
1862e1300c97        ubuntu              "/bin/bash"         46 hours ago        Up 46 hours                             stupefied_chebyshev
4f078216cfef        ubuntu              "/bin/bash"         46 hours ago        Up 46 hours                             competent_sammet
41432143ef92        ubuntu              "/bin/bash"         46 hours ago        Up 46 hours                             hardcore_payne
929860f00fa4        ubuntu              "/bin/bash"         47 hours ago        Up 47 hours                             competent_perlman
d2170af637a4        centos              "/bin/bash"         47 hours ago        Up 47 hours                             epic_stallman
3ac68df6d5de        ubuntu              "/bin/bash"         47 hours ago        Up 47 hours                             blissful_northcutt
8b9e5cc28561        ubuntu              "/bin/bash"         47 hours ago        Up 47 hours                             cranky_stallman

[root@ubuntu:~]# docker container rm 1862e1300c97 
Error response from daemon: You cannot remove a running container cd92f0ec9d4018f24ee4a06edd17f38d382dbed174bc578825d72e42219f6653.
Stop the container before attempting removal or force remove
注意:因为当前容器是正在运行的,所以直接停止会报错,加上参数-f,docker会发送sigkill信号给容器

[root@ubuntu:~]# docker container rm -f 1862e1300c97
1862e1300c97
[root@ubuntu:~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4f078216cfef        ubuntu              "/bin/bash"         46 hours ago        Up 46 hours                             competent_sammet
41432143ef92        ubuntu              "/bin/bash"         46 hours ago        Up 46 hours                             hardcore_payne
929860f00fa4        ubuntu              "/bin/bash"         47 hours ago        Up 47 hours                             competent_perlman
d2170af637a4        centos              "/bin/bash"         47 hours ago        Up 47 hours                             epic_stallman
3ac68df6d5de        ubuntu              "/bin/bash"         47 hours ago        Up 47 hours                             blissful_northcutt
8b9e5cc28561        ubuntu              "/bin/bash"         47 hours ago        Up 47 hours                             cranky_stallman
[root@ubuntu:~]#

prune清除所有处于终止状态的容器

[root@ubuntu:~]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
cd92f0ec9d40        ubuntu              "/bin/bash"              46 hours ago        Exited (0) 6 minutes ago                       upbeat_blackwell
4f078216cfef        ubuntu              "/bin/bash"              46 hours ago        Up 46 hours                                    competent_sammet
51783f7ff529        ubuntu              "/bin/bash"              46 hours ago        Exited (0) 46 hours ago                        competent_chebyshev
41432143ef92        ubuntu              "/bin/bash"              47 hours ago        Up 47 hours                                    hardcore_payne
4e80f171f0dd        ubuntu              "/bin/bash"              47 hours ago        Exited (0) 47 hours ago                        xenodochial_dijkstra
e34b31703aac        ubuntu:17.10        "/bin/sh -c 'whilr t…"   47 hours ago        Exited (0) 47 hours ago                        reverent_bhaskara
929860f00fa4        ubuntu              "/bin/bash"              47 hours ago        Up 47 hours                                    competent_perlman
af27e78a0078        ubuntu              "/bin/bash"              47 hours ago        Exited (0) 47 hours ago                        goofy_hamilton
d4306e006205        ubuntu              "/bin/echo ' hello w…"   47 hours ago        Exited (0) 47 hours ago                        relaxed_albattani
6134f0d591a5        ubuntu              "/bin/hello world"       47 hours ago        Created                                        determined_heisenberg
d2170af637a4        centos              "/bin/bash"              47 hours ago        Up 47 hours                                    epic_stallman
3ac68df6d5de        ubuntu              "/bin/bash"              47 hours ago        Up 47 hours                                    blissful_northcutt
8b9e5cc28561        ubuntu              "/bin/bash"              47 hours ago        Up 47 hours                                    cranky_stallman

[root@ubuntu:~]# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
cd92f0ec9d4018f24ee4a06edd17f38d382dbed174bc578825d72e42219f6653
51783f7ff529e82fa852921bfe1f567f97345e9fb2934808803954e24a24a1e9
4e80f171f0ddaebde20a79e643585e8ace4e515804b423b6da04a67bf49c346a
e34b31703aaca5931468cee0df8763d4dc78df36bb7ed5ba0e0c09729f8ec825
af27e78a00784aea88258f4d528f35c1cd616f68a76d3dbf222104061f533ba5
d4306e006205fb89b3ca64b5efe4e73591bfce0b283c208733cf1e8f5ca6fe58
6134f0d591a50ae081db0c91d6d8fc1efda0c9588424fa6890daa01a84e115b0

Total reclaimed space: 8B

可以看到终止的容器已经被清理掉了
[root@ubuntu:~]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4f078216cfef        ubuntu              "/bin/bash"         46 hours ago        Up 46 hours                             competent_sammet
41432143ef92        ubuntu              "/bin/bash"         47 hours ago        Up 47 hours                             hardcore_payne
929860f00fa4        ubuntu              "/bin/bash"         47 hours ago        Up 47 hours                             competent_perlman
d2170af637a4        centos              "/bin/bash"         47 hours ago        Up 47 hours                             epic_stallman
3ac68df6d5de        ubuntu              "/bin/bash"         47 hours ago        Up 47 hours                             blissful_northcutt
8b9e5cc28561        ubuntu              "/bin/bash"         47 hours ago        Up 47 hours                             cranky_stallman
[root@ubuntu:~]#

docker stop停止一个容器

[root@ubuntu:~]# docker run -it ubuntu /bin/bash
[root@2a1de0464580:/]#
[root@ubuntu:~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
2a1de0464580        ubuntu              "/bin/bash"         41 seconds ago      Up 40 seconds                           romantic_bhaskara
[root@ubuntu:~]# docker stop 2a1de0464580
2a1de0464580

docker search拉取镜像

[root@ubuntu:~]# docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   4188                [OK]
ansible/centos7-ansible            Ansible on Centos7                              108                                     [OK]
jdeathe/centos-ssh                 CentOS-6 6.9 x86_64 / CentOS-7 7.4.1708 x86_…   94                                      [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   52                                      [OK]
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              40                                      [OK]
tutum/centos                       Simple CentOS docker image with SSH access      38
gluster/gluster-centos             Official GlusterFS Image [ CentOS-7 +  Glust…   26                                      [OK]
centos/mysql-57-centos7            MySQL 5.7 SQL database server                   23
openshift/base-centos7             A Centos7 derived base image for Source-To-I…   22
kinogmt/centos-ssh                 CentOS with SSH                                 19                                      [OK]
centos/python-35-centos7           Platform for building and running Python 3.5…   19
centos/postgresql-96-centos7       PostgreSQL is an advanced Object-Relational …   12
openshift/jenkins-2-centos7        A Centos7 based Jenkins v2.x image for use w…   11
centos/httpd-24-centos7            Platform for running Apache httpd 2.4 or bui…   10
openshift/mysql-55-centos7         DEPRECATED: A Centos7 based MySQL v5.5 image…   6
openshift/wildfly-101-centos7      A Centos7 based WildFly v10.1 image for use …   3
openshift/jenkins-1-centos7        DEPRECATED: A Centos7 based Jenkins v1.x ima…   3
pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag names…   3
pivotaldata/centos                 Base centos, freshened up a little with a Do…   1
pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile t…   1
blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                                       [OK]
openshift/php-55-centos7           DEPRECATED: A Centos7 based PHP v5.5 image f…   1
pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated wi…   0
smartentry/centos                  centos with smartentry                          0                                       [OK]
jameseckersall/sonarr-centos       Sonarr on CentOS 7                              0                                       [OK]
root@ubuntu:~#

可以看到拉取的镜像返回了很多内容,包括:镜像名称,描述,收藏数,是否是官方创建,是否为自动创建


列出所有镜像
Usage:    docker images [OPTIONS] [REPOSITORY[:TAG]]
[root@ubuntu:~]# docker images -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test/ubuntu         v1.0                072cd43a3a6a        About an hour ago   85.8MB
<none>              <none>              0ccd5ef276a3        46 hours ago        85.8MB
ubuntu              latest              c9d990395902        2 days ago          113MB
ubuntu              17.10               14107f6d2c97        2 days ago          99.1MB
centos              latest              e934aafc2206        8 days ago          199MB
[root@ubuntu:~]#

 -a 显示所有镜像
 -q 显示镜像ID

docker pull下载镜像到本地

[root@ubuntu:~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
Digest: sha256:989b936d56b1ace20ddf855a301741e52abca38286382cba7f44443210e96d16
Status: Image is up to date for centos:latest
root@ubuntu:~#
-------------本文结束感谢您的阅读-------------