ページ

ラベル Docker の投稿を表示しています。 すべての投稿を表示
ラベル Docker の投稿を表示しています。 すべての投稿を表示

2018年7月25日水曜日

CircleCI2.0上でDockerHubへPushする

概要

  • Docker Buildを行う
  • Serverspecでテストを行う
  • Docker HubへPushを行う

環境

↓環境毎のディレクトリにDockerfileとspec用のディレクトリがある
.
├── android
│   └── spec
├── gcc_cmake
│   └── spec
├── rails
│   └── spec
...
├── specfiles #spec用の共通して使用するファイル
│   └── spec
テストを行う場合、各環境のDocker Imageをビルドし、それぞれのテストをServerspecで行う。

Dockerfile

version: 2
jobs:
  build:
    working_directory: ~/workspace
    branches:
      only:
        - develop
    docker:
      - image: circleci/ruby:2.4
    steps:
      - checkout
      - setup_remote_docker

      # Download and cache dependencies
      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "Gemfile.lock" }}
          - v1-dependencies-

      # Install dependencies
      - run:
          name: bundle install
          command: bundle install --path vendor/bundle

      - save_cache:
          paths:
            - vendor/bundle
          key: v1-dependencies-{{ checksum "Gemfile.lock" }}

      # Test
      - run:
          name: run test
          command: ./run-tests.sh

      # Deploy
      - run:
          name: deploy docker images
          command: ./deploy.sh
  • ポイント
    • setup_remote_docker を設定
      • dockerやdocker-composeコマンドが使えるようになる
      • 詳しくはこちら
    • gemに関してはキャッシュしてます

テスト

run-tests.sh
#!/usr/bin/env bash

WORKDIR=$PWD

# Find target directory.
for entity in `find . -type d -mindepth 1 -maxdepth 1 -not -name ".git"`; do

  if [ $entity = "./specfiles" ]; then
    # Ignore directory
    continue
  fi

  # Only exist spec directory.
  if [ -e $entity/spec ]; then
    # Copy specfiles.
    cp $WORKDIR/specfiles/.rspec $entity/.
    cp -r $WORKDIR/specfiles/spec $entity/.

    # Build docker image.
    cd $entity
    ./docker-build.sh
    cd $WORKDIR

    # Exec rspec
    bundle exec rspec --default-path $entity/spec
  fi
done
対象となるディレクトリ配下に spec ディレクトリがある環境だけ
Docker Imageをビルドし、テストを行なっています。
sh
   cp $WORKDIR/specfiles/.rspec $entity/.
   cp -r $WORKDIR/specfiles/spec $entity/.
↑は共通して使用するテスト用のファイルを各環境へコピーしてます。

Docker Hub へ Push

deploy.sh
#!/usr/bin/env bash

docker login -u $DOCKER_USER -p $DOCKER_PASS

# Android
docker push slowhand/android:1.0

# Rails
docker push slowhand/rails:1.0

# gcc + cmake
docker push slowhand/gcc_cmake:1.0
こちらは単純にloginしてpushしているだけです。
$DOCKER_USER などの環境変数はCircleCI上で定義してます。

dockerfiles

今回書いた分は
https://github.com/Slowhand0309/dockerfiles
こちらのリポジトリで公開してます。何かツッコミ等あれば指摘頂けると助かります。

2016年11月3日木曜日

Docker Composeの利用


前回の記事で複数のコンテナを起動/停止する際に
シェルを作成し実行していましたが、
Docker Composeを使うとそこら辺をよろしくやってくれます。
Docker for Macをインストールすると一緒にインストールされます。

docker-compose.ymlの作成


前回はと言うと・・・
#!/usr/bin/env bash

DOCKER_HOME=$PWD

if [ -e $DOCKER_HOME/registry ]; then
  # 何もしない
else
  mkdir $DOCKER_HOME/registry
fi

docker run \
    -d \
    -p 5000:5000 \
    -v $DOCKER_HOME/registry:/var/lib/registry \
    --name docker_registry2 \
    registry:2

