Think Twice

Memorandum

Hello docker 〜その1

f:id:mix-juice001:20150708225218j:plain

dockerでexecutable-jarを動かすためにやったこと

boot2docker のインストール

Get Started with Dockerに従ってインストール

インストールが完了すると、VirtualBox上にboot2docker-vmというLinux仮想マシンが作成され、
そこにsshでホストOSのterminalからアクセスし、dockerのコマンドを発行できるようになる。

docker 準備

$ boot2docker init
$ boot2docker up
Waiting for VM and Docker daemon to start...
...............ooooooooo
Started.

To connect the Docker client to the Docker daemon, please set:
    export DOCKER_HOST=tcp://192.168.59.103:2375
    unset DOCKER_CERT_PATH
    unset DOCKER_TLS_VERIFY

Linux仮想マシンがまだ作成されていない場合は boot2docker init で作成できる。
boot2docker up で仮想マシンの起動する。

環境変数をセットするように教えてくれるのでそれに従う。

$ docker version
Client version: 1.7.0
Client API version: 1.19
Go version (client): go1.4.2
Git commit (client): 0baf609
OS/Arch (client): darwin/amd64
Server version: 1.7.0
Server API version: 1.19
Go version (server): go1.4.2
Git commit (server): 0baf609
OS/Arch (server): linux/amd64

docker を無事に使えることを確認。

docker お試し

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from hello-world
a8219747be10: Pull complete
91c95931e552: Already exists
hello-world:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Digest: sha256:aa03e5d0d5553b4c3473e89c8619cf79df368babd18681cf5daeb82aab55838d
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.
    (Assuming it was not already locally available.)
 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

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

最初はdocker image をダウンロードするのに時間がかかるが、2回目以降はすぐに起動する。

整いましたので、本題へ。

Dockerfile の作成

dockerで動かすのに必要なものだけを置くディレクトリを作成し、その中にDockerfileを配置.
executable-jarも同じ場所に配置

docker-myApplication
 ├─Dockerfile
 └─myApplication.jar

FROM ubuntu:14.10

# Setup
RUN apt-get install -y software-properties-common debconf-utils
RUN add-apt-repository -y ppa:webupd8team/java
RUN apt-get update

# Java8
RUN echo "oracle-java8-installer shared/accepted-oracle-license-v1-1 boolean true" | debconf-set-selections
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get install -y oracle-java8-installer

CMD ["java", "-version"]

EXPOSE 8080

ADD ./myApplication.jar /myApplication.jar

CMD ["java", "-jar", "/myApplication.jar"]

docker image の作成

$ docker build -t myApplication ./

これも初回は時間がかかるが、2回目以降はcacheをつかうので早くなる。

container 起動

$ docker run myApplication

とっても簡単!

でも、このままではブラウザでアクセスできない。

docker run -P  myApplication 

-P でポートを利用できるようになる。
docker ps で確認。

$ docker ps
CONTAINER ID        IMAGE                    COMMAND                CREATED             STATUS              PORTS                     NAMES
b443330c4d66        myApplication   "java -jar /tmp/fa-s   13 hours ago        Up 14 seconds       0.0.0.0:32768->8080/tcp   elegant_feynman

上記の場合だと
http://localhost:32768
でアクセスできる。

もしくはdockerコンテナのipアドレスを調べて

$ docker inspect -f '{{ .NetworkSettings.IPAddress }}' b443330c4d66
172.17.0.3

http://172.17.0.3:8080
でもアクセスできる。

ちょっとはまったところ

TLSつかってないじゃないのと言われる

Post http:///var/run/docker.sock/v1.19/containers/create: dial unix /var/run/docker.sock: no such file or directory. Are you trying to connect to a TLS-enabled daemon without TLS?

/var/lib/boot2docker/profileを作成し、DOCKER_TLS=noを記載する。

$ boot2docker ssh
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 1.7.0, build master : 7960f90 - Thu Jun 18 18:31:45 UTC 2015
Docker version 1.7.0, build 0baf609
docker@boot2docker:~$ cat /var/lib/boot2docker/profile
DOCKER_TLS=no
docker@boot2docker:~$

dial tcp うんたらかんたらと言われる

Get https://index.docker.io/v1/repositories/training/webapp/images: dial tcp: lookup index.docker.io on 192.168.0.1:53: read udp 192.168.0.1:53: i/o timeout

boot2docker を再起動

$ boot2docker restart