Develop Nerves apps with Azure Container Instances

This article is the 7th day of "Docker Advent Calendar 2020". I filled it because it was open. The day before, @ c3drive's Building a container environment on AWS

Introduction

-Nerves is -Elixir's IoT is now and young cool awesome guy: rocket: -You can enjoy IoT development with a programming language called Elixir. -Nerves requires various environment settings ――It's not so once you get used to it, but there are various traps to do for the first time. --So, @takasehideki-sensei said IoT # 4.1.2 with Elixir: [How to use] Docker (and VS Code) only! Improve Nerves development environment has been released. ――I want an acquaintance to try it right away! -Can't this container be run on Azure Container Instances at a group meeting called NervesJP? I heard a story like that, so I thought it was a good story.

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"]

-Enabled ssh connection based on https://github.com/NervesJP/docker-nerves/blob/v0.2/Dockerfile --For the ssh connection, I referred to @ kuboshu83's [Introduction to Docker] Setting memo for accessing the container with ssh.

$ mkdir config_files
$ cp ~/.ssh/id_rsa.pub config_files/
$ docker build -t docker-nerves .

――After creating the image, the rest of the procedure is as follows ―― 1. Register the image in Azure Container Registry --2. Set Azure Container Instances to use the custom image. ―― 3. Connect with ssh and develop Nerves application ――Hereafter, we will take a closer look.

1. Register the image in Azure Container Registry

--Quick Start: Create an Azure Container Registry Using the Azure Portal (https://docs.microsoft.com/ja-jp/azure/container-registry/container-registry-get-started-portal) -: Point_up :: point_up_tone :: point_up_tone2 :: point_up_tone3 :: point_up_tone4 :: point_up_tone5: You should do as it is written here. ――I didn't use any Azure CLI to push images --There is a page called "Quick Start" on the console of Azure Container Registry, so you can refer to it. --First, go to the page called "Access Keys" and enable the admin user --Use the username and password written there with docker login

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

--xxx is the name (value) you decided when you created Azure Container Registry.

2. Set Azure Container Instances (https://docs.microsoft.com/ja-jp/azure/container-instances/) to use that custom image

スクリーンショット 2020-12-16 0.02.11.png

-Quick Start: Deploy a container instance in Azure using the Azure portal --The network should be public and port 22 should be open. --That's all you need to set, and then Next`` Next --The container will start up after a while, so remember the assigned IP address. --Hereafter, assume that it is 20.43.93.225 --This container was deleted after writing the article

3. Connect with ssh and develop Nerves app

$ ssh [email protected]

: point_up :: point_up_tone :: point_up_tone2 :: point_up_tone3 :: point_up_tone4 :: point_up_tone5: Enter the container

Operation in a container

$ 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

So, the firmware is safely in the container You can create /root/hello_nerves/_build/rpi4_dev/nerves/images/hello_nerves.fw: fire :: fire :: fire:

For other than Raspberry Pi 4, select a value from Targets.

Return to the development machine

$ scp [email protected]:/root/hello_nerves/_build/rpi4_dev/nerves/images/hello_nerves.fw .

--Bring firmware from a container in the cloud --Insert the microSD card into the development machine

$ fwup hello_nerves.fw

--Oh, I didn't explain how to install fwup. .. .. ――There --Install fwup :gift::gift::gift: --Nothing was done in this article where you mentioned that you wanted to mix upload (that is, ssh) from a cloud container to a Raspberry Pi.

Note

--When you execute the mix command

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)

There was a little warning.

What can you make with Nerves?

--You can make various things: rocket :: rocket :: rocket: --Introducing a simple example ――This is miso in the foreground -Nerves alarm created with Raspberry Pi 4 + Grove Base HAT for Raspberry Pi + Grove Buzzer + Grove Button

https___qiita-image-store.s3.ap-northeast-1.amazonaws.com_0_131808_decc299d-a6e7-1e9d-2d0e-6bb58c57b476.jpeg

Bonus 2 How do you edit the .ex in the container?

-You can go with Visual Studio Code -Please refer to Remote Development using SSH ――You can proceed with development as if you were editing a local file like this.

スクリーンショット 2020-12-16 8.42.39.png

Wrapping Up :christmas_tree::santa::santa_tone1::santa_tone2::santa_tone3::santa_tone4::santa_tone5::christmas_tree: --There may be a smarter way to deal with @ takasehideki's Dockerfile as it is without rewriting. --I want to mix upload (that is, ssh) from a cloud container to a Raspberry Pi! ?? I haven't stepped into a story like that

https___qiita-user-contents.imgix.net_https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F240349%2F5ef22bb9-f357-778c-1bff-b018cce54948.png_ixlib=rb-1.2.png

Tomorrow is @ kosuketakei's A programmer from a liberal arts background who has no work experience tried using Docker. Please continue to enjoy.

Recommended Posts

Develop Nerves apps with Azure Container Instances
Azure Container Instances! Lightweight and explosive container usage!
I wanted to develop PHP with vscode remote container
Comparison of Web App for Containers and Azure Container Instances