azukipochette's weblog

memory dump (mini)

Markdown から PowerPoint (pptx) を作成したくて Docker イメージを作ったという話

Markdown から PowerPoint を生成できたら便利そうなのにと思ったら、いつの間にか Pandoc が公式に PowerPoint (pptx) への出力に対応していました。

昔は、PDF までは変換できて、あとは Adobe Acrobat の機能で PowerPoint 化する必要があったのですが、PDF を経由しなくても変換できるのはいいですね *1

https://pandoc.org

インストールするイメージはここにあります。結構よい頻度で更新されているのも好印象です。

ただ...この Pandoc というのは、Haskell ベースなツールなのです。 Windows な人は普通に msi をインストールしそうなものですが、私はこういう一時的にしか使わないツールなのに、結構大きな実行環境がセットなものをインストールしたくない!という人なので、msi は使いたくありません。

実行環境をまるっと手に入れて使い終わったら捨てたい...そう、いまこそ Docker の出番です。

ただ、Ubuntu のパッケージマネージャーで落ちてくるのが古くて PowerPoint 変換に未対応だったりする罠がありまして、なかなか簡単ではありませんでした。 とうわけで、結局、自分で Dockerfile から Docker イメージを作ることにしました。

せっかくなので、下記に張り付けておきます。

FROM ubuntu:18.04

# Update ubuntu
RUN apt-get update -qq
RUN apt-get upgrade -qq

RUN apt-get install -y wget
RUN wget -q https://github.com/jgm/pandoc/releases/download/2.2.1/pandoc-2.2.1-1-amd64.deb
RUN dpkg -i pandoc-2.2.1-1-amd64.deb

# Install texlive
RUN apt-get install -y texlive-latex-recommended

# install wkhtmltopdf
RUN apt-get install -y -qq xvfb libfontconfig wkhtmltopdf

RUN apt-get clean

VOLUME /workspace
WORKDIR /workspace

Pandoc で PowerPoint へ変換する

まず、上記の Dockerfile から Docker Image を作ります。 お使いのエディターで Dockerfile を作ってそのフォルダー上で下記のコマンドを実行します。 Dockerfile の作成には時間がかかりますので、ご注意を。

docker build -t <yourname>/pandoc .

作成が完了したら、作成したイメージを下記のコマンドで実行します。 -v はホスト環境のボリュームを Docker のコンテナにマッピングするものです。この値は私の環境用の値にしているので、お好きな値に変えてください。

docker run -v /Users/<yourname>/share:/workspace -it <yourname>/pandoc /bin/bash

bash が起動したら、以下のコマンドで Markdown から pptx に変換します。拡張子から判定してくれるので、簡単でいいですね。

pandoc test.md -o test.pptx

おまけ

Dockerfile から docker build を試していると気が付けば大量の停止コンテナと<none>:<none> なイメージができますよね。

これの削除方法も紹介しておきます。Web で検索すると古い削除方法をよく見かけますが、今のコマンドは以下です。

停止中のコンテナを一括削除するコマンド

docker container prune

リポジトリ名とタグがない (<none>:<none> な) イメージを一括削除するコマンド

docker images prune

では、良い Pandoc 生活を。Enjoy!

*1:なにより Adobe Acrobat を買わなくていいのがいい(笑)

Hyper はじめました

夏の冷やし中華はじめましたのように、Hyper という Terminal を使い始めました *1。 Hyper という名前が一般名詞すぎるのか、世間的には Hyper Terminal とも呼ばれるようです。

Hyper というのは、Electron ベースな Terminal ソフトウェアです。 Electron ベースなので、macOS/Linux だけではなく、Windows でも動作します。

どうも、Mac ユーザーが多いようで、プラグインの量は macOS 向けのものが多いです。Electron なんだから、Windows 向けもなにもないんじゃないの?と思うかもしれませんが、システム情報を取り出して表示するとかプラットフォーム依存の部分があって、そのあたりのプラグインは見事に macOS だけしか考慮されていないということが良くあります。

