docker 基本命令格式:

Usage: docker [OPTIONS] COMMAND [arg...]



搜索镜像

docker search [OPTIONS] TERM


# docker search ubuntu
INDEX       NAME                                                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
   /ubuntu                                                 Ubuntu is a Debian-based Linux operating s...   6518      [OK]       
   /dorowu/ubuntu-desktop-lxde-vnc                         Ubuntu with openssh-server and NoVNC            128                  [OK]
   /rastasheep/ubuntu-sshd                                 Dockerized SSH service, built on top of of...   98                   [OK]
   /ansible/ubuntu14.04-ansible                            Ubuntu 14.04 LTS with ansible                   86                   [OK]
   /ubuntu-upstart                                         Upstart is an event-based replacement for ...   77        [OK]       
   /neurodebian                                            NeuroDebian provides neuroscience research...   38        [OK]       
   /ubuntu-debootstrap                                     debootstrap --variant=minbase --components...   30        [OK]       
   /nuagebec/ubuntu                                        Simple always updated Ubuntu docker p_w_picpaths...   22                   [OK]
   /tutum/ubuntu                                           Simple Ubuntu docker p_w_picpaths with SSH access     18                   
   /1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          13                   [OK]
   /ppc64le/ubuntu                                         Ubuntu is a Debian-based Linux operating s...   10                   
   /aarch64/ubuntu                                         Ubuntu is a Debian-based Linux operating s...   9                    
   /i386/ubuntu                                            Ubuntu is a Debian-based Linux operating s...   8                    
   /codenvy/ubuntu_jdk8                                    Ubuntu, JDK8, Maven 3, git, curl, nmap, mc...   3                    [OK]



下载镜像

Usage:  docker pull [OPTIONS] NAME[:TAG|@DIGEST]

例:

# docker pull ubuntu:latest


注:设置latest后是下载最新版本,也可指定使用版本如:ubuntu:15.10、ubuntu:14.04

镜像中的名称中,在“/” 之前指定用户名即可下载指定用户上传的镜像,如“pyrasis/ubuntu”,而官方的镜像不会出现用户名



查看已下载的镜像

# docker p_w_picpaths



创建容器

Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

# docker run -i -t --name test ubuntu:15.10 /bin/bash
root@7173c362e465:/#

注:容器是基于镜像创建的。

  使用 -i(interaction) 和 -t(Pseudo-tty)选项可以在运行的Bash shell中进行输入与输出操作。

  使用 --name 选项可以指定新建的容器名称,假若不指定则Docker会自动生成一个名称。

  由上图可以看到我们已经创建了一个名称为test的ubuntu容器,并且已经在Bash shell中,可以使用cd、ls查看容器内部,从中可以发现其与主机OS的不同。

  新建的ubuntu容器中还没安装大部分软件包,所以无法使用ifconfig 和ping,所以在容器里执行

# apt-get update
# apt install net-tools       # ifconfig 
# apt install iputils-ping     # ping
root@7173c362e465:/# ifconfig eth0  
eth0      Link encap:Ethernet  HWaddr 02:42:ac:11:00:0a  
          inet addr:172.17.0.10  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe11:a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:648 (648.0 B)
root@7173c362e465:/# ping www.baidu.com
PING www.a.shifen.com (180.149.131.98) 56(84) bytes of data.
64 bytes from 180.149.131.98: icmp_seq=1 ttl=50 time=37.0 ms
64 bytes from 180.149.131.98: icmp_seq=2 ttl=50 time=37.1 ms
64 bytes from 180.149.131.98: icmp_seq=3 ttl=50 time=37.0 ms
^C
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 37.003/37.077/37.136/0.229 ms

使用exit即可退出容器。



查看容器列表

Usage:  docker ps [OPTIONS]

例:

# docker ps -a        #列出所用的容器
# docker ps           #列出运行的容器



使用start启动容器和restart重启容器以及stop停止容器

例:

# docker start test 
# docker restart test
# docker stop test

注:可以使用容器标签(CONTAINER ID)来代表名称



连接容器

Usage:  docker attach [OPTIONS] CONTAINER

例:

# docker attch test

注:执行命令后要再enter一次才显示Bash shell.



从外部运行容器内部的命令

Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

例:

# docker exec test echo "hello world"
hello world



删除容器和镜像

docker rm test#删除容器,需停止test
docker rmi ubuntu:15.10     #删除镜像,假若无标签则无视标签,所有名称带ubuntu都会被删除