方針:virtualbox上のcentos6.9に導入する。
■1.導入
1.os最新化
yum update
2.dockerインストール
yum install epel-release
sudo tee /etc/yum.repos.d/docker.repo <<- font="">
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/$releasever/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF->
yum install docker-engine
3.dockerサービス起動
service docker start
4.docker composeのインストール
sh -c "curl -L https://github.com/docker/compose/releases/download/1.5.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose"
chmod +x /usr/local/bin/docker-compose
参照
https://qiita.com/is_mgmt_dept/items/f4a7c64d72c2cba440c2
■2.基本操作
以下を参考にさわってみる。
https://www.ogis-ri.co.jp/otc/hiroba/technical/docker/part1.html
操作の概要
---
•イメージの取得(pull)
•イメージの一覧表示(images)
•コンテナのライフサイクル管理 ◦作成(create)
◦起動(start)
◦停止(stop)
◦削除(rm)
•コンテナの一覧表示(ps)
---
###イメージ一覧###
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
新しいメールが /var/spool/mail/root にあります
###バージョン確認###
[root@localhost ~]# docker -v
Docker version 1.7.1, build 786b29d
[root@localhost ~]# docker-compose -v
docker-compose version 1.5.2, build 7240ff3
###動作中のコンテナ一覧###
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
###全コンテナ一覧(停止中含)###
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
■■3.hello-worldを実行
[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from hello-world
882673a3c694: Pull complete
83f0de727d85: Pull complete
Digest: sha256:4555e23a9cf5a1a216bd8b0d71b08a25e4144c2ecf6adb26df9620245ba99529
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
新しいメールが /var/spool/mail/root にあります
###全コンテナ一覧(停止中含)###
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dfc38761efe1 hello-world "/hello" 3 minutes ago Exited (0) 3 minutes ago pensive_einstein
新しいメールが /var/spool/mail/root にあります
###イメージ一覧###
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
hello-world latest 83f0de727d85 3 weeks ago 1.848 kB
■■Alpine Linuxの操作
Alpine Linuxコンテナ向けに開発されている軽量Linuxディストリビューション
###イメージの取得###
[root@localhost ~]# docker pull alpine
latest: Pulling from alpine
28a0c8bbcab3: Pull complete
9797e5e798a0: Pull complete
Digest: sha256:ed8059bd90dd8cd6b7bfddedc7bba0f7555f766a00daf6a6efc86fa3339c09ef
Status: Downloaded newer image for alpine:latest
###イメージの確認###
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
hello-world latest 83f0de727d85 3 weeks ago 1.848 kB
alpine latest 9797e5e798a0 3 months ago 4.143 MB
[root@localhost ~]#
###alpineイメージからコンテナを起動し、echoコマンドを実行###
[root@localhost ~]# docker run alpine echo "hello from alpine"
hello from alpine
[root@localhost ~]#
###対話操作を可能にするコマンド "-it"を付与###
###プロンプトが"/ #"となり"ls"コマンドの入力が可能###
[root@localhost ~]# docker run -it alpine bin/sh
/ # ls
bin etc lib mnt root sbin sys usr
dev home media proc run srv tmp var
###別ウィンドウで動作中のコンテナ一覧###
###alpineがbin/shを実行###
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5dc3c0d3f46a alpine "bin/sh" 45 seconds ago Up 44 seconds
### コンテナaplineを停止###
[root@localhost ~]# docker stop 5dc3c0d3f46a
5dc3c0d3f46a
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
###同タイミングで別ウィンドウ上のプロンプトは
/ #
から
[root@localhost ~]#
となる。
### コンテナaplineを開始###
[root@localhost ~]# docker start 5dc3c0d3f46a
5dc3c0d3f46a
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5dc3c0d3f46a alpine "bin/sh" 5 minutes ago Up 8 seconds clever_brown
##コンテナ操作を再開するにはattachを使用###
[root@localhost ~]# docker attach 5dc3c0d3f46a
/ #
なお、Ctrl+Cを実行しても
/ #
から
[root@localhost ~]#
へ変更できなかった。
■削除
https://www.ogis-ri.co.jp/otc/hiroba/technical/docker/part1.html
の通り、コンテナを削除
その後はコンテナの起動ができないことを確認
[root@localhost ~]# docker rm 5dc3c0d3f46a
5dc3c0d3f46a
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
hello-world latest 83f0de727d85 3 weeks ago 1.848 kB
alpine latest 9797e5e798a0 3 months ago 4.143 MB
[root@localhost ~]# docker start 5dc3c0d3f46a
Error response from daemon: no such id: 5dc3c0d3f46a
Error: failed to start containers: [5dc3c0d3f46a]
■■後片付け
##導入したイメージalpineとコンテナalpineを削除###
###コンテナの確認##
###alpineは消えたと思ったのになぜかある###
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
24fe1e1924b0 alpine "echo 'hello from al 11 minutes ago Exited (0) 11 minutes ago high_torvalds
dfc38761efe1 hello-world "/hello" 26 minutes ago Exited (0) 26 minutes ago pensive_einstein
###イメージの確認###
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
hello-world latest 83f0de727d85 3 weeks ago 1.848 kB
alpine latest 9797e5e798a0 3 months ago 4.143 MB
###まずはイメージから削除###
###-fを付けない場合、削除できなかった###
###IDは IMAGE IDを指定###
[root@localhost ~]# docker rmi -f 9797e5e798a0
Untagged: alpine:latest
Deleted: 9797e5e798a034d53525968de25bd25c913e7bb17c6d068ebc778cb33e3ff6e5
Deleted: 28a0c8bbcab32237452c3dadfb8302a6fab4f6064be2d858add06a7be8c32924
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
hello-world latest 83f0de727d85 3 weeks ago 1.848 kB
###続いてコンテナの削除###
##削除前:alpineあり###
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
24fe1e1924b0 alpine "echo 'hello from al 21 minutes ago Exited (0) 21 minutes ago high_torvalds
dfc38761efe1 hello-world "/hello" 36 minutes ago Exited (0) 36 minutes ago pensive_einstein
##削除実行:rmの後は container id を指定###
[root@localhost ~]# docker rm 24fe1e1924b0
24fe1e1924b0
##削除後:alpineなし###
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dfc38761efe1 hello-world "/hello" 36 minutes ago Exited (0) 36 minutes ago pensive_einstein
イメージとコンテナの削除方法参照
https://qiita.com/tifa2chan/items/e9aa408244687a63a0ae