docker 基本使用命令

docker 基本命令格式:

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



搜索鏡像

docker search [OPTIONS] TERM


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



創建容器

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都會被刪除



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章