今更ですが、、Dockerの基本的なことをまとめる。
Dockerとは
Dockerが必要となった背景
・アプリケーションは作ったら終わりではなく、ユーザの要望を取り込み価値のあるアプリケーションに改善していく必要がある
・ユーザ要望が多様化し、変化のスピードも早くなっているため、迅速かつ柔軟なIT基盤が求められるようになってきた
・継続的インテグレーション時の仮想マシンでの問題を、Dockerは解決できる
特徴
・アプリケーションの実行環境をコード管理できる
・アプリケーション開発者も環境をすぐに用意することができる
(サーバ管理者にサーバ構築してもらう必要がなくなる)
・本番環境に追加した設定がテスト環境には追加されていない、を回避しやすくなる
・ベースOSのカーネルや機能を利用するため、仮想マシンと比較してオーバーヘッドが少なくなり、リソース消費量(メモリやディスク容量)が少なくなる
・作業が効率化される(構築時間がとても早くなる)
・仮想マシンとは違って、ハードウェアやカーネルに影響するアプリケーションは利用できない場合がある
操作方法
Docker for Windows で操作する。
まずは、windows10へのdockerインストールにしたがって、Dockerをインストールした。
ubuntuにnginxをインストールして、ブラウザで閲覧できるようにする
### Dockerレジストリ(リポジトリ)から、コンテナイメージを検索する ###
### 公式で、ubuntuを含むコンテナイメージを検索している ###
> docker search --no-trunc --filter is-official=true ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating system based on free software. 7991 [OK]
ubuntu-upstart Upstart is an event-based replacement for the /sbin/init daemon which starts processes at boot 87 [OK]
neurodebian NeuroDebian provides neuroscience research software for Debian, Ubuntu, and other derivatives. 50 [OK]
ubuntu-debootstrap debootstrap --variant=minbase --components=main,universe --include=inetutils-ping,iproute2 <suite> / 39 [OK]
### コンテナイメージをダウンロードする ###
### ubuntuの最新バージョンをダウンロードする ###
### もしDockerホストのubuntuのバージョンが古くても、Linuxカーネルは後方互換性があるため基本的には問題ない ###
> docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
6b98dfc16071: Pull complete
4001a1209541: Pull complete
6319fc68c576: Pull complete
b24603670dc3: Pull complete
97f170c87c6f: Pull complete
Digest: sha256:5f4bdc3467537cbbe563e80db2c3ec95d548a9145d64453b06939c4592d67b6d
Status: Downloaded newer image for ubuntu:latest
### ダウンロードしたコンテナイメージを表示する ###
> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 113a43faa138 5 weeks ago 81.2MB
### Dockerコンテナを起動して、nginxをインストールする ###
### 最初にapt-get updateしないとエラーとなる ###
### docker run オプション ###
### -h hostname ホスト名を指定することが可能※デフォルトではコンテナIDと同じ名前となる ###
### --cpuset-cpus 0-3 --memory 4096m などで利用するCPUやメモリ数を指定することが可能 ###
> docker run -it --name ubuntu-container ubuntu
root@b6ec500d4fd7:/# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04 LTS"
root@b6ec500d4fd7:/# apt-get install nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package nginx
root@b6ec500d4fd7:/# apt-get update && apt-get upgrade
...
root@b6ec500d4fd7:/# apt-get install nginx
...
root@b6ec500d4fd7:/# /etc/init.d/nginx status
* nginx is not running
root@b6ec500d4fd7:/# /etc/init.d/nginx start
* Starting nginx nginx [ OK ]
root@b6ec500d4fd7:/# ps afx
PID TTY STAT TIME COMMAND
1 pts/0 Ss 0:00 /bin/bash
920 ? Ss 0:00 nginx: master process /usr/sbin/nginx
921 ? S 0:00 \_ nginx: worker process
922 ? S 0:00 \_ nginx: worker process
924 pts/0 R+ 0:00 ps afx
root@b6ec500d4fd7:/# exit
exit
### コンテナイメージを作成する ###
> docker commit ubuntu-container akatuki/ubuntu-nginx
sha256:507f4bf357e5a0bcb5df74d50e51fea31778fcc6f0f17938baf977aeb6db38d0
> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
akatuki/ubuntu-nginx latest 507f4bf357e5 5 seconds ago 196MB
ubuntu latest 113a43faa138 5 weeks ago 81.2MB
### 80番ポートを利用してコンテナを起動する ###
> docker run -itd -p 80:80 --name ubuntu-nginx-container akatuki/ubuntu-nginx
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
76c54b34974b akatuki/ubuntu-nginx "/bin/bash" 29 seconds ago Up 27 seconds 0.0.0.0:80->80/tcp ubuntu-nginx-container
> docker attach ubuntu-nginx-container
root@76c54b34974b:/# /etc/init.d/nginx start
* Starting nginx nginx
### [Ctl]+[P]+[Q]でコンテナを終了せずに、PowerShellに戻ることが可能 ###
### exitするとPID1の /bin/bash が終了する、つまりコンテナが終了する ###
root@76c54b34974b:/# read escape sequence
localhostにアクセスすると、nginxのページが表示される。

