はじめに
以下の書籍の演習をやったので、そのまとめ。
Docker初心者でも実際に手を動かしながら、Dockerの概念や基本を学ぶことが出来るので良本だと思います。演習のホストOSはCentOS7で試しています。
書名:Dockerによるアプリケーション開発環境構築ガイド
著者:櫻井 洋一郎, 村崎 大輔 (著)
関連
[part2] Dockerfile記述方法
[part3] Baseimage-dockerを使った1コンテナでの複数サービス管理
Docekrのインストール
# yum install docker
# systemctl start docker
# docker version
Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-103.git7f2769b.el7.centos.x86_64
Go version: go1.10.3
Git commit: 7f2769b/1.13.1
Built: Sun Sep 15 14:06:47 2019
OS/Arch: linux/amd64
Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Package version: docker-1.13.1-103.git7f2769b.el7.centos.x86_64
Go version: go1.10.3
Git commit: 7f2769b/1.13.1
Built: Sun Sep 15 14:06:47 2019
OS/Arch: linux/amd64
Experimental: false
インストール後のテスト
# docker run hello-world
Unable to find image 'hello-world:latest' locally
Trying to pull repository docker.io/library/hello-world ...
latest: Pulling from docker.io/library/hello-world
1b930d010525: Pull complete
Digest: sha256:4df8ca8a7e309c256d60d7971ea14c27672fc0d10c5f303856d7bc48f8cc17ff
Status: Downloaded newer image for docker.io/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/get-started/
docker-composeのインストール
実行ファイルをダウンロード
# curl -L https://github.com/docker/compose/releases/download/1.24.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 617 0 617 0 0 1070 0 --:--:-- --:--:-- --:--:-- 1071
100 15.4M 100 15.4M 0 0 616k 0 0:00:25 0:00:25 --:--:-- 624k
# chmod +x /usr/local/bin/docker-compose
# ll /usr/local/bin/docker-compose
-rwxr-xr-x. 1 root root 16168192 Nov 24 22:16 /usr/local/bin/docker-compose
Dockerコマンド利用例
Dockerイメージの取得 pull
# docker pull ubuntu:16.04
Trying to pull repository docker.io/library/ubuntu ...
16.04: Pulling from docker.io/library/ubuntu
e80174c8b43b: Pull complete
d1072db285cc: Pull complete
858453671e67: Pull complete
3d07b1124f98: Pull complete
Digest: sha256:bb5b48c7750a6a8775c74bcb601f7e5399135d0a06de004d000e05fd25c1a71c
Status: Downloaded newer image for docker.io/ubuntu:16.04
ローカルのイメージ一覧の確認 images
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu 16.04 5f2bf26e3524 3 weeks ago 123 MB
docker.io/hello-world latest fce289e99eb9 10 months ago 1.84 kB
DockerイメージからDockerコンテナの作成
# docker run -d -it ubuntu:16.04 bash
d070e9c506d47474590a0ce250ee420f6d6d729d75b0fa1a7f9ad7932fc7c269
-d, --detach Run container in background and print container ID
-i, --interactive Keep STDIN open even if not attached
-t, --tty Allocate a pseudo-TTY
Dockerコンテナ一覧の確認 ps
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d070e9c506d4 ubuntu:16.04 "bash" About a minute ago Up About a minute amazing_noether
Dockerコンテナ上でコマンド実行 exec
# docker exec -it d070e9c506d4 bash
root@d070e9c506d4:/# apt-get update && apt-get install -y php
root@d070e9c506d4:/# php --version
PHP 7.0.33-0ubuntu0.16.04.7 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.33-0ubuntu0.16.04.7, Copyright (c) 1999-2017, by Zend Technologies
root@d070e9c506d4:/# exit
exit
Dockerコンテナの現在の状態からイメージを作成 commit
# docker tag 351b1877a1a0 sample/ubuntu_with_php:1_0
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sample/ubuntu_with_php 1_0 351b1877a1a0 7 minutes ago 206 MB
docker.io/ubuntu 16.04 5f2bf26e3524 3 weeks ago 123 MB
###作成したイメージをファイルに出力&ローカルからイメージ削除した後に、ファイルから復元する
Dockerイメージのファイルへの保存 save
# docker save -o /tmp/ubuntu_with_php_1_0 sample/ubuntu_with_php:1_0
# ll /tmp/ubuntu_with_php_1_0
-rw-------. 1 root root 211963904 Nov 25 00:53 /tmp/ubuntu_with_php_1_0
Dockerイメージの削除 rmi
# docker rmi sample/ubuntu_with_php:1_0
Untagged: sample/ubuntu_with_php:1_0
Deleted: sha256:351b1877a1a0d82cd6626fa0e65512d4b275ca532f3e7164d95c3e05452ad151
Deleted: sha256:9e2f42e8210d03f5bb53cd86bf4a69822f0983a0c29920b68f7601de6c10180c
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu 16.04 5f2bf26e3524 3 weeks ago 123 MB
docker.io/hello-world latest fce289e99eb9 10 months ago 1.84 kB
Dockerイメージをファイルから読み込む load
# docker load < /tmp/ubuntu_with_php_1_0
08526c63bb5b: Loading layer [==================================================>] 85.31 MB/85.31 MB
Loaded image: sample/ubuntu_with_php:1_0
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sample/ubuntu_with_php 1_0 351b1877a1a0 22 minutes ago 206 MB
docker.io/ubuntu 16.04 5f2bf26e3524 3 weeks ago 123 MB
docker.io/hello-world latest fce289e99eb9 10 months ago 1.84 kB
###ホスト環境とコンテナ間ででファイルのコピー cp
# touch samplefile
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d070e9c506d4 ubuntu:16.04 "bash" 32 minutes ago Up 32 minutes amazing_noether
# docker cp samplefile d070e9c506d4:/
[root@testvm ~]# docker exec d070e9c506d4 bash -c 'ls -al / | grep sample'
-rw-r--r--. 1 root root 0 Nov 25 01:09 samplefile
[root@testvm ~]# docker cp d070e9c506d4:/samplefile /tmp
[root@testvm ~]# ls /tmp/ | grep sample
samplefile
###コンテナの停止、起動、強制停止、削除
####コンテナの停止 stop
[root@testvm ~]# docker stop d070e9c506d4
d070e9c506d4
[root@testvm ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d070e9c506d4 ubuntu:16.04 "bash" 38 minutes ago Exited (0) 27 seconds ago amazing_noether
981377322c09 hello-world "/hello" 3 hours ago Exited (0) 3 hours ago xenodochial_hugle```
####コンテナの起動 start
[root@testvm ~]# docker start d070e9c506d4
d070e9c506d4
[root@testvm ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d070e9c506d4 ubuntu:16.04 "bash" 39 minutes ago Up 4 seconds amazing_noether
981377322c09 hello-world "/hello" 3 hours ago Exited (0) 3 hours ago xenodochial_hugle
####コンテナの強制停止 kill
[root@testvm ~]# docker kill d070e9c506d4
d070e9c506d4
[root@testvm ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d070e9c506d4 ubuntu:16.04 "bash" 39 minutes ago Exited (137) 2 seconds ago amazing_noether
981377322c09 hello-world "/hello" 3 hours ago Exited (0) 3 hours ago xenodochial_hugle
####コンテナの削除 rm
[root@testvm ~]# docker rm d070e9c506d4
d070e9c506d4
[root@testvm ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
981377322c09 hello-world "/hello" 3 hours ago Exited (0) 3 hours ago xenodochial_hugle