コンテナ内に入って作業しよう

コンテナ内のcentosに入ります。

vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker run -i -t centos /bin/bash
[root@83a948642dbe /]# touch hello.text
[root@83a948642dbe /]# ls
anaconda-post.log dev hello.text lib media opt root sbin sys usr
bin etc home lib64 mnt proc run srv tmp var
[root@83a948642dbe /]# exit
exit

vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
83a948642dbe centos “/bin/bash” 2 minutes ago Exited (0) About a minute ago elated_swartz
45e5501bd8ef centos “free -s 3” 10 minutes ago Up 10 minutes compassionate_mccarthy
0b8011965ea0 centos “echo ‘hello world'” 19 minutes ago Exited (0) 19 minutes ago romantic_kapitsa
c6490f99e041 hello-world “/hello” About an hour ago Exited (0) About an hour ago distracted_wilson
1abf8bdb8845 hello-world “/hello” About an hour ago Exited (0) About an hour ago sad_colden

sudo docker commitでcontainerからimageを作る。
vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker commit 83a
sha256:ab93f4f1679d8efa1843606ea3d8741b35fe3f9b73e1ddb8acbfbd2ddf25f468
vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ab93f4f1679d 35 seconds ago 200MB
hello-world latest 4ab4c602aa5e 2 days ago 1.84kB
centos latest 5182e96772bf 4 weeks ago 200MB
vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker run -i -t ab9 /bin/bash
[root@5e92b8233188 /]# ls
anaconda-post.log dev hello.text lib media opt root sbin sys usr
bin etc home lib64 mnt proc run srv tmp var

なるほどなるほど。

コンテナを走らせよう

vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker run centos echo “hello world”
hello world

何!? どういう事? 

vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker run centos echo "hello world"
hello world

docker psで一覧を見ることができる。
vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker ps

動作が終了したものを見るのは docker ps -a
vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0b8011965ea0 centos “echo ‘hello world'” 5 minutes ago Exited (0) 5 minutes ago romantic_kapitsa
c6490f99e041 hello-world “/hello” 40 minutes ago Exited (0) 40 minutes ago distracted_wilson
1abf8bdb8845 hello-world “/hello” 40 minutes ago Exited (0) 40 minutes ago sad_colden

vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker run -d centos free -s 3
45e5501bd8ef3b3dd8ec4a717e992550222cb68743d78bcc4dd88db5f3673cba
vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
45e5501bd8ef centos “free -s 3” 11 seconds ago Up 10 seconds compassionate_mccarthy

$ sudo docker logs 45e
total used free shared buff/cache available
Mem: 501708 155076 82484 416 264148 325451
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155068 82484 416 264156 325467
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155068 82484 416 264156 325467
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155060 82484 416 264164 325467
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155056 82484 416 264168 325479
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155048 82484 416 264176 325479
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155048 82484 416 264176 325487
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155040 82484 416 264184 325487
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155164 82360 416 264184 325363
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155156 82360 416 264192 325363
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155156 82360 416 264192 325363
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155148 82360 416 264200 325387
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155148 82360 416 264200 325387
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155136 82360 416 264212 325387
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155136 82360 416 264212 325387
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155128 82360 416 264220 325387
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155128 82360 416 264220 325407
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155120 82360 416 264228 325415
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155120 82360 416 264228 325415
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155112 82360 416 264236 325415
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155496 81616 416 264596 325039
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155484 81616 416 264608 325047
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155484 81616 416 264608 325047
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155476 81616 416 264616 325047
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155228 81864 416 264616 325307
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155228 81856 416 264624 325299
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155232 81852 416 264624 325295
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155172 81844 416 264692 325363
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155172 81844 416 264692 325363
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155164 81844 416 264700 325371
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155160 81844 416 264704 325371
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155152 81844 416 264712 325383
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155152 81844 416 264712 325383
Swap: 0 0 0

こんなのやった記憶ねーぞ

docker pull

docker pullでDocker Indexからimageを持ってくる
Containerができる。docker runで実行する。
docker commitでImageを作る。
docker pushでDocker indexにimageが入る。
あ、docker indexはdocker hub

docker hub

コマンドラインからもsearchできる。
vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker search centos | more

Imageが取得できたかどうかは、sudo docker imagesで確認できる。
vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 4ab4c602aa5e 2 days ago 1.84kB
centos latest 5182e96772bf 4 weeks ago 200MB