(ベスト プラグイン!とか書かれている多くのプラグインが動かなくて悲しい気持ちになりました。)

プラグインは、Hyper のサイトからも検索できますが、以下のリポジトリに素敵にまとめられています。

プラグインは npm で取得する形になります。Hyper 2 以降は 「hyper コマンドを使ってね」という説明がありますが、設定ファイルである hyper.js の plugin に書くことでも大丈夫です。

hyper.js を書く端末に配置するだけで、勝手に npm でプラグインもとってきてくれるので、複数環境で同じ設定の Terminal を使いたいときに便利ですね。

Electron 特有の大量プロセス問題がさらに悪化するのが気になるところですが...プラグインも手軽にかけるので、しばらく使ってみようと思っています。

一応、私が使っている設定 (hyper.js) を張り付けておきます (Surface Book 2 で利用)。

// Future versions of Hyper may add additional config options,
// which will not automatically be merged into this file.
// See https://hyper.is#cfg for all currently supported options.

module.exports = {
  config: {
    // choose either `'stable'` for receiving highly polished,
    // or `'canary'` for less polished but more frequent updates
    updateChannel: 'stable',

    // default font size in pixels for all tabs
    fontSize: 14,

    // font family with optional fallbacks
    fontFamily: 'Menlo, "DejaVu Sans Mono", Consolas, "Lucida Console", monospace',

    // default font weight: 'normal' or 'bold'
    fontWeight: 'normal',

    // font weight for bold characters: 'normal' or 'bold'
    fontWeightBold: 'bold',

    // line height as a relative unit
    lineHeight: 1,

    // letter spacing as a relative unit
    letterSpacing: 0,

    // terminal cursor background color and opacity (hex, rgb, hsl, hsv, hwb or cmyk)
    cursorColor: 'rgba(248,28,229,0.8)',

    // terminal text color under BLOCK cursor
    cursorAccentColor: '#000',

    // `'BEAM'` for |, `'UNDERLINE'` for _, `'BLOCK'` for â–ˆ
    cursorShape: 'BLOCK',

    // set to `true` (without backticks and without quotes) for blinking cursor
    cursorBlink: false,

    // color of the text
    foregroundColor: '#fff',

    // terminal background color
    // opacity is only supported on macOS
    backgroundColor: '#000',

    // terminal selection color
    selectionColor: 'rgba(248,28,229,0.3)',

    // border color (window, tabs)
    borderColor: '#333',

    // custom CSS to embed in the main window
    css: '',

    // custom CSS to embed in the terminal window
    termCSS: '',

    // if you're using a Linux setup which show native menus, set to false
    // default: `true` on Linux, `true` on Windows, ignored on macOS
    showHamburgerMenu: '',

    // set to `false` (without backticks and without quotes) if you want to hide the minimize, maximize and close buttons
    // additionally, set to `'left'` if you want them on the left, like in Ubuntu
    // default: `true` (without backticks and without quotes) on Windows and Linux, ignored on macOS
    showWindowControls: '',

    // custom padding (CSS format, i.e.: `top right bottom left`)
    padding: '12px 14px',

    // the full list. if you're going to provide the full color palette,
    // including the 6 x 6 color cubes and the grayscale map, just provide
    // an array here instead of a color map object
    colors: {
      black: '#000000',
      red: '#C51E14',
      green: '#1DC121',
      yellow: '#C7C329',
      blue: '#0A2FC4',
      magenta: '#C839C5',
      cyan: '#20C5C6',
      white: '#C7C7C7',
      lightBlack: '#686868',
      lightRed: '#FD6F6B',
      lightGreen: '#67F86F',
      lightYellow: '#FFFA72',
      lightBlue: '#6A76FB',
      lightMagenta: '#FD7CFC',
      lightCyan: '#68FDFE',
      lightWhite: '#FFFFFF',
    },

    MaterialTheme: {
      //   // Set the theme variant,
      //   // OPTIONS: 'Darker', 'Palenight', ''
      theme: '',
    },

    opacity : {
      focus : 0.95,
      blur : 0.8,
    },

    // the shell to run when spawning a new session (i.e. /usr/local/bin/fish)
    // if left empty, your system's login shell will be used by default
    //
    // Windows
    // - Make sure to use a full path if the binary name doesn't work
    // - Remove `--login` in shellArgs
    //
    // Bash on Windows
    // - Example: `C:\\Windows\\System32\\bash.exe`
    //
    // PowerShell on Windows
    // - Example: `C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe`
    shell: '',

    // for setting shell arguments (i.e. for using interactive shellArgs: `['-i']`)
    // by default `['--login']` will be used
    shellArgs: ['--login'],

    // for environment variables
    env: {},

    // set to `false` for no bell
    bell: 'SOUND',

    // if `true` (without backticks and without quotes), selected text will automatically be copied to the clipboard
    copyOnSelect: false,

    // if `true` (without backticks and without quotes), hyper will be set as the default protocol client for SSH
    defaultSSHApp: true,

    // if `true` (without backticks and without quotes), on right click selected text will be copied or pasted if no
    // selection is present (`true` by default on Windows and disables the context menu feature)
    // quickEdit: true,

    // URL to custom bell
    // bellSoundURL: 'http://example.com/bell.mp3',

    // for advanced config flags please refer to https://hyper.is/#cfg
  },

  // a list of plugins to fetch and install from npm
  // format: [@org/]project[#version]
  // examples:
  //   `hyperpower`
  //   `@company/project`
  //   `project#1.0.1`
  plugins: [
    'hyper-material-theme',
    "hyper-opacity"
  ],

  // in development, you can create a directory under
  // `~/.hyper_plugins/local/` and include it here
  // to load it and avoid it being `npm install`ed
  localPlugins: [],

  keymaps: {
    // Example
    // 'window:devtools': 'cmd+alt+o',
  },

};