Dockerfileを利用した場合
# Dockerfile FROM ubuntu MAINTAINER akatuki RUN apt-get update && apt-get upgrade && apt-get install nginx CMD service nginx start && bash
> docker build -t akatuki/ubuntu-nginx-2 .\docker_ubuntu_nginx\ Sending build context to Docker daemon 2.048kB Step 1/4 : FROM ubuntu ---> 113a43faa138 Step 2/4 : MAINTAINER akatuki ---> Running in 16fcf202048b Removing intermediate container 16fcf202048b ---> 216a90a83370 Step 3/4 : RUN apt-get update && apt-get -y upgrade && apt-get install -y nginx ---> Running in 33c5c837b636 ... Removing intermediate container 33c5c837b636 ---> 3e4fc279fc09 Step 4/4 : CMD service nginx start && bash ---> Running in f71c691629b7 Removing intermediate container f71c691629b7 ---> 1bae7466b72c Successfully built 1bae7466b72c Successfully tagged akatuki/ubuntu-nginx-2:latest SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories. > docker images REPOSITORY TAG IMAGE ID CREATED SIZE akatuki/ubuntu-nginx-2 latest 1bae7466b72c About a minute ago 194MB > docker run -itd -p 80:80 --name ubuntu-nginx-container-2 akatuki/ubuntu-nginx-2
その他便利なコマンド
### dockerのバージョンを確認する ###
> docker version
Client:
Version: 18.03.1-ce
API version: 1.37
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:12:48 2018
OS/Arch: windows/amd64
Experimental: false
Orchestrator: swarm
Server:
Engine:
Version: 18.03.1-ce
API version: 1.37 (minimum version 1.12)
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:22:38 2018
OS/Arch: linux/amd64
Experimental: true
### docker実行環境を確認する ###
> docker info
Containers: 2
Running: 1
Paused: 0
Stopped: 1
Images: 2
Server Version: 18.03.1-ce
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 773c489c9c1b21a6d78b5c538cd395416ec50f88
runc version: 4fc53a81fb7c994640722ac585fa9ca548971871
init version: 949e6fa
Security Options:
seccomp
Profile: default
Kernel Version: 4.9.87-linuxkit-aufs
Operating System: Docker for Windows
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.934GiB
Name: linuxkit-00155d011c1d
ID: IHML:MLXN:VWM4:BN3Q:QYWT:QJC4:QVAO:UD5Q:ZS3M:DQC7:2NVH:EP34
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): true
File Descriptors: 27
Goroutines: 48
System Time: 2018-07-16T14:06:28.8408325Z
EventsListeners: 1
Registry: https://index.docker.io/v1/
Labels:
Experimental: true
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
### コンテナの詳細情報を取得する ###
> docker inspect ubuntu-nginx-container
[
{
"Id": "76c54b34974ba7c0cba4aade1875f280517e27588796b96831290386fc12e813",
"Created": "2018-07-16T13:58:33.5983827Z",
"Path": "/bin/bash",
"Args": [],
...
### コンテナ内で変更があったディレクトリとファイルを表示する ###
> docker diff ubuntu-nginx-container
C /root/.bash_history
C /run/nginx.pid
C /var/log/nginx/access.log
C /var/log/nginx/error.log
### コンテナのプロセスを確認する ###
> docker top ubuntu-nginx-container
PID USER TIME COMMAND
14521 root 0:00 /bin/bash
14671 root 0:00 nginx: master process /usr/sbin/nginx
14672 xfs 0:00 nginx: worker process
14673 xfs 0:00 nginx: worker process
### コンテナ内部で実行された標準出力、標準エラー出力への内容を確認する ###
> docker logs ubuntu-nginx-container
root@76c54b34974b:/# /etc/init.d/nginx start
* Starting nginx nginx
### コンテナイメージの中間イメージの履歴を表示する ###
> docker history akatuki/ubuntu-nginx
IMAGE CREATED CREATED BY SIZE COMMENT
507f4bf357e5 30 minutes ago /bin/bash 115MB
113a43faa138 5 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 5 weeks ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B
<missing> 5 weeks ago /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$… 2.76kB
<missing> 5 weeks ago /bin/sh -c rm -rf /var/lib/apt/lists/* 0B
<missing> 5 weeks ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 745B
<missing> 5 weeks ago /bin/sh -c #(nop) ADD file:28c0771e44ff530db… 81.1MB
### コンテナを出力する ###
> docker export ubuntu-nginx-container > ubuntu-nginx-container.img
補足1 – コンテナ管理について
複数アプリケーション利用時のコンテナ数について
・アプリケーションごとにコンテナを分割がベストプラクティス
・コンテナを作り直す際に、設定変更内容が明確になるため
コンテナのスナップショット
・コンテナ起動時にスナップショットを作成する
停止時はスナップショットを保持する
削除時にスナップショットを削除する
補足2 – すべてのコンテナイメージを削除する
> docker rmi -f $(docker images -aq)
参考
今、なぜ「Docker」なのか
Docker実践入門――Linuxコンテナ技術の基礎から応用まで
docker コマンド チートシート
docker初心者の方が知っておいた方がよい基礎知識
docker-compose upするとコンテナが一瞬でexited with code 1する話