sudo docker inspectで詳細を見ることができる。
vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker inspect centos:latest
[
{
“Id”: “sha256:5182e96772bf11f4b912658e265dfe0db8bd314475443b6434ea708784192892”,
“RepoTags”: [
“centos:latest”
],
“RepoDigests”: [
“centos@sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf”
],
“Parent”: “”,
“Comment”: “”,
“Created”: “2018-08-06T19:21:48.235227329Z”,
“Container”: “d60ffc9ddd12462af4bdcdbe45b74f3b3f99b46607ada80c3ed877b7def84250”,
“ContainerConfig”: {
“Hostname”: “d60ffc9ddd12”,
“Domainname”: “”,
“User”: “”,
“AttachStdin”: false,
“AttachStdout”: false,
“AttachStderr”: false,
“Tty”: false,
“OpenStdin”: false,
“StdinOnce”: false,
“Env”: [
“PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”
],
“Cmd”: [
“/bin/sh”,
“-c”,
“#(nop) “,
“CMD [\”/bin/bash\”]”
],
“ArgsEscaped”: true,
“Image”: “sha256:748eacc0f236df2fc9ba87c4d76a66cb10742120387e99e2acdb9454915c841d”,
“Volumes”: null,
“WorkingDir”: “”,
“Entrypoint”: null,
“OnBuild”: null,
“Labels”: {
“org.label-schema.build-date”: “20180804”,
“org.label-schema.license”: “GPLv2”,
“org.label-schema.name”: “CentOS Base Image”,
“org.label-schema.schema-version”: “1.0”,
“org.label-schema.vendor”: “CentOS”
}
},
“DockerVersion”: “17.06.2-ce”,
“Author”: “”,
“Config”: {
“Hostname”: “”,
“Domainname”: “”,
“User”: “”,
“AttachStdin”: false,
“AttachStdout”: false,
“AttachStderr”: false,
“Tty”: false,
“OpenStdin”: false,
“StdinOnce”: false,
“Env”: [
“PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”
],
“Cmd”: [
“/bin/bash”
],
“ArgsEscaped”: true,
“Image”: “sha256:748eacc0f236df2fc9ba87c4d76a66cb10742120387e99e2acdb9454915c841d”,
“Volumes”: null,
“WorkingDir”: “”,
“Entrypoint”: null,
“OnBuild”: null,
“Labels”: {
“org.label-schema.build-date”: “20180804”,
“org.label-schema.license”: “GPLv2”,
“org.label-schema.name”: “CentOS Base Image”,
“org.label-schema.schema-version”: “1.0”,
“org.label-schema.vendor”: “CentOS”
}
},
“Architecture”: “amd64”,
“Os”: “linux”,
“Size”: 199716491,
“VirtualSize”: 199716491,
“GraphDriver”: {
“Data”: {
“DeviceId”: “7”,
“DeviceName”: “docker-8:1-263323-d5a0c73632bbe71e2ab66b6f6d04da6bca10076e1f9f787ba6adcf10844a9b67”,
“DeviceSize”: “10737418240”
},
“Name”: “devicemapper”
},
“RootFS”: {
“Type”: “layers”,
“Layers”: [
“sha256:1d31b5806ba40b5f67bde96f18a181668348934a44c9253b420d5f04cfb4e37a”
]
},
“Metadata”: {
“LastTagTime”: “0001-01-01T00:00:00Z”
}
}
]

set up the repository for docker

公式を参考にします。
https://docs.docker.com/install/linux/docker-ce/ubuntu/#install-from-a-package

$ sudo apt-get update

$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common

vagrant@vagrant-ubuntu-trusty-64:~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –
OK

vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt-key fingerprint 0EBFCD88
/etc/apt/trusted.gpg
——————–
pub 1024D/437D05B5 2004-09-12
Key fingerprint = 6302 39CC 130E 1A7F D81A 27B1 4097 6EAF 437D 05B5
uid Ubuntu Archive Automatic Signing Key
sub 2048g/79164387 2004-09-12

pub 1024D/FBB75451 2004-12-30
Key fingerprint = C598 6B4F 1257 FFA8 6632 CBA7 4618 1433 FBB7 5451
uid Ubuntu CD Image Automatic Signing Key

pub 4096R/C0B21F32 2012-05-11
Key fingerprint = 790B C727 7767 219C 42C8 6F93 3B4F E6AC C0B2 1F32
uid Ubuntu Archive Automatic Signing Key (2012)