それでは、よい Hyper 生活を。Enjoy!

*1:Windows な人には Hyper-V を想像しそうですが、関係はありません。

WSL (Ubuntu 18.04) に Terraform をコマンドラインでインストールする方法

以下のコマンドを bash 上で実行します。

なお、記事を書いている現時点では以下の URL が最新ですが、今も URL が最新とは限らないので、https://releases.hashicorp.com/terraform から最新を探してくださいね。

sudo apt-get install unzip
wget https://releases.hashicorp.com/terraform/0.11.7/terraform_0.11.7_linux_amd64.zip
unzip terraform_0.11.7_linux_amd64.zip
sudo mv terraform /usr/local/bin/

バージョン情報を表示して、呼び出せるかどうかをを確認します。

terraform --version

以下のような感じでバージョン情報が表示されれば OK です。

Terraform v0.11.7

Enjoy!

Ubuntu (Ubuntu 18.04 with WSL) でビープ音を止める

/etc/inputrc を開き、下記行を検索してコメントを無効にします。

set bell-style none

VIM のビープ音も止める

もし VIM 使いなら VIM のビープ音も止めたいのではないかと思います。

その場合は、以下のコマンドを実行します。

set belloff=all

これで間違っても静かに対応できますね。Enjoy!

Docker on VMWare Workstation と WSL で快適生活

前回の続編です。

今回は、WSL (Windows Subsystem for Linux) から VMWare Worksation 上で動作する Docker-machine を操作する方法です。 なお、WSL 上で動作する Linux ディストリビューションは複数ありますが、私は、最近リリースされた Ubuntu 18.04 を使ってます。

WSL に Docker-CE をインストールする

Ubuntu に対して Docker をインストールします。

Docker のインストール方法は公式に手順があるので、素直に従います。

apt-get install で docker-ce が見つからないと言われたら...

ちゃんとリポジトリを追加したのに docker-ce がないといわれるぞ!言われた場合は、もう少しちゃんとマニュアルを読みましょう。

現時点で Ubuntu 18.04 向けの docker-ce は stable には無いので、edge か test にする必要があります。test はちょっとアレなので、edge ぐらいがいいのではないでしょうか。 というわけで、以下のコマンドを代わりに実行します。

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

