2017年8月17日木曜日

コンテナからホストOSに抜けるためにctrl+P+Qを押す

コンテナからホストOSに抜けるためにctrl+P+Qを押す

[root@956ebd5d66b4 /]#
[root@956ebd5d66b4 /]#  <==(ここでctrl+P+Qを押すと抜けれる)
[root@localhost ~]#

コンテナの中のネットワーク設定状況

[root@956ebd5d66b4 /]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02
          inet addr:172.17.0.2  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:23 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:2512 (2.4 KiB)  TX bytes:648 (648.0 b)

[root@956ebd5d66b4 /]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.17.0.1      0.0.0.0         UG    0      0        0 eth0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0
[root@956ebd5d66b4 /]#

docker run:docker runコマンドでリポジトリ内のイメージのコンテナを起動する

docker runコマンドでリポジトリ内のイメージのコンテナを起動する
docker run -it --name centos01 [使用するイメージ名(リポジトリ名:イメージ名)]
→-itオプションはおまじないとして基本的にはつけておく。
→--nameオプションは起動するコンテナ名を指定する。つけないと適当な名前が付与されるらしい。

コンテナの起動と併せてbashを起動することで、起動したコンテナにて操作を行える。

[root@localhost ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
docker.io/hogehogeRepository/centos   centos6             b8c94c5d2d7c        2 years ago         203.1 MB
docker.io/hogehogeRepository/centos   centos7             3bd78cf8ed76        2 years ago         172.2 MB
docker.io/hogehogeRepository/centos   latest              3bd78cf8ed76        2 years ago         172.2 MB
[root@localhost ~]# docker run -it --name centos01 hogehogeRepository/centos:centos6 /bin/bash
[root@956ebd5d66b4 /]#
[root@956ebd5d66b4 /]# cat /etc/centos-release
CentOS release 6.6 (Final)
[root@956ebd5d66b4 /]# df -h /
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/docker-253:0-50488737-d54959bdb29a05cb10a65d049b237a8e5aaaab33d9fc84f6c1de1c4890d3ab49
                       10G  250M  9.8G   3% /
[root@956ebd5d66b4 /]#
[root@956ebd5d66b4 /]#
[root@956ebd5d66b4 /]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 23:46 ?        00:00:00 /bin/bash
root         15      1  0 23:48 ?        00:00:00 ps -ef
[root@956ebd5d66b4 /]#

docker pullとdocker images:リポジトリのダウンロードとリポジトリ内のイメージの一覧表示

①リポジトリをpullサブコマンドでダウンロードしてくる
docker pull -a hogehogeRepository/centos

②ダウンロードしてきたリポジトリ内のイメージの一覧を出力する
docker images


実行例①
[root@localhost ~]# docker pull -a hogehogeRepository/centos
Trying to pull repository docker.io/hogehogeRepository/centos ...
centos6: Pulling from docker.io/hogehogeRepository/centos

a3ed95caeb02: Pull complete
dfabbb82181a: Pull complete
Digest: sha256:1422c5ba80430ae5899e40774bb3033e71cba5589d1163e04b1b801698e61127
centos7: Pulling from docker.io/hogehogeRepository/centos

a3ed95caeb02: Pull complete
a5b52df5865b: Pull complete
Digest: sha256:5871aecd08cd037002d42ebb92cfe11a78c93806ab2275a627bf4551cc638855
latest: Pulling from docker.io/hogehogeRepository/centos

a3ed95caeb02: Already exists
a5b52df5865b: Already exists
Digest: sha256:49ecd6397d1f23deb245d3e0332862d68a9d34bb1d030066ae6175b8e9540c34
[root@localhost ~]#


実行例②
[root@localhost ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
docker.io/hogehogeRepository/centos   centos6             b8c94c5d2d7c        2 years ago         203.1 MB
docker.io/hogehogeRepository/centos   centos7             3bd78cf8ed76        2 years ago         172.2 MB
docker.io/hogehogeRepository/centos   latest              3bd78cf8ed76        2 years ago         172.2 MB
[root@localhost ~]#

docker searchでリポジトリを検索する

docker searchでリポジトリを検索する時の実行例
docker search hogehogeRepository
→hogehogeRepositoryというリポジトリを検索。
→docker.ioはdockerHubを表現
→hogehogeRepository/centosはリポジトリを表現

[root@localhost ~]# docker search hogehogeRepository
INDEX       NAME                                               DESCRIPTION                               STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/hogehogeRepository/jupyter_tensorflow                                                        3                
docker.io   docker.io/hogehogeRepository/app                                                                       0                
docker.io   docker.io/hogehogeRepository/centos                          Sample CentOS repository for dockerbon.   0                
docker.io   docker.io/hogehogeRepository/dbs                                                                       0                
docker.io   docker.io/hogehogeRepository/eplite                                                                    0                
docker.io   docker.io/hogehogeRepository/epmysql                                                                   0                
docker.io   docker.io/hogehogeRepository/fedora                          Sample fedora repository for dockerbon.   0                
docker.io   docker.io/hogehogeRepository/httpd                                                                     0                
docker.io   docker.io/hogehogeRepository/ostools                                                                   0                
docker.io   docker.io/hogehogeRepository/test-httpd                                                                0                
docker.io   docker.io/hogehogeRepository/vcp_base                                                                  0                
docker.io   docker.io/hogehogeRepository/web                                                                       0                
docker.io   docker.io/yacchin1205/hogehogeRepository-tensorflow-kernel                                             0                
[root@localhost ~]#

dockerコマンドのmanページ、サブコマンドの確認

[root@localhost ~]# man docker

DOCKER(1)                                               APRIL 2014                                              DOCKER(1)

NAME
       docker - Docker image and container command line interface

SYNOPSIS
       docker [OPTIONS] COMMAND [arg...]

       docker daemon [--help|...]

       docker [--help|-v|--version]

DESCRIPTION
       is a client for interacting with the daemon (see dockerd(8)) through the CLI.

       The  Docker  CLI  has  over 30 commands. The commands are listed below and each has its own man page which explain
       usage and arguments.

       To see the man page for a command run man docker <command>.

OPTIONS
       --help
         Print usage statement

       --config=""
         Specifies the location of the Docker client configuration files. The default is ' /.docker'.

       -D, --debug=true|false
         Enable debug mode. Default is false.

       -H, --host=[unix:///var/run/docker.sock]: tcp://[host]:[port][path] to bind or unix://[/path/to/socket] to use.
         The socket(s) to bind to in daemon mode specified using one or more
         tcp://host:port/path, unix:///path/to/socket, fd://* or fd://socketfd.
         If the tcp port is not specified, then it will default to either 2375 when
         --tls is off, or 2376 when --tls is on, or --tlsverify is specified.

       -l, --log-level="debug|info|warn|error|fatal"
         Set the logging level. Default is info.

       --tls=true|false
         Use TLS; implied by --tlsverify. Default is false.

       --tlscacert= /.docker/ca.pem
         Trust certs signed only by this CA.

       --tlscert= /.docker/cert.pem
         Path to TLS certificate file.

       --tlskey= /.docker/key.pem
         Path to TLS key file.

       --tlsverify=true|false
         Use TLS and verify the remote (daemon: verify client, client: verify daemon).
[root@localhost ~]#man dokcer-run
DOCKER(1)                                               JUNE 2014                                               DOCKER(1)

NAME
       docker-run - Run a command in a new container

SYNOPSIS
       docker  run  [-a|--attach[=[]]]  [--add-host[=[]]]  [--blkio-weight[=[BLKIO-WEIGHT]]] [--blkio-weight-device[=[]]]
       [--cpu-shares[=0]]  [--cap-add[=[]]]   [--cap-drop[=[]]]   [--cgroup-parent[=CGROUP-PATH]]   [--cidfile[=CIDFILE]]
       [--cpu-period[=0]]  [--cpu-quota[=0]]  [--cpuset-cpus[=CPUSET-CPUS]]  [--cpuset-mems[=CPUSET-MEMS]]  [-d|--detach]
       [--detach-keys[=[]]] [--device[=[]]] [--device-read-bps[=[]]] [--device-read-iops[=[]]]  [--device-write-bps[=[]]]
       [--device-write-iops[=[]]] [--dns[=[]]] [--dns-opt[=[]]] [--dns-search[=[]]] [-e|--env[=[]]] [--entrypoint[=ENTRY‐
       POINT]] [--env-file[=[]]] [--expose[=[]]] [--group-add[=[]]]  [-h|--hostname[=HOSTNAME]]  [--help]  [-i|--interac‐
       tive]  [--ip[=IPv4-ADDRESS]]  [--ip6[=IPv6-ADDRESS]]  [--ipc[=IPC]] [--isolation[=default]] [--kernel-memory[=KER‐
       NEL-MEMORY]]  [-l|--label[=[]]]  [--label-file[=[]]]  [--link[=[]]]   [--link-local-ip[=[]]]   [--log-driver[=[]]]
       [--log-opt[=[]]]  [-m|--memory[=MEMORY]] [--mac-address[=MAC-ADDRESS]] [--memory-reservation[=MEMORY-RESERVATION]]
       [--memory-swap[=LIMIT]] [--memory-swappiness[=MEMORY-SWAPPINESS]] [--name[=NAME]]  [--network-alias[=[]]]  [--net‐
       work[="bridge"]] [--oom-kill-disable] [--oom-score-adj[=0]] [-P|--publish-all] [-p|--publish[=[]]] [--pid[=[PID]]]
       [--userns[=[]]] [--pids-limit[=PIDS_LIMIT]] [--privileged]  [--read-only]  [--restart[=RESTART]]  [--rm]  [--secu‐
       rity-opt[=[]]]     [--storage-opt[=[]]]     [--stop-signal[=SIGNAL]]     [--shm-size[=[]]]    [--sig-proxy[=true]]
       [--sysctl[=[]]] [-t|--tty] [--tmpfs[=[CONTAINER-DIR[:<OPTIONS>]]] [-u|--user[=USER]] [--ulimit[=[]]]  [--uts[=[]]]
       [-v|--volume[=[[HOST-DIR:]CONTAINER-DIR[:OPTIONS]]]]        [--volume-driver[=DRIVER]]       [--volumes-from[=[]]]
       [-w|--workdir[=WORKDIR]] IMAGE [COMMAND] [ARG...]

DESCRIPTION
       Run a process in a new container. docker run starts a process with its own file system, its  own  networking,  and
       its  own isolated process tree. The IMAGE which starts the process may define defaults related to the process that
       will be run in the container, the networking to expose, and more, but docker run gives final control to the opera‐
       tor or administrator who starts the container from the image. For that reason docker run has more options than any
       other Docker command.

       If the IMAGE is not already loaded then docker run will pull the IMAGE,  and  all  image  dependencies,  from  the
       repository in the same way running docker pull IMAGE, before it starts the container from that image.

OPTIONS
       -a, --attach=[]
          Attach to STDIN, STDOUT or STDERR.

       In  foreground  mode (the default when -d is not specified), docker run can start the process in the container and
       attach the console to the process's standard input, output, and standard error. It can even pretend to  be  a  TTY
       (this  is  what  most commandline executables expect) and pass along signals. The -a option can be set for each of
       stdin, stdout, and stderr.

       --add-host=[]
          Add a custom host-to-IP mapping (host:ip)

       Add a line to /etc/hosts. The format is hostname:ip.  The --add-host option can be set multiple times.

       --blkio-weight=0
          Block IO weight (relative weight) accepts a weight value between 10 and 1000.

       --blkio-weight-device=[]
          Block IO weight (relative device weight, format: DEVICE_NAME:WEIGHT).



dockerインストール

OS:centos7
前提:yum -y updateを実行済み

[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# systemctl mask firewalld.service
Created symlink from /etc/systemd/system/firewalld.service to /dev/null.
[root@localhost ~]#
[root@localhost ~]# yum -y install docker
読み込んだプラグイン:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ docker.x86_64 2:1.12.6-32.git88a4867.el7.centos を インストール
--> 依存性の処理をしています: docker-common = 2:1.12.6-32.git88a4867.el7.centos のパッケージ: 2:docker-1.12.6-32.git88a4867.el7.centos.x86_64
--> 依存性の処理をしています: docker-client = 2:1.12.6-32.git88a4867.el7.centos のパッケージ: 2:docker-1.12.6-32.git88a4867.el7.centos.x86_64
--> 依存性の処理をしています: oci-systemd-hook >= 1:0.1.4-9 のパッケージ: 2:docker-1.12.6-32.git88a4867.el7.centos.x86_64
--> 依存性の処理をしています: oci-register-machine >= 1:0-3.10 のパッケージ: 2:docker-1.12.6-32.git88a4867.el7.centos.x86_64
--> 依存性の処理をしています: container-selinux >= 2:2.12-2 のパッケージ: 2:docker-1.12.6-32.git88a4867.el7.centos.x86_64
--> 依存性の処理をしています: skopeo-containers のパッケージ: 2:docker-1.12.6-32.git88a4867.el7.centos.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ container-selinux.noarch 2:2.19-2.1.el7 を インストール
---> パッケージ docker-client.x86_64 2:1.12.6-32.git88a4867.el7.centos を インストール
---> パッケージ docker-common.x86_64 2:1.12.6-32.git88a4867.el7.centos を インストール
---> パッケージ oci-register-machine.x86_64 1:0-3.11.gitdd0daef.el7 を インストール
---> パッケージ oci-systemd-hook.x86_64 1:0.1.7-4.gite533efa.el7 を インストール
---> パッケージ skopeo-containers.x86_64 1:0.1.20-2.el7 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

=============================================================================================================================
 Package                          アーキテクチャー   バージョン                                     リポジトリー        容量
=============================================================================================================================
インストール中:
 docker                           x86_64             2:1.12.6-32.git88a4867.el7.centos              extras              14 M
依存性関連でのインストールをします:
 container-selinux                noarch             2:2.19-2.1.el7                                 extras              28 k
 docker-client                    x86_64             2:1.12.6-32.git88a4867.el7.centos              extras             3.2 M
 docker-common                    x86_64             2:1.12.6-32.git88a4867.el7.centos              extras              77 k
 oci-register-machine             x86_64             1:0-3.11.gitdd0daef.el7                        extras             1.0 M
 oci-systemd-hook                 x86_64             1:0.1.7-4.gite533efa.el7                       extras              30 k
 skopeo-containers                x86_64             1:0.1.20-2.el7                                 extras             7.8 k

トランザクションの要約
=============================================================================================================================
インストール  1 パッケージ (+6 個の依存関係のパッケージ)

総ダウンロード容量: 19 M
インストール容量: 64 M
Downloading packages:
(1/7): container-selinux-2.19-2.1.el7.noarch.rpm                                                      |  28 kB  00:00:00
(2/7): oci-systemd-hook-0.1.7-4.gite533efa.el7.x86_64.rpm                                             |  30 kB  00:00:00
(3/7): skopeo-containers-0.1.20-2.el7.x86_64.rpm                                                      | 7.8 kB  00:00:00
(4/7): docker-common-1.12.6-32.git88a4867.el7.centos.x86_64.rpm                                       |  77 kB  00:00:00
(5/7): oci-register-machine-0-3.11.gitdd0daef.el7.x86_64.rpm                                          | 1.0 MB  00:00:01
(6/7): docker-client-1.12.6-32.git88a4867.el7.centos.x86_64.rpm                                       | 3.2 MB  00:00:02
(7/7): docker-1.12.6-32.git88a4867.el7.centos.x86_64.rpm                                              |  14 MB  00:00:39
-----------------------------------------------------------------------------------------------------------------------------
合計                                                                                         477 kB/s |  19 MB  00:00:39
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  インストール中          : 2:docker-common-1.12.6-32.git88a4867.el7.centos.x86_64                                       1/7
  インストール中          : 2:docker-client-1.12.6-32.git88a4867.el7.centos.x86_64                                       2/7
  インストール中          : 1:skopeo-containers-0.1.20-2.el7.x86_64                                                      3/7
  インストール中          : 1:oci-register-machine-0-3.11.gitdd0daef.el7.x86_64                                          4/7
  インストール中          : 2:container-selinux-2.19-2.1.el7.noarch                                                      5/7
  インストール中          : 1:oci-systemd-hook-0.1.7-4.gite533efa.el7.x86_64                                             6/7
Stopping containers...
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
"docker stop" requires at least 1 argument(s).
See 'docker stop --help'.

Usage:  docker stop [OPTIONS] CONTAINER [CONTAINER...]

Stop one or more running containers
  インストール中          : 2:docker-1.12.6-32.git88a4867.el7.centos.x86_64                                              7/7
  検証中                  : 2:docker-1.12.6-32.git88a4867.el7.centos.x86_64                                              1/7
  検証中                  : 1:oci-systemd-hook-0.1.7-4.gite533efa.el7.x86_64                                             2/7
  検証中                  : 2:container-selinux-2.19-2.1.el7.noarch                                                      3/7
  検証中                  : 2:docker-common-1.12.6-32.git88a4867.el7.centos.x86_64                                       4/7
  検証中                  : 1:oci-register-machine-0-3.11.gitdd0daef.el7.x86_64                                          5/7
  検証中                  : 1:skopeo-containers-0.1.20-2.el7.x86_64                                                      6/7
  検証中                  : 2:docker-client-1.12.6-32.git88a4867.el7.centos.x86_64                                       7/7

インストール:
  docker.x86_64 2:1.12.6-32.git88a4867.el7.centos

依存性関連をインストールしました:
  container-selinux.noarch 2:2.19-2.1.el7                       docker-client.x86_64 2:1.12.6-32.git88a4867.el7.centos
  docker-common.x86_64 2:1.12.6-32.git88a4867.el7.centos        oci-register-machine.x86_64 1:0-3.11.gitdd0daef.el7
  oci-systemd-hook.x86_64 1:0.1.7-4.gite533efa.el7              skopeo-containers.x86_64 1:0.1.20-2.el7

完了しました!
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# docker -v
Docker version 1.12.6, build 88a4867/1.12.6
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# systemctl enable docker.service
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@localhost ~]# systemctl start docker.service
[root@localhost ~]#