pub 4096R/EFE21092 2012-05-11
Key fingerprint = 8439 38DF 228D 22F7 B374 2BC0 D94A A3F0 EFE2 1092
uid Ubuntu CD Image Automatic Signing Key (2012)

pub 4096R/0EBFCD88 2017-02-22
Key fingerprint = 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid Docker Release (CE deb)
sub 4096R/F273FCD8 2017-02-22

sudo add-apt-repository \
“deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable”

さあ、遂にdockerを入れていきます。
$ sudo apt-get install docker-ce

vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker run hello-world

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/

vagrant@vagrant-ubuntu-trusty-64:~$ docker –version
Docker version 18.06.1-ce, build e68fc7a

OKKKKKKKKKKKKKKKKKKKKKKKKKKKK!

ubuntuを入れていこう

まずvagrant box
https://www.vagrantbox.es/

まず、trustyをひっぱってくる
vagrant box add trusty64 https://github.com/sepetrov/trusty64/releases/download/v0.0.5/trusty64.box

cd trusty

vagrant init trusty64

vagrant file line54
config.vm.network “private_network”, ip: “192.168.55.44”

login as: vagrant
vagrant@192.168.55.44’s password:
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-100-generic x86_64)

* Documentation: https://help.ubuntu.com/

System information as of Mon Sep 10 00:10:54 UTC 2018

System load: 0.57 Processes: 75
Usage of /: 3.5% of 39.34GB Users logged in: 0
Memory usage: 26% IP address for eth0: 10.0.2.15
Swap usage: 0% IP address for eth1: 192.168.55.44

Graph this data and manage this system at:
https://landscape.canonical.com/

Get cloud support with Ubuntu Advantage Cloud Guest:
http://www.ubuntu.com/business/services/cloud

New release ‘16.04.5 LTS’ available.
Run ‘do-release-upgrade’ to upgrade to it.

OK!

apt-get updateする。
vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt-get update
vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt-get update
Ign http://archive.ubuntu.com trusty InRelease
Get:1 http://security.ubuntu.com trusty-security InRelease [65.9 kB]
Get:2 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB]
Get:3 http://security.ubuntu.com trusty-security/main Sources [162 kB]
Get:4 http://archive.ubuntu.com trusty-backports InRelease [65.9 kB]
Get:5 http://security.ubuntu.com trusty-security/universe Sources [80.4 kB]
Hit http://archive.ubuntu.com trusty Release.gpg
Get:6 http://security.ubuntu.com trusty-security/main amd64 Packages [765 kB]
Get:7 http://archive.ubuntu.com trusty-updates/main Sources [422 kB]
Get:8 http://security.ubuntu.com trusty-security/universe amd64 Packages [246 kB]
Get:9 http://archive.ubuntu.com trusty-updates/restricted Sources [6,322 B]
Get:10 http://archive.ubuntu.com trusty-updates/universe Sources [208 kB]
Get:11 http://archive.ubuntu.com trusty-updates/multiverse Sources [7,441 B]
Get:12 http://security.ubuntu.com trusty-security/main Translation-en [413 kB]
Get:13 http://archive.ubuntu.com trusty-updates/main amd64 Packages [1,101 kB]
Get:14 http://security.ubuntu.com trusty-security/universe Translation-en [134 kB]
Get:15 http://archive.ubuntu.com trusty-updates/restricted amd64 Packages [17.2 kB]
Get:16 http://archive.ubuntu.com trusty-updates/universe amd64 Packages [474 kB]
Get:17 http://archive.ubuntu.com trusty-updates/multiverse amd64 Packages [14.6 kB]
Get:18 http://archive.ubuntu.com trusty-updates/main Translation-en [547 kB]
Get:19 http://archive.ubuntu.com trusty-updates/multiverse Translation-en [7,616 B]
Get:20 http://archive.ubuntu.com trusty-updates/restricted Translation-en [4,021 B]
Get:21 http://archive.ubuntu.com trusty-updates/universe Translation-en [253 kB]
Get:22 http://archive.ubuntu.com trusty-backports/main Sources [9,709 B]
Get:23 http://archive.ubuntu.com trusty-backports/restricted Sources [28 B]
Get:24 http://archive.ubuntu.com trusty-backports/universe Sources [35.4 kB]
Get:25 http://archive.ubuntu.com trusty-backports/multiverse Sources [1,896 B]
Get:26 http://archive.ubuntu.com trusty-backports/main amd64 Packages [13.3 kB]
Get:27 http://archive.ubuntu.com trusty-backports/restricted amd64 Packages [28 B]
Get:28 http://archive.ubuntu.com trusty-backports/universe amd64 Packages [43.1 kB]
Get:29 http://archive.ubuntu.com trusty-backports/multiverse amd64 Packages [1,567 B]
Get:30 http://archive.ubuntu.com trusty-backports/main Translation-en [7,503 B]
Get:31 http://archive.ubuntu.com trusty-backports/multiverse Translation-en [1,215 B]
Get:32 http://archive.ubuntu.com trusty-backports/restricted Translation-en [28 B]
Get:33 http://archive.ubuntu.com trusty-backports/universe Translation-en [36.8 kB]
Hit http://archive.ubuntu.com trusty Release
Get:34 http://archive.ubuntu.com trusty/main Sources [1,064 kB]
Get:35 http://archive.ubuntu.com trusty/restricted Sources [5,433 B]
Get:36 http://archive.ubuntu.com trusty/universe Sources [6,399 kB]
Get:37 http://archive.ubuntu.com trusty/multiverse Sources [174 kB]
Hit http://archive.ubuntu.com trusty/main amd64 Packages
Hit http://archive.ubuntu.com trusty/restricted amd64 Packages
Hit http://archive.ubuntu.com trusty/universe amd64 Packages
Hit http://archive.ubuntu.com trusty/multiverse amd64 Packages
Hit http://archive.ubuntu.com trusty/main Translation-en
Hit http://archive.ubuntu.com trusty/multiverse Translation-en
Hit http://archive.ubuntu.com trusty/restricted Translation-en
Hit http://archive.ubuntu.com trusty/universe Translation-en
Ign http://archive.ubuntu.com trusty/main Translation-en_US
Ign http://archive.ubuntu.com trusty/multiverse Translation-en_US
Ign http://archive.ubuntu.com trusty/restricted Translation-en_US
Ign http://archive.ubuntu.com trusty/universe Translation-en_US
Fetched 12.9 MB in 50s (255 kB/s)
Reading package lists… Done