.bashrc を変更する

~/.bashrc に以下の行を追加します。

MACHINE_IP=`'/mnt/c/Program Files/Docker Toolbox/docker-machine.exe' ls 2>/dev/null | grep default | awk '{ print $5}'`

export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="$MACHINE_IP"
export DOCKER_CERT_PATH="/mnt/c/Users/$USER/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"

なお、docker-machine コマンドで作成する環境の名前は default を想定しています。 別の名前にしている場合は、grep の引数と DOCKER_MACHINE_NAME の値を適宜変更してください。

内容は読むとわかりますが、docker-machine コマンドを実行して、出力結果から Grep で default 環境の行を取り出し、さらに IP アドレス部分を AWK で取り出して変数を作成、それを DOCKER_HOST 環境変数に設定することにより、接続できるようにしています。これで、毎回環境変数を更新する必要はありませんね (ヤッタ!)。

更新後は、ターミナルを再起動するか、以下のコマンドを実行して .bashrc を再読み込みします。

source ~/.bashrc

設定がうまく動いているかどうかを確認する

bash 上で以下のコマンドを実行します

docker version

以下のように出力結果に Server が表示されていれば、あとは普通に docker コマンドが使えます。

Client:
 Version:      18.05.0-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   f150324
 Built:        Wed May  9 22:16:13 2018
 OS/Arch:      linux/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.05.0-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.10.1
  Git commit:   f150324
  Built:        Wed May  9 22:20:42 2018
  OS/Arch:      linux/amd64
  Experimental: false

試しに hello-world コンテナでも実行してみます。

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/engine/userguide/

それでは、良い Docker on VMWare workstation 生活を。Enjoy!

VMWare Workstation で Docker 生活

はじめに

こん**わ。

なんだか VMWare 自体がやる気がないように見える VMWare Workstation。 Docker も Hyper-V 対応になって、時代は Hyper-V なんじゃないの...と思い出しているかもしれない皆様、いかがお過ごしでしょうか。

私はしばらくの間 Docker は VMWare Workstation 上で使えないのだと思っていて、Vagrant + VMWare Workstation で CoreOS を動かしてその上で Docker を扱っていましたが、実は VMWare Workstation でも Docker が扱えることがわかったので、今日はこの話をまとめます。

Docker Machine VMWare Workstation Drive の導入

世の中には VMWare 愛のある人がいるもので、Docker 用の VMWare Workstation ドライバーを書いてくれています。

Docker for Windows というのは簡単に言うと、docker-machine というコマンドを使って Docker を動かす Linux 仮想マシンを起動して、その環境を docker コマンド経由で操作するようなことをしています。 通常、docker-machine コマンドのドライバーは Hyper-V か VirtualBox なわけですが、これを上記の docker-machine-vmwareworkstation を使うことで、VMWare 上で動作する仮想マシンを用意することができます。

インストール手順は、上記の GitHub のページの "Installation" に書いてあるので読んでください。 なお、最初にある "Install Docker Toolbox" というのは、以下にあります。

start.sh のスクリプトはまるっと置き換えてしまえば大丈夫です。

docker-machine-driver-vmworkstation.exe はパスが通っているフォルダーに置くか、docker-machine.exe のあるフォルダーのどちらかに置くことになりますが、私は docker-machine.exe のフォルダに直接置いています。*1

docker-machine を実行する

docker-machine コマンドを使って、docker を動かす Linux (boot2docker) 仮想マシンを作成します。

docker-machine create --driver=vmwareworkstation default

私は既定のまま使っていますが、ディスクやメモリ、CPU 数などはパラメータで変更可能です。詳しくはこちらも GitHub のページの記載を見てください。 実行すると以下のような流れで仮想マシンが作成されます (それなりに時間がかかります)。