docker run \
    -d \
    -e ENV_DOCKER_REGISTRY_HOST=[ローカルPCのIPアドレス] \
    -e ENV_DOCKER_REGISTRY_PORT=5000 \
    -p 8080:80 \
    --name registry_browser \
    konradkleine/docker-registry-frontend:v2
このようなシェルを書いてました。
これをdocker-compose.ymlファイルを作成し以下のように記述します。
docker_registry2:
  image: registry:2
  ports:
    - "5000:5000"
  volumes:
    - "./registry:/var/lib/registry"
registry_browser:
  image: konradkleine/docker-registry-frontend:v2
  ports:
    - "8080:80"
  environment:
    ENV_DOCKER_REGISTRY_HOST: [ローカルPCのIPアドレス]
    ENV_DOCKER_REGISTRY_PORT: 5000
シェルとの対比は見れば分かるとは思います。
docker-compose.ymlがある場所で
$ docker-compose up -d
を実行すると依存関係なんかをよろしくやってくれて、
コンテナが起動します。
また、
$ docker-compose stop
でコンテナをまとめて終了させる事ができます。
最後に
$ docker-compose rm
でまとめてコンテナを削除します。
便利ですw

2016年10月23日日曜日

Docker for MacでプライベートなDockerHubを作る


DockerHubは沢山のimageが公開されていて便利ですが、
自分用や会社用のDockerHubを作りたい時があるかと思います。
そんな時はDockerを使ってDockerHubを作成できるので作ってみます。

環境構築


最近はLinuxに限らず、WindowsでもMacでもDockerが動きます。
今回はDocker for Macを使ってMacに環境を構築してみます。
まずは
ここ からdmgファイルをダウンロード。
インストールが完了するとステータスバーにDockerのアイコンが表示されます
最初アイコンをクリックすると↓のようなポップアップが表示され
Dockerの使用状況を改善の為に送るかどうか設定できます。
「Got it」をクリックし準備OKです。
ちゃんと動いているかターミナルから
$ docker --version
を実行し、バージョンが表示されればOKです。

Registryコンテナの取得


公式に配布されているRegistryコンテナを取得し、
プライベートなDockerHubを構築していきます。
まずはimageをpull
$ docker pull registry:2
ちゃんとpullできたかdocker imagesで確認します。
$ docker images
REPOSITORY  TAG   IMAGE ID         CREATED        SIZE
registry    2     XXXXXXXXXXXX     4 weeks ago    33.3 MB

起動&可視化


早速起動して使ってみたいと思います・・が、
その前にDockerHubみたいにブラウザで登録状況が確認できた方が
使いやすいので、可視化するコンテナの
を使います。
毎回コマンドで起動して〜停止して〜というのもきついので 以下のスクリプトを用意します。
  • run_registry.sh
#!/usr/bin/env bash

DOCKER_HOME=$PWD

if [ -e $DOCKER_HOME/registry ]; then
  # 何もしない
else
  mkdir $DOCKER_HOME/registry
fi

docker run \
    -d \
    -p 5000:5000 \
    -v $DOCKER_HOME/registry:/var/lib/registry \
    --name docker_registry2 \
    registry:2

docker run \
    -d \
    -e ENV_DOCKER_REGISTRY_HOST=[ローカルPCのIPアドレス] \
    -e ENV_DOCKER_REGISTRY_PORT=5000 \
    -p 8080:80 \
    --name registry_browser \
    konradkleine/docker-registry-frontend:v2
-v $DOCKER_HOME/registry:/var/lib/registry \では
登録されたimageの内容はコンテナ内の/var/lib/registry
書き込まれます。永続化の為ホストの$DOCKER_HOME/registry
マウントさせています。
2番目のdocker runでは可視化の為のコンテナを走らせてます。
早速起動します。
$ ./run_registry.sh
初回はimageのダウンロードで遅いかもしれません。起動ができたら
http://localhost:8080にアクセスし以下の画面が出れば成功です。

push&pull