なんだ、Docker CEって。。

あ、CE…無償のコミュニティエディション
なるほど

ubuntu14.04(trusty)にDockerをインストール

vagrantのubuntu14.04(trusty)にDockerをインストールします。

まず、パッケージマネージャーをアップデートしておきます。

sudo apt-get update

そして、Dockerの最新を取得します。

wget -qO- https://get.docker.com/ | sh

文末にアラートが表示されるので、ユーザーをDockerグループに追加します。

If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:

  sudo usermod -aG docker vagrant

Remember that you will have to log out and back in for this to take effect!
vagrant@vagrant-ubuntu-trusty-64:~$ sudo usermod -aG docker vagrant

では、Dockerが入っているか確認してみましょう。大丈夫そうですね。

vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c04b14da8d14: Pull complete
Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
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.
 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 Hub account:
 https://hub.docker.com

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker --version
Docker version 1.12.3, build 6b644ec

Dockerを動かすためにvagrantにUbuntuをインストール

Dockerは軽量な仮想環境で、OSやアプリケーションを別の場所に簡単にもっていくことができます。Build once, run anywhere と呼ばれたりします。

Docker

Dockerはubuntu上で開発されているので、環境は、ubuntuをvagrantでインストールして、そこで作業していきましょう。ということで、vagrantのフォルダに移動し、コマンドラインからubuntuを入れます。

まず、vagrantから、boxesからtrustyをコピーします。
http://www.vagrantbox.es/
a

そして、vagrantのフォルダからコマンドラインで以下のように打ち込みます。

C:\Users\username\MyVagrant>vagrant box add trusty64 https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box

インストールが終わったら、Dockerのファイルを作り、vagrant initをしましょう。

C:\Users\username\MyVagrant>mkdir Docker
C:\Users\username\MyVagrant>cd Docker
C:\Users\username\MyVagrant\Docker>vagrant init trusty64

そすると、vagrant fileが作られるので、vagrant fileの中で、以下のようにipのコメントを外して、任意のipアドレスを設定しましょう。ここでは、192.168.55.44としてます。

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.55.44"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

その後、vagrant upとし、Ubuntu sshで接続完了です。

login as: vagrant
vagrant@192.168.55.44's password:
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-100-generic x86_64)

* Documentation: https://help.ubuntu.com/

System information disabled due to load higher than 1.0

Get cloud support with Ubuntu Advantage Cloud Guest:
http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.