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 p_w_picpaths...   22                   [OK]
docker.io   docker.io/tutum/ubuntu                                           Simple Ubuntu docker p_w_picpaths 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 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

注意:Bash shellを表示するには、コマンドを実行した後にもう一度enterを表示します.
コンテナ内部のコマンドを外部から実行
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