Azure Container InstancesでNervesアプリを開発する


この記事は、「Docker Advent Calendar 2020」 7日目です。
あいていたので埋めました。
前日は、@c3driveさんのAWSにコンテナ環境を構築する

はじめに

Dockerfile

# docker-elixir 1.11.2
# https://hub.docker.com/_/elixir
FROM elixir:1.11.2

ENV DEBCONF_NOWARNINGS yes

# Install libraries for Nerves development
RUN apt-get update && \
    apt-get install -y build-essential automake autoconf git squashfs-tools ssh-askpass pkg-config curl openssh-server && \
    rm -rf /var/lib/apt/lists/*

RUN mkdir /var/run/sshd
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin prohibit-password/' /etc/ssh/sshd_config
COPY ./config_files/id_rsa.pub /root/.ssh/authorized_keys
EXPOSE 22

# [Optional] Uncomment this section to install libraries for customizing Nerves System
#RUN apt-get update && \
#    apt-get install -y libssl-dev libncurses5-dev bc m4 unzip cmake python && \
#    rm -rf /var/lib/apt/lists/*

# Install fwup (https://github.com/fhunleth/fwup)
ENV FWUP_VERSION="1.8.2"
RUN wget https://github.com/fhunleth/fwup/releases/download/v${FWUP_VERSION}/fwup_${FWUP_VERSION}_amd64.deb && \
    apt-get install -y ./fwup_${FWUP_VERSION}_amd64.deb && \
    rm ./fwup_${FWUP_VERSION}_amd64.deb && \
    rm -rf /var/lib/apt/lists/*

# Install hex and rebar
RUN mix local.hex --force
RUN mix local.rebar --force
# Install Mix environment for Nerves
RUN mix archive.install hex nerves_bootstrap 1.10.0 --force

CMD ["/usr/sbin/sshd", "-D"]
$ mkdir config_files
$ cp ~/.ssh/id_rsa.pub config_files/
$ docker build -t docker-nerves .
  • イメージを作ったら、あとの手順は以下のような感じです
  • 以下、もう少し詳しくみていきます

1. Azure Container Registryにイメージを登録

$ docker login xxx.azurecr.io
$ docker tag docker-nerves xxx.azurecr.io/docker-nerves
$ docker push xxx.azurecr.io/docker-nerves

2. Azure Container Instancesでそのカスタムイメージを使うように設定する

3. sshで接続してNervesアプリの開発をする

で、コンテナの中に入ります

コンテナの中での操作

$ cd ~/.ssh
$ ssh-keygen -t rsa -b 4096
$ cd
$ mix nerves.new hello_nerves
$ cd hello_nerves
$ export MIX_TARGET=rpi4
$ mix deps.get
$ mix firmware

で、無事にコンテナの中にファームウェア
/root/hello_nerves/_build/rpi4_dev/nerves/images/hello_nerves.fwができます

Raspberry Pi 4以外の場合は、Targetsから値を選んでください。

開発マシンにもどって

$ scp [email protected]:/root/hello_nerves/_build/rpi4_dev/nerves/images/hello_nerves.fw .
  • クラウドにあるコンテナからファームウェアをもってきて
  • microSDカードを開発マシンに挿して
$ fwup hello_nerves.fw
  • あっ、fwupのインストール方法を説明していない。。。
  • クラウドのコンテナからRaspberry Piにmix upload(つまりssh)したいという話がでていた部分はこの記事ではなにもできていません

メモ

  • mixコマンドを実行したときに、
warning: the VM is running with native name encoding of latin1 which may cause Elixir to malfunction as it expects utf8. Please ensure your locale is set to UTF-8 (which can be verified by running "locale" in your shell)

という警告がちょいちょいでていました。

Nervesで一体何がつくれるの?

おまけ2 どうやってコンテナの中の.exを編集するの?

Wrapping Up

  • @takasehideki 先生のDockerfileは書き換えずにそのままで対応できるもっとスマートな方法があるかもしれません
  • クラウドのコンテナからRaspberry Piにmix upload(つまりssh)したい!? というような話には踏み込めていません
  • Enjoy Elixir !!!
  • この記事をきっかけにNervesに興味をもっていただけましたら、ぜひNervesJPのSlackでお待ちしています!
    • 愉快なfolksたちが歓迎します!!!

明日は、@kosuketakeiさんの文系出身業務経験なしのプログラマーがDocker使ってみた です。引き続きお楽しみください。