ICTについて使える情報を発信していきます. 遠回りしたり, 自分のペースで...

このブログを検索

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

dockerでサクッとhttpsサーバーを作る

コンカイハ, 簡単にSSL証明書を取得でき, HTTPSサーバーを簡単に作れるlinuxserver/letsencrypt dockerを紹介したいと思います.

linuxserver/letsencrypt dockerの使い方

公式の使い方のブログがあるので, 気になる方は是非そちらもご参照ください. (このブログを探すのにも結構手間取りましたw

まずはdocker-compose.ymlファイルを作成し, 次のように書きます.

version: "2"
services:
  letsencrypt:
    image: linuxserver/letsencrypt
    container_name: letsencrypt
    cap_add:
      - NET_ADMIN
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Asia/Tokyo
      - URL=<yourdomain>
      - SUBDOMAINS=www,
      - VALIDATION=http
      - EMAIL= #optional
      - EXTRA_DOMAINS= #optional
      - STAGING=false #optional true or false
    volumes:
      - ./config:/config
    ports:
      - 443:443
      - 80:80 #optional
    restart: unless-stopped

各environmentの値の意味はこちらに書いてありますが, <yourdomain>のところを, SSL証明書を取得したいドメインに変えるだけで良いと思います.
例えばURL=example-aka.comなど.

次に, docker-composeコマンドを使えるようにします. 公式サイトからインストールするか, 手前味噌ですが, dockerでdocker-composeを動かす記事を参考にしていただければと思います.

あとは実際にdocker-compose run letsencrypt, もしくはdocker-compose upを実行します.
証明書の生成に時間がかかりますのでお待ちください☕︎

$ docker-compose run letsencrypt
[s6-init] making user provided files available at /var/run/s6/etc...exited 0.
[s6-init] ensuring user provided files have correct perms...exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] 01-envfile: executing... 
[cont-init.d] 01-envfile: exited 0.
[cont-init.d] 10-adduser: executing... 
usermod: no changes

-------------------------------------
          _         ()
         | |  ___   _    __
         | | / __| | |  /  \ 
         | | \__ \ | | | () |
         |_| |___/ |_|  \__/


Brought to you by linuxserver.io
-------------------------------------

To support the app dev(s) visit:
Let's Encrypt: https://letsencrypt.org/donate/

To support LSIO projects visit:
https://www.linuxserver.io/donate/

...

New certificate generated; starting nginx
Starting 2019/12/30, GeoIP2 databases require personal license key to download. Please manually download/update the GeoIP2 db and save as /config/geoip2db/GeoLite2-City.mmdb
[cont-init.d] 50-config: exited 0.
[cont-init.d] 60-renew: executing... 
The cert does not expire within the next day. Letting the cron script handle the renewal attempts overnight (2:08am).
[cont-init.d] 60-renew: exited 0.
[cont-init.d] 99-custom-files: executing... 
[custom-init] no custom files found exiting...
[cont-init.d] 99-custom-files: exited 0.
[cont-init.d] done.
[services.d] starting services
[services.d] done.
nginx: [alert] detected a LuaJIT version which is not OpenResty's; many optimizations will be disabled and performance will be compromised (see https://github.com/openresty/luajit2 for OpenResty's LuaJIT or, even better, consider using the OpenResty releases from https://openresty.org/en/download.html)
Server ready

Server readyが表示されたら準備完了です.
実際に https://yourdomain/ にアクセスしてみましょう.
次のページが表示されたら成功です!

$ curl -L example-aka.com
    <html>
        <head>
            <title>Welcome to our server</title>
            <style>
            body{
                font-family: Helvetica, Arial, sans-serif;
            }
            .message{
                width:330px;
                padding:20px 40px;
                margin:0 auto;
                background-color:#f9f9f9;
                border:1px solid #ddd;
            }
            center{
                margin:40px 0;
            }
            h1{
                font-size: 18px;
                line-height: 26px;
            }
            p{
                font-size: 12px;
            }
            </style>
        </head>
        <body>
            <div class="message">
                <h1>Welcome to our server</h1>
                <p>The website is currently being setup under this address.</p>
                <p>For help and support, please contact: <a href="me@example.com">me@example.com</a></p>
            </div>
        </body>
    </html>

アクセスできない場合はFirewall設定など見てみてください.

以上の手順で簡単にHTTPSサーバーを立てることができました!

まとめ

dockerで簡単にHTTPSサーバーをlinuxserver/letsencrypt Dockerを使ってみました. 証明書の自動更新機能もあるらしいのでとても便利ですね.

ただ, 私がこの記事を書いたきっかけは, 「dockerでサブディレクトリ型のhttps対応リバースプロキシが簡単に作れない!」というのを憤慨してのことなので, 次回はそれを記事にしていこうと思います.