Running pre-create checks...
Creating machine...
(default) Copying C:\Users\<YourName>\.docker\machine\cache\boot2docker.iso to C:\Users\<YourName>\.docker\machine\machines\default\boot2docker.iso...
(default) Creating SSH key...
(default) Creating VM...
(default) Creating disk 'C:\Users\<YourName>\.docker\machine\machines\default\default.vmdk'
(default) Virtual disk creation successful.
(default) Starting default...
(default) Waiting for VM to come online...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env default

作成が完了しているかどうかを念のため確認してみます。

docker-machine ls

以下のような結果が帰ってくるはずです。

NAME      ACTIVE   DRIVER              STATE     URL                         SWARM   DOCKER        ERRORS
default   -        vmwareworkstation   Running   tcp://192.168.32.139:2376           v18.05.0-ce

次に、docker-machine create のレスポンスの最後にあるように、作成した仮想マシン上で docker コマンドが使えるように接続します。

docker-machine env default

以下のような結果が帰ってきます (実はこの記述は置き換えた start.sh の中に書いてあります)。

SET DOCKER_TLS_VERIFY=1
SET DOCKER_HOST=tcp://192.168.32.139:2376
SET DOCKER_CERT_PATH=C:\Users\<YourName>\.docker\machine\machines\default
SET DOCKER_MACHINE_NAME=default
SET COMPOSE_CONVERT_WINDOWS_PATHS=true
REM Run this command to configure your shell:
REM     @FOR /f "tokens=*" %i IN ('docker-machine env default') DO @%i

"Run this command to configure your shell" と書かれているように、表示されたコマンドを次のように実行します。

@FOR /f "tokens=*" %i IN ('docker-machine env default') DO @%i

これで、Docker コマンドを実行する準備ができました。

docker コマンドを試す

実際に docker コマンドが使えるかどうかを hello-world というイメージを使用して確認します

docker run hello-world

すると、以下のような結果が返ってきます。

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
9bb5a5d4561a: Pull complete
Digest: sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
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/engine/userguide/

これで、無事に docker が使えることが確認できました!(ヤッタ!)

ホストとコンテナのフォルダを共有する

ホスト (Windows) のフォルダーをコンテナ上で扱えないと不便ですよね。

なぜか Docker Machine VMWare Workstation Drive の README.md には記載がないのですが、docker-machine コマンドで仮想マシンを作成したときに、VMWare の機能を使用して C:\Users フォルダーが /Users として共有されています。これまでに説明したように、"Windows <-> Linux (boot2docker) 仮想マシン <-> Container" という流れなので、docker コマンドの --volume で /Users をマッピングすればいいことになります。

たとえば、以下は ubuntu コンテナ上で Windows 上の C:\Users を /share としてマッピングする時のコマンドです。

docker run --name mycontainer -v /Users:/share -it ubuntu /bin/bash

なお、Windows の世界の人は忘れがちですが、大文字・小文字を区別するので、"/Users" とするのがポイントです。

"C:\Users" 配下ではないフォルダーをマウントしたい

上記を見て、「え? C:\Users 配下しか使えないの?」と思った方々、私たちには mklink コマンドがあります。

これは、シンボリックリンクを作成するコマンドです。シンボリックリンクというのを知らない人に簡単に説明すると、特定のフォルダーに実態があるものを、あたかもそのフォルダーにあるかのようにリンクを作成するものと思ってください。たとえば、"D:\Works" を使いたい場合には、まず C:\Users 配下にシンボリックリンクを作成すればよいことになります。

以下は、自分のユーザープロファイル配下に Share フォルダーがあるように D:\Works フォルダーをリンクした例です。

mklink/d C:\Users\<YourName>\share D:\Works

あとは docker コマンド実行時に -v を使ってマッピングすればよいだけです。

docker run --name mycontainer -v /Users/<YourName>/share:/share -it ubuntu /bin/bash

これで、楽しく Docker が使えますね。

それでは、楽しい Docker ライフを。Enjoy!

*1:どうせ start.sh ファイルの内容を置き換えしているし、いいかなという感覚で置いています。ちなみに、変更していなければ既定の場所は "C:\Program Files\Docker Toolbox" です。