「Docker」の版間の差分

提供: Ubuntu入門
移動: 案内検索
(関連項目)
(関連項目)
行347: 行347:
 
* [[Dockerコンテナのメモリ容量を制限する]]
 
* [[Dockerコンテナのメモリ容量を制限する]]
 
* [[Dockerコンテナをすべて削除する方法]]
 
* [[Dockerコンテナをすべて削除する方法]]
 +
* [[Dockerコンテナ内でプロセスを実行する方法]]
 
<!-- vim: fileencoding=utf-8 filetype=mediawiki
 
<!-- vim: fileencoding=utf-8 filetype=mediawiki
 
-->
 
-->

2015年5月2日 (土) 19:16時点における版

Docker とは、Linux用のアプリケーション実行環境のコンテナです。Dockerは、Go言語で開発され、Linuxの機能を利用します。

読み方

Docker
どっかー

概要

Docker 0.9からは、LXC(Linux Containers)に依存しません。libcontainerドライバが実装され、LXCなしに稼働できます。

  • Docker
    • libcontainer
    • libvirt
    • lxc
    • systemd-nspawn
  • Linux
    • cgroups
    • namespaces
    • netlink
    • selinux
    • capabilities
    • apparmor

インストール

$ sudo apt install docker.io

設定

 

使い方

docker コマンド

docker/docker.ioは、Dockerを操作するコマンドです。

docker.ioは、dockerコマンドのシンボリックリンクです。

$ ls -li /usr/bin/docker*
529144 -rwxr-xr-x 1 root 15240483  822  2014 /usr/bin/docker*
529146 lrwxrwxrwx 1 root        6  822  2014 /usr/bin/docker.io -> docker*

コマンドラインオプション

Usage of docker.io:
  -D, --debug=false: Enable debug mode
  -G, --group="docker": Group to assign the unix socket specified by -H when running in daemon mode; use '' (the empty string) to disable setting of a group
  -H, --host=[]: tcp://host:port, unix://path/to/socket, fd://* or fd://socketfd to use in daemon mode. Multiple sockets can be specified
  --api-enable-cors=false: Enable CORS headers in the remote API
  -b, --bridge="": Attach containers to a pre-existing network bridge; use 'none' to disable container networking
  --bip="": Use this CIDR notation address for the network bridge's IP, not compatible with -b
  -d, --daemon=false: Enable daemon mode
  --dns=[]: Force docker to use specific DNS servers
  -e, --exec-driver="native": Force the docker runtime to use a specific exec driver
  -g, --graph="/var/lib/docker": Path to use as the root of the docker runtime
  --icc=true: Enable inter-container communication
  --ip="0.0.0.0": Default IP address to use when binding container ports
  --ip-forward=true: Disable enabling of net.ipv4.ip_forward
  --iptables=true: Disable docker's addition of iptables rules
  --mtu=0: Set the containers network MTU; if no value is provided: default to the default route MTU or 1500 if no default route is available
  -p, --pidfile="/var/run/docker.pid": Path to use for daemon PID file
  -r, --restart=true: Restart previously running containers
  -s, --storage-driver="": Force the docker runtime to use a specific storage driver
  -v, --version=false: Print version information and quit
[2]    20100 exit 2     docker.io -h

docker run

sudo docker run -i ubuntu /bin/bash

dockerのイメージを確認する

$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              13.10               5e019ab7bf6d        4 weeks ago         180 MB
ubuntu              saucy               5e019ab7bf6d        4 weeks ago         180 MB
ubuntu              precise             74fe38d11401        4 weeks ago         209.6 MB
ubuntu              12.04               74fe38d11401        4 weeks ago         209.6 MB
ubuntu              12.10               a7cf8ae4e998        4 weeks ago         171.3 MB
ubuntu              quantal             a7cf8ae4e998        4 weeks ago         171.3 MB
ubuntu              latest              99ec81b80c55        4 weeks ago         266 MB
ubuntu              14.04               99ec81b80c55        4 weeks ago         266 MB
ubuntu              trusty              99ec81b80c55        4 weeks ago         266 MB
ubuntu              13.04               316b678ddf48        4 weeks ago         169.4 MB
ubuntu              raring              316b678ddf48        4 weeks ago         169.4 MB
ubuntu              10.04               3db9c44f4520        5 weeks ago         183 MB
ubuntu              lucid               3db9c44f4520        5 weeks ago         183 MB