作成したプライベートなDockerHubにimageをpushする場合は、
まずpushしたいimageを特定の名前で作成します。
例えば、jenkins:latestのイメージから名前を帰る場合、
で作成します。
例)
$ docker tag jenkins:latest localhost:5000/slowhand/jenkins:latest
あとはpushすればOKです。
$ docker push localhost:5000/slowhand/jenkins:latest
http://localhost:8080上で登録されてると思います。

逆にpullする場合は
$ docker pull localhost:5000/slowhand/jenkins:latest
でOKです。

停止


停止用のスクリプトも書いておきます。
  • stop_registry.sh
#!/usr/bin/env bash

docker stop registry_browser
docker stop docker_registry2

docker rm registry_browser
docker rm docker_registry2
以上、色々便利になりましたw

2014年11月30日日曜日

Dockerをさわってみる 3 - Dockerfile -

Dockerfileで環境作り

前回から少ししか経ってないのにもうDocker1.1が出たらしい。。早っ><;
今回はDockerfileを使って環境を作成してみる。

Dockerfileとは
  MakefileのDocker版。#がコメント。{命令}と{引数}でスペース区切りで記述。
  「docker build」する事でDockerfileの上から順に実行される。

※主な命令
{命令}用途
FROM元となるDockerイメージの指定
MAINTAINER作成者の情報
RUNコマンドの実行
ADDファイル/ディレクトリの追加
CMDコンテナーの実行コマンド 1
ENTRYPOINTコンテナーの実行コマンド 2
WORKDIR作業ディレクトリの指定
ENV環境変数の指定
USER実行ユーザーの指定
EXPOSEポートのエクスポート
VOLUMEボリュームのマウント

試しに、前回同様nginxをインストール + index.htmlを追加するDockerfileを作成してみる。
[root@localhost docker]# mkdir nginx1
[root@localhost docker]# cd nginx1/
[root@localhost nginx1]# ls
[root@localhost nginx1]# vi Dockerfile
[root@localhost nginx1]# ls
Dockerfile
[root@localhost nginx1]# echo 'hello docker' > index.html
[root@localhost nginx1]# ls
Dockerfile  index.html
[root@localhost nginx1]# 

いざ、build!!
[root@localhost nginx1]# docker build -t fuji/nginx:1.0 .
Sending build context to Docker daemon 3.584 kB
Sending build context to Docker daemon 
Step 0 : FROM ubuntu
 ---> 1357f421be38
Step 1 : MAINTAINER XXXXXX <xxxx@gmail.com>
 ---> Running in e779846d56f3
 ---> 86560b5fee44
Removing intermediate container e779846d56f3
Step 2 : RUN apt-get update
 ---> Running in a3a46568fd97
Ign http://archive.ubuntu.com trusty InRelease
Ign http://archive.ubuntu.com trusty-updates InRelease
Ign http://archive.ubuntu.com trusty-security InRelease
Ign http://archive.ubuntu.com trusty-proposed InRelease

ん〜、ビルドは最後までいったっぽいが、image名が空?
[root@localhost nginx1]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
<none>              <none>              164fa62b79b6        42 seconds ago      231.5 MB
fuji/nginx          latest              fd64b49b28ca        7 weeks ago         231.2 MB
ubuntu              14.10               b8d67033ed07        7 weeks ago         242.4 MB
ubuntu              utopic              b8d67033ed07        7 weeks ago         242.4 MB
ubuntu              14.04               1357f421be38        7 weeks ago         192.7 MB
ubuntu              14.04.1             1357f421be38        7 weeks ago         192.7 MB
ubuntu              trusty              1357f421be38        7 weeks ago         192.7 MB
ubuntu              latest              1357f421be38        7 weeks ago         192.7 MB
ubuntu              12.04               2bed76595591        7 weeks ago         114.6 MB
ubuntu              12.04.5             2bed76595591        7 weeks ago         114.6 MB
ubuntu              precise             2bed76595591        7 weeks ago         114.6 MB

一旦、IMAGE IDを指定してコンテナ作成&実行してみる。
[root@localhost nginx1]# docker run -d -p 80:80 --name nginx1 164fa62b79b6 /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf
44756c2137b2a1fdcf0b3dd06b7a8bb9b4a5b0a41f803cc8da03a5dc6e2d61ac
[root@localhost nginx1]# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                NAMES
44756c2137b2        164fa62b79b6        /usr/sbin/nginx -g '   19 seconds ago      Up 18 seconds       0.0.0.0:80->80/tcp   nginx1              
[root@localhost nginx1]# curl localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost nginx1]# 

実行はできたが、index.htmlがコピーされていないorz
よくみてみたら、Dockerfileでindex.htmlのコピー先が間違っていたorz
[root@localhost nginx1]# cat Dockerfile 
FROM ubuntu
MAINTAINER XXXXXXX <xxxx@gmail.com>
RUN apt-get update
RUN apt-get install -y nginx
ADD index.html /usr/share/nginx/html/

×「/usr/share/nginx/html」⚪︎「/usr/share/nginx/html/」

もう一度ビルドしてみたら、ちゃんとイメージができてるっぽい。
[root@localhost nginx1]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
build_nginx         1.0                 aaf400998859        10 seconds ago      231.5 MB
fuji/nginx          latest              fd64b49b28ca        7 weeks ago         231.2 MB
ubuntu              14.10               b8d67033ed07        7 weeks ago         242.4 MB
ubuntu              utopic              b8d67033ed07        7 weeks ago         242.4 MB
ubuntu              14.04.1             1357f421be38        7 weeks ago         192.7 MB
ubuntu              latest              1357f421be38        7 weeks ago         192.7 MB
ubuntu              14.04               1357f421be38        7 weeks ago         192.7 MB
ubuntu              trusty              1357f421be38        7 weeks ago         192.7 MB
ubuntu              12.04               2bed76595591        7 weeks ago         114.6 MB
ubuntu              12.04.5             2bed76595591        7 weeks ago         114.6 MB
ubuntu              precise             2bed76595591        7 weeks ago         114.6 MB

起動&確認
[root@localhost nginx1]# docker run -d -p 80:80 --name nginx1 build_nginx:1.0 /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf
21b37014f9657950c88db17d331d9075d5d8b6a9803b7534303cf32ffc2ee881
[root@localhost nginx1]# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                NAMES
21b37014f965        build_nginx:1.0     /usr/sbin/nginx -g '   4 seconds ago       Up 4 seconds        0.0.0.0:80->80/tcp   nginx1              
[root@localhost nginx1]# curl localhost:80
hello docker
[root@localhost nginx1]# 

ん〜ちゃんとできている^^
参考URL


2014年10月12日日曜日

Dockerをさわってみる 2 - Docker1.0 -

2014.06.09にDocker1.0がリリースされて色々変わっている件

いままでDockerと読んでいた本体のソフトウェアは「Docker Engine」に名称が変更になったらしい。新しいサービスとしてDocker Hubを発表。Docker indexは「Docker Hub Registory」 へ。。。

↓イメージ














詳しくはWEB で・・・
http://www.atmarkit.co.jp/ait/articles/1406/10/news031.html

今回は、コンテナ作成し色々カスタマイズしてみる。

・コンテナ作成
[root@localhost ~]# docker run -it --name ubuntu02 ubuntu /bin/bash
root@a3cccf6e59be:/# apt-get install -y nginx
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package nginx
root@a3cccf6e59be:/# exit

コンテナ作成は上手くいったが、パッケージインストールができない!!
よく分からないのでとりあえずapt-get update !!
すると、、、上手く行きましたorz

control + d でコンテナ終了させる。
docker ps -a で実行させた全てのコンテナ一覧を表示。
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
367e49fa4dc0        ubuntu:14.04        /bin/bash           3 minutes ago       Exited (0) 13 seconds ago                        ubuntu03             
a3cccf6e59be        ubuntu:14.04        /bin/bash           8 minutes ago       Exited (100) 7 minutes ago                       ubuntu02             
2510a5487997        ubuntu:14.04        /bin/bash           14 minutes ago      Exited (0) 9 minutes ago                         ubuntu01             
879b7468bb85        ubuntu:14.04        echo Hello          28 hours ago        Exited (0) 28 hours ago                          condescending_pike   
[root@localhost ~]# 

nginxをインストールしたubuntu03を元に新規イメージを作成してみる。
[root@localhost ~]# docker commit ubuntu03 fuji/nginx
fd64b49b28caf96275bbaccbd6e9bed47d76331d7ab64a851474bf3f44e246e5
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
fuji/nginx          latest              fd64b49b28ca        8 seconds ago       231.2 MB
ubuntu              14.10               b8d67033ed07        32 hours ago        242.4 MB
ubuntu              utopic              b8d67033ed07        32 hours ago        242.4 MB
ubuntu              14.04.1             1357f421be38        32 hours ago        192.7 MB
ubuntu              latest              1357f421be38        32 hours ago        192.7 MB
ubuntu              trusty              1357f421be38        32 hours ago        192.7 MB
ubuntu              14.04               1357f421be38        32 hours ago        192.7 MB
ubuntu              12.04               2bed76595591        32 hours ago        114.6 MB
ubuntu              12.04.5             2bed76595591        32 hours ago        114.6 MB
ubuntu              precise             2bed76595591        32 hours ago        114.6 MB
[root@localhost ~]# 

ちゃんと「fuji/nginx」のイメージが作成されている^^

・バックグラウンドで実行
先ほどのfuji/nginxイメージを元にバックグラウンドで実行するコンテナを作成してみる。
[root@localhost ~]# docker run -d -p 80:80 --name nginx1 fuji/nginx /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf
d500411c0af2d2c6a07cc7882e043fb641a2035f24090033e588d9c17a09c24a
2014/10/12 00:11:36 Error response from daemon: Cannot start container d500411c0af2d2c6a07cc7882e043fb641a2035f24090033e588d9c17a09c24a: listen tcp 0.0.0.0:80: bind: address already in use

ん!?何かエラーが・・・
80ポートがかぶっているorzホストのCentOSで既に80ポートをapacheさんが使用しているからか・・・
とりあえずポート指定無しで実行!!
[root@localhost ~]# docker run -d --name nginx2 fuji/nginx /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf
960de36b0911a2ccad45773aae6938bf542a3aa657cd922177c783e217bb56c0
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
960de36b0911        fuji/nginx:latest   /usr/sbin/nginx -g '   4 seconds ago       Up 3 seconds                            nginx2              
[root@localhost ~]# 
起動はでけた^^;

こんどはホスト側のapacheを停止させて実行してみる。
[root@localhost ~]# service httpd stop
httpd を停止中:                                            [  OK  ]

バックグラウンドで実行中のコンテナを停止
[root@localhost ~]# docker stop nginx2
nginx2
[root@localhost ~]# 

ポートを指定して実行
[root@localhost ~]# docker run -d -p 80:80 --name nginx3 fuji/nginx /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf
8f6f070d392d53ca71b4ac0ba53d6032cd2dd96a685beb621231952bf257f553
[root@localhost ~]# curl localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost ~]# 

できてる^^
ブラウザでも確認














コンテナを停止させ、再度起動してみる。
バックグラウンドでのコンテナを停止。
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                NAMES
8f6f070d392d        fuji/nginx:latest   /usr/sbin/nginx -g '   2 minutes ago       Up 2 minutes        0.0.0.0:80->80/tcp   nginx3              
[root@localhost ~]# docker stop nginx3
nginx3
[root@localhost ~]# !curl
curl localhost:80
curl: (7) couldn't connect to host
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

再度実行
[root@localhost ~]# docker start nginx3
nginx3

確認
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                NAMES
8f6f070d392d        fuji/nginx:latest   /usr/sbin/nginx -g '   5 minutes ago       Up 19 seconds       0.0.0.0:80->80/tcp   nginx3              
[root@localhost ~]# 

ちゃんと実行されている^^
[root@localhost ~]# !curl
curl localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

次回はDockerfileをお勉強^^