サブドメイン型のhttpsリバースプロキシなら, https-portalとかnginx-reverse-proxyとかもあるので, そちらを試す方が良いかもしれません. (全然更新されていないのが気になるけど.

Read More

dockerでDocker Composeができるようになっていたお話

コンカイハ, Docker ComposeをインストールすることなくDocker Composeを使う話をします. なんのこっちゃと思われそうですがw

あらかじめ, Dockerはインストールしていてください.

環境

docker/composeイメージを使う

docker/composeというdockerイメージがDocker Hubにあります.
それを使うことにより, dockerコマンドのみで, Docker Composeが使えるようになります.

実際に使ってみましょう.

$ docker run --rm \
-v "/var/run/docker.sock:/var/run/docker.sock" \
-v $PWD:$PWD -w $PWD docker/compose:alpine-1.25.4 \
version

Unable to find image 'docker/compose:alpine-1.25.4' locally
alpine-1.25.4: Pulling from docker/compose
89d9c30c1d48: Pull complete
eccc02a3b135: Pull complete
7382d3c5d78f: Pull complete
f410c750fbcb: Pull complete
Digest: sha256:390bdf3da99a622f5c11f4db386dd97f2858db30cfed07d000b867f1fb25b2f7
Status: Downloaded newer image for docker/compose:alpine-1.25.4
Define and run multi-container applications with Docker.

docker-compose version 1.25.4, build 8d51620
docker-py version: 4.1.0
CPython version: 3.7.5
OpenSSL version: OpenSSL 1.1.1d  10 Sep 2019

これで簡単にdocker-composeが使えちゃいます. ありがたい.

使っているオプションを説明すると

-v "/var/run/docker.sock:/var/run/docker.sock"

docker deamonの通信に必要.
Ref: https://docs.docker.com/engine/docker-overview/


-v $PWD:$PWD -w $PWD

これがないとdocker-compose.ymlのvolume機能が効かないため, 追加した.
volumeを使わない場合はいらない.

となっています.

docker/composeでhello world

では, docker-compose.ymlを使って実際に動かしてみましょう.

$ cat <<EOF > docker-compose.yml
version:  "3"
services:
  hello-world:
    image: hello-world
EOF
% docker run --rm \
-v "/var/run/docker.sock:/var/run/docker.sock" \
-v $PWD:$PWD -w $PWD docker/compose:alpine-1.25.4 \
> up
Creating network "hello-world_default" with the default driver
Pulling hello-world (hello-world:)...
latest: Pulling from library/hello-world
Digest: sha256:9572f7cdcee8591948c2963463447a53466950b3fc15a247fcad1917ca215a2f
Status: Downloaded newer image for hello-world:latest
Creating hello-world_hello-world_1 ... done
Attaching to hello-world_hello-world_1
hello-world_1  |
hello-world_1  | Hello from Docker!
hello-world_1  | This message shows that your installation appears to be working correctly.
hello-world_1  |
hello-world_1  | To generate this message, Docker took the following steps:
hello-world_1  | 1. The Docker client contacted the Docker daemon.
hello-world_1  | 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
hello-world_1  | (amd64)
hello-world_1  | 3. The Docker daemon created a new container from that image which runs the
hello-world_1  | executable that produces the output you are currently reading.
hello-world_1  | 4. The Docker daemon streamed that output to the Docker client, which sent it
hello-world_1  | to your terminal.
hello-world_1  |
hello-world_1  | To try something more ambitious, you can run an Ubuntu container with:
hello-world_1  | $ docker run -it ubuntu bash
hello-world_1  |
hello-world_1  | Share images, automate workflows, and more with a free Docker ID:
hello-world_1  | https://hub.docker.com/
hello-world_1  |
hello-world_1  | For more examples and ideas, visit:
hello-world_1  | https://docs.docker.com/get-started/
hello-world_1  |
hello-world_hello-world_1 exited with code 0

これによって, docker-compose.ymlが動かせることがわかりました.
実際にはdocker-composeの公式のhello worldを動かしたほうが良いと思いますが, ここは手抜きさせてください…

docker/composeを使うコマンドを関数にする

これで, docker-composeがdockerのみで動かせるようになりましたが, 何しろコマンドが長い. なので, コマンドを関数化しましょう.

docker-compose(){  docker run --rm -v "/var/run/docker.sock:/var/run/docker.sock" -v $PWD:$PWD -w $PWD docker/compose:alpine-1.25.4 "$@"; }

これを\.*shrcなりにおいておくと, つぎからdocker-composeコマンドがインストールされているかのように使えます.

$ docker-compose(){  docker run --rm -v "/var/run/docker.sock:/var/run/docker.sock" -v $PWD:$PWD -w $PWD docker/compose:alpine-1.25.4 "$@"; }
$ docker-compose version
docker-compose version 1.25.4, build 8d51620
docker-py version: 4.1.0
CPython version: 3.7.5
OpenSSL version: OpenSSL 1.1.1d  10 Sep 2019
$ docker-compose up -d
Starting hello-world_hello-world_1 ... done

もしくはaliasを使うという手もあります.

alias docker-compose='docker run --rm -v "/var/run/docker.sock:/var/run/docker.sock" -v $PWD:$PWD -w $PWD docker/compose:alpine-1.25.4'

まとめ

ということで, Docker ComposeをインストールせずにDocker Composeを使う方法を紹介しました. 言われてみれば思うのですが, DockerあるのにDocker Composeをインストールするのはおかしいですよね…
あんまり目立った欠点もなさそうですし, 普段使いはこれでDocker Composeを使おうと思います.

Read More

mitmproxyでプロキシを立てて簡単にiphoneの通信を取得する

コンカイハ, プロキシ経由で簡単に通信情報が取得できるmitmproxyを使ってみました.

環境

  • Mac OS(プロキシを立てるためのパソコン)
  • Iphone(クライアント)

どうやって通信情報を取得する?

iphoneとwebの間にmitmproxyを挟みます.
それによって, mitmproxyで通信情報を取得, 見ることができます.

iphone <-> mitmproxy <-> network

httpsでも情報を見ることができるそうです.

詳しくは公式に色々書いてあります.

実際に通信情報を見てみた.

実際にmitmproxyのサーバーをmacで立てて, iphoneをつなげて試してみました.

mitmproxyでプロキシを立てる

プロキシを立てるためにdockerを使います.
公式が用意しているdockerがdockerhubにあるのでそれを使います.

ターミナルで

docker run --rm -it -p 8080:8080 mitmproxy/mitmproxy

と打つと画面が更新され, サーバーが立ち上がります.

-p <任意のポート>:8080にすれば任意のポートを開放することができます.

サーバーのつながっているネットワークのSSIDとIPアドレスを確認する.

サーバーのネットワーク接続先のSSIDとIPアドレスを確認します.
Macの場合だとシステム環境設定から開くことができます.

Mac System Preference > Network Settings

今回の場合だと, SSIDはhogehoge, IPアドレスは192.168.0.2となります.

Iphoneで通信先のプロキシを設定する

iphoneで実際に通信先のプロキシを設定してみます.

iphoneのsetting画面を開いてWi-Fi欄をタップします.

SSIDはhogehogeとしておきます.

iphone settings

Wifi設定画面でhogehogeをタップします.

iphone>wifi settings

下の方にスクロールし, プロキシを構成をタップします.

iphone>wifi>ssid settings

プロキシの設定画面で, 手動をタップし, サーバーのIPアドレス(192.168.0.2)とポート(8080)をそれぞれ入力します.

iphone>wifi>ssid>proxy settings

以上でセッティングは終わりです.

まとめ

ということで, プロキシ経由で簡単に通信情報が取れるmitmproxyを使ってみました. デバッグとかに使えそうです.

Read More

Docker install on Raspbian [RaspberryPi]

コンカイハ, Raspbianにdockerをインストールする方法を書いていきたいと思います.
公式ドキュメント

環境

  • Raspbian(version 4.19)

Docker Install

こちらのコマンドを入力すると終わりです.
curl -sSL https://get.docker.com | sh
お疲れさまです.
Read More

[エラー対処] windows10 の docker で 「Error response from daemon」

コンカイハ, windows10のdockerで「Error response from daemon」となったので, その対処をしました。

環境

  • windows10
  • docker toolbox

エラー発生

docker run コマンドを打つだけで以下のようなエラーが出ました。
$ docker run --rm hello-world

Unable to find image 'hello-world:latest' locally

C:\Program Files\Docker Toolbox\docker.exe: Error response from daemon: Get https://registry-1.docker.io/v2/: dial tcp: lookup registry-1.docker.io on 10.0.2.3:53: read udp 10.0.2.15:39085->10.0.2.3:53: i/o timeout.

See 'C:\Program Files\Docker Toolbox\docker.exe run --help'.

対処

DNSの名前解決ができていないらしいので、Google Public DNS を追加します。

/etc/resolv.confにDNSを追加

/etc/resolv.confnameserver 8.8.8.8を追加しましょう。
ここでは手っ取り早くワンライナーでできる方法vmに入って操作する方法の2つを紹介します。

ワンライナー

以下のコマンドで一発でできます。
docker-machine ssh default "sudo sh -c 'echo \"nameserver 8.8.8.8\" >> /etc/resolv.conf'"
これでdocker run --rm hello-worldコマンドを打つとできました。
$ docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:b8ba256769a0ac28dd126d584e0a2011cd2877f3f76e093a7ae560f2a5301c00
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.
    (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/

vmに入って追加

まずvmの中に入ります。
docker-machine ssh default
その後、/etc/resolv.confnameserver 8.8.8.8追加します。そのままだとpermissionがないのでsudoも使いましょう。
sudo vi /etc/resolv.conf #add "nameserver 8.8.8.8"
もしくはcatで追加します.
sudo sh -c 'echo "nameserver 8.8.8.8" >> /etc/resolv.conf'
終わったあとはexitして、docker run --rm hello-worldをやるとちゃんと動きました。
$ exit # vmから抜ける.
$ docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:b8ba256769a0ac28dd126d584e0a2011cd2877f3f76e093a7ae560f2a5301c00
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.
    (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/
Read More

dockerインストール方法(mac)

コンカイハ、dockerをインストールする方法を書いていきたいと思います。
現時点(2019/09/21)で書いたのはmacのみです。

docker on mac (2019/09/16)

こちらはhomebrewを入れていたら簡単で、
brew cask install docker
で終わりです。

Read More

Popular Posts