Dockerfile

Dockerfile の例

FROM ubuntu
RUN apt-get install -y nginx

docker build

docker build のときに、 -t オプションでイメージに名前をつけることができます。

$ sudo docker build .
[sudo] password for kaworu:
Uploading context 4.096 kB
Uploading context
Step 0 : FROM ubuntu
 ---> 99ec81b80c55
Step 1 : RUN apt-get install -y nginx
 ---> Running in e2d26d470085
Reading package lists...
Building dependency tree...
Reading state information...
The following extra packages will be installed:
  fontconfig-config fonts-dejavu-core geoip-database libfontconfig1
  libfreetype6 libgd3 libgeoip1 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5
  libvpx1 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4
  libxslt1.1 nginx-common nginx-core sgml-base xml-core
Suggested packages:
  libgd-tools geoip-bin fcgiwrap nginx-doc sgml-base-doc debhelper
The following NEW packages will be installed:
  fontconfig-config fonts-dejavu-core geoip-database libfontconfig1
  libfreetype6 libgd3 libgeoip1 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5
  libvpx1 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4
  libxslt1.1 nginx nginx-common nginx-core sgml-base xml-core
0 upgraded, 25 newly installed, 0 to remove and 0 not upgraded.
Need to get 5612 kB of archives.
After this operation, 19.8 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main libgeoip1 amd64 1.6.0-1 [71.0 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ trusty/main libxau6 amd64 1:1.0.8-1 [8376 B]
Get:3 http://archive.ubuntu.com/ubuntu/ trusty/main libxdmcp6 amd64 1:1.1.1-1 [12.8 kB]
Get:4 http://archive.ubuntu.com/ubuntu/ trusty/main libxcb1 amd64 1.10-2ubuntu1 [38.0 kB]
Get:5 http://archive.ubuntu.com/ubuntu/ trusty/main libx11-data all 2:1.6.2-1ubuntu2 [111 kB]
Get:6 http://archive.ubuntu.com/ubuntu/ trusty/main libx11-6 amd64 2:1.6.2-1ubuntu2 [560 kB]
Get:7 http://archive.ubuntu.com/ubuntu/ trusty/main libxml2 amd64 2.9.1+dfsg1-3ubuntu4 [571 kB]
Get:8 http://archive.ubuntu.com/ubuntu/ trusty/main sgml-base all 1.26+nmu4ubuntu1 [12.5 kB]
Get:9 http://archive.ubuntu.com/ubuntu/ trusty/main fonts-dejavu-core all 2.34-1ubuntu1 [1024 kB]
Get:10 http://archive.ubuntu.com/ubuntu/ trusty/main fontconfig-config all 2.11.0-0ubuntu4 [47.3 kB]
Get:11 http://archive.ubuntu.com/ubuntu/ trusty/main libfreetype6 amd64 2.5.2-1ubuntu2 [305 kB]
Get:12 http://archive.ubuntu.com/ubuntu/ trusty/main libfontconfig1 amd64 2.11.0-0ubuntu4 [123 kB]
Get:13 http://archive.ubuntu.com/ubuntu/ trusty/main libjpeg-turbo8 amd64 1.3.0-0ubuntu2 [104 kB]
Get:14 http://archive.ubuntu.com/ubuntu/ trusty/main libjpeg8 amd64 8c-2ubuntu8 [2194 B]
Get:15 http://archive.ubuntu.com/ubuntu/ trusty/main libjbig0 amd64 2.0-2ubuntu4 [25.9 kB]
Get:16 http://archive.ubuntu.com/ubuntu/ trusty/main libtiff5 amd64 4.0.3-7 [142 kB]
Get:17 http://archive.ubuntu.com/ubuntu/ trusty/main libvpx1 amd64 1.3.0-2 [556 kB]
Get:18 http://archive.ubuntu.com/ubuntu/ trusty/main libxpm4 amd64 1:3.5.10-1 [38.3 kB]
Get:19 http://archive.ubuntu.com/ubuntu/ trusty/main libgd3 amd64 2.1.0-3 [147 kB]
Get:20 http://archive.ubuntu.com/ubuntu/ trusty/main libxslt1.1 amd64 1.1.28-2build1 [145 kB]
Get:21 http://archive.ubuntu.com/ubuntu/ trusty/main geoip-database all 20140313-1 [1196 kB]
Get:22 http://archive.ubuntu.com/ubuntu/ trusty/main xml-core all 0.13+nmu2 [23.3 kB]
Get:23 http://archive.ubuntu.com/ubuntu/ trusty/main nginx-common all 1.4.6-1ubuntu3 [18.1 kB]
Get:24 http://archive.ubuntu.com/ubuntu/ trusty/main nginx-core amd64 1.4.6-1ubuntu3 [324 kB]
Get:25 http://archive.ubuntu.com/ubuntu/ trusty/main nginx all 1.4.6-1ubuntu3 [5586 B]
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
dpkg-preconfigure: unable to re-open stdin:
Fetched 5612 kB in 46s (121 kB/s)
Selecting previously unselected package libgeoip1:amd64.
(Reading database ... 11518 files and directories currently installed.)
Preparing to unpack .../libgeoip1_1.6.0-1_amd64.deb ...
Unpacking libgeoip1:amd64 (1.6.0-1) ...
Selecting previously unselected package libxau6:amd64.
Preparing to unpack .../libxau6_1%3a1.0.8-1_amd64.deb ...
Unpacking libxau6:amd64 (1:1.0.8-1) ...
Selecting previously unselected package libxdmcp6:amd64.
Preparing to unpack .../libxdmcp6_1%3a1.1.1-1_amd64.deb ...
Unpacking libxdmcp6:amd64 (1:1.1.1-1) ...
Selecting previously unselected package libxcb1:amd64.
Preparing to unpack .../libxcb1_1.10-2ubuntu1_amd64.deb ...
Unpacking libxcb1:amd64 (1.10-2ubuntu1) ...
Selecting previously unselected package libx11-data.
Preparing to unpack .../libx11-data_2%3a1.6.2-1ubuntu2_all.deb ...
Unpacking libx11-data (2:1.6.2-1ubuntu2) ...
Selecting previously unselected package libx11-6:amd64.
Preparing to unpack .../libx11-6_2%3a1.6.2-1ubuntu2_amd64.deb ...
Unpacking libx11-6:amd64 (2:1.6.2-1ubuntu2) ...
Selecting previously unselected package libxml2:amd64.
Preparing to unpack .../libxml2_2.9.1+dfsg1-3ubuntu4_amd64.deb ...
Unpacking libxml2:amd64 (2.9.1+dfsg1-3ubuntu4) ...
Selecting previously unselected package sgml-base.
Preparing to unpack .../sgml-base_1.26+nmu4ubuntu1_all.deb ...
Unpacking sgml-base (1.26+nmu4ubuntu1) ...
Selecting previously unselected package fonts-dejavu-core.
Preparing to unpack .../fonts-dejavu-core_2.34-1ubuntu1_all.deb ...
Unpacking fonts-dejavu-core (2.34-1ubuntu1) ...
Selecting previously unselected package fontconfig-config.
Preparing to unpack .../fontconfig-config_2.11.0-0ubuntu4_all.deb ...
Unpacking fontconfig-config (2.11.0-0ubuntu4) ...
Selecting previously unselected package libfreetype6:amd64.
Preparing to unpack .../libfreetype6_2.5.2-1ubuntu2_amd64.deb ...
Unpacking libfreetype6:amd64 (2.5.2-1ubuntu2) ...
Selecting previously unselected package libfontconfig1:amd64.
Preparing to unpack .../libfontconfig1_2.11.0-0ubuntu4_amd64.deb ...
Unpacking libfontconfig1:amd64 (2.11.0-0ubuntu4) ...
Selecting previously unselected package libjpeg-turbo8:amd64.
Preparing to unpack .../libjpeg-turbo8_1.3.0-0ubuntu2_amd64.deb ...
Unpacking libjpeg-turbo8:amd64 (1.3.0-0ubuntu2) ...
Selecting previously unselected package libjpeg8:amd64.
Preparing to unpack .../libjpeg8_8c-2ubuntu8_amd64.deb ...
Unpacking libjpeg8:amd64 (8c-2ubuntu8) ...
Selecting previously unselected package libjbig0:amd64.
Preparing to unpack .../libjbig0_2.0-2ubuntu4_amd64.deb ...
Unpacking libjbig0:amd64 (2.0-2ubuntu4) ...
Selecting previously unselected package libtiff5:amd64.
Preparing to unpack .../libtiff5_4.0.3-7_amd64.deb ...
Unpacking libtiff5:amd64 (4.0.3-7) ...
Selecting previously unselected package libvpx1:amd64.
Preparing to unpack .../libvpx1_1.3.0-2_amd64.deb ...
Unpacking libvpx1:amd64 (1.3.0-2) ...
Selecting previously unselected package libxpm4:amd64.
Preparing to unpack .../libxpm4_1%3a3.5.10-1_amd64.deb ...
Unpacking libxpm4:amd64 (1:3.5.10-1) ...
Selecting previously unselected package libgd3:amd64.
Preparing to unpack .../libgd3_2.1.0-3_amd64.deb ...
Unpacking libgd3:amd64 (2.1.0-3) ...
Selecting previously unselected package libxslt1.1:amd64.
Preparing to unpack .../libxslt1.1_1.1.28-2build1_amd64.deb ...
Unpacking libxslt1.1:amd64 (1.1.28-2build1) ...
Selecting previously unselected package geoip-database.
Preparing to unpack .../geoip-database_20140313-1_all.deb ...
Unpacking geoip-database (20140313-1) ...
Selecting previously unselected package xml-core.
Preparing to unpack .../xml-core_0.13+nmu2_all.deb ...
Unpacking xml-core (0.13+nmu2) ...
Selecting previously unselected package nginx-common.
Preparing to unpack .../nginx-common_1.4.6-1ubuntu3_all.deb ...
Unpacking nginx-common (1.4.6-1ubuntu3) ...
Selecting previously unselected package nginx-core.
Preparing to unpack .../nginx-core_1.4.6-1ubuntu3_amd64.deb ...
Unpacking nginx-core (1.4.6-1ubuntu3) ...
Selecting previously unselected package nginx.
Preparing to unpack .../nginx_1.4.6-1ubuntu3_all.deb ...
Unpacking nginx (1.4.6-1ubuntu3) ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up libgeoip1:amd64 (1.6.0-1) ...
Setting up libxau6:amd64 (1:1.0.8-1) ...
Setting up libxdmcp6:amd64 (1:1.1.1-1) ...
Setting up libxcb1:amd64 (1.10-2ubuntu1) ...
Setting up libx11-data (2:1.6.2-1ubuntu2) ...
Setting up libx11-6:amd64 (2:1.6.2-1ubuntu2) ...
Setting up libxml2:amd64 (2.9.1+dfsg1-3ubuntu4) ...
Setting up sgml-base (1.26+nmu4ubuntu1) ...
Setting up fonts-dejavu-core (2.34-1ubuntu1) ...
Setting up fontconfig-config (2.11.0-0ubuntu4) ...
Setting up libfreetype6:amd64 (2.5.2-1ubuntu2) ...
Setting up libfontconfig1:amd64 (2.11.0-0ubuntu4) ...
Setting up libjpeg-turbo8:amd64 (1.3.0-0ubuntu2) ...
Setting up libjpeg8:amd64 (8c-2ubuntu8) ...
Setting up libjbig0:amd64 (2.0-2ubuntu4) ...
Setting up libtiff5:amd64 (4.0.3-7) ...
Setting up libvpx1:amd64 (1.3.0-2) ...
Setting up libxpm4:amd64 (1:3.5.10-1) ...
Setting up libgd3:amd64 (2.1.0-3) ...
Setting up libxslt1.1:amd64 (1.1.28-2build1) ...
Setting up geoip-database (20140313-1) ...
Setting up xml-core (0.13+nmu2) ...
Setting up nginx-common (1.4.6-1ubuntu3) ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up nginx-core (1.4.6-1ubuntu3) ...
invoke-rc.d: policy-rc.d denied execution of start.
Setting up nginx (1.4.6-1ubuntu3) ...
Processing triggers for libc-bin (2.19-0ubuntu6) ...
Processing triggers for sgml-base (1.26+nmu4ubuntu1) ...
 ---> f5c6635a7494
Successfully built f5c6635a7494
Removing intermediate container e2d26d470085

docker tag

docker build で作成したイメージは名前がないので、 docker tag でつけます。

$ sudo docker tag f5c6635a7494 nginx
$ sudo docker images|head -2
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
nginx               latest              f5c6635a7494        17 minutes ago      284.4 MB

docker build のときに-tオプションで名前を指定することもできます。

イメージを確認する

$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
<none>              <none>              f5c6635a7494        16 minutes ago      284.4 MB
ubuntu              13.10               5e019ab7bf6d        4 weeks ago         180 MB
ubuntu              saucy               5e019ab7bf6d        4 weeks ago         180 MB
ubuntu              12.04               74fe38d11401        4 weeks ago         209.6 MB
ubuntu              precise             74fe38d11401        4 weeks ago         209.6 MB
ubuntu              quantal             a7cf8ae4e998        4 weeks ago         171.3 MB
ubuntu              12.10               a7cf8ae4e998        4 weeks ago         171.3 MB
ubuntu              trusty              99ec81b80c55        4 weeks ago         266 MB
ubuntu              14.04               99ec81b80c55        4 weeks ago         266 MB
ubuntu              latest              99ec81b80c55        4 weeks ago         266 MB
ubuntu              13.04               316b678ddf48        4 weeks ago         169.4 MB
ubuntu              raring              316b678ddf48        4 weeks ago         169.4 MB
ubuntu              10.04               3db9c44f4520        5 weeks ago         183 MB
ubuntu              lucid               3db9c44f4520        5 weeks ago         183 MB

dockerでmemcachedを動かす

Dockerfile.memcached

FROM ubuntu
RUN apt-get install -y memcached
ENTRYPOINT ["memcached"]
USER daemon
EXPOSE 11211

イメージを作成します。

sudo docker build -t memcached - < Dockerfile.memcached

コンテナを起動します。

sudo docker run -d -p 11211 memcached

11211:11211と指定すれば、ホスト側から11211でアクセスできます。

ps で11211にアクセスするためのポートが調べられます。

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                      NAMES
7b58125ba29d        memcached:latest    memcached           3 minutes ago       Up 3 minutes        0.0.0.0:49156->11211/tcp   sick_brattain

ローカルホストの 49156 にアクセスすることで、コンテナの11211のmemcachedに接続できます。 

telnet localhost 49156

コンテナを終了させるには、docker psで調べられるコンテナIDを指定します。

sudo docker stop 7b58125ba29d

Docker 用のツール

Docker Machine
仮想マシン、クラウド、マシンにDocker環境を構築する。
Docker Swarm
複数のDocker Engineをまとめて1つの仮想的なDocker Engineにみせる。
Docker Compose
複数のコンテナで構成されるアプリケーションを簡単にデプロイするオーケストレーションツール。

関連項目