LoginSignup
1
0

More than 3 years have passed since last update.

Azure Functions × Node.js × Typescript をローカルの仮想環境上のDockerで動かして VSCodeからデバッグ実行したメモ

Posted at

概要

Azureのチュートリアル「Visual Studio Code から Azure Functions をデプロイする」を行っていたところ、
関数のローカル実行でエラーとなった。
Nodeのバージョンによるものだったため、関数のローカル実行をDocker上で行うようにした。
デバッグ実行の設定を調べるのに時間がかかったため、備忘録として残す。

この時点のソース

環境

  • Windows 10 Home
  • chocolatey 0.10.15
  • Vagrant 2.2.10
  • virtualbox 6.1.16
  • Ubuntu 20.04 LTS
  • Docker version 19.03.13, build 4484c46d9d
  • docker-compose version 1.27.4, build 40524192

※Vagrantは192.168.50.10のIPで立ち上げ。

ツールの準備

choco install azure-functions-core-tools-3 --params "'/x64'" -y

ソース

Dockerの設定

  • 2020.11.01現在、Azure Functionsはnode v12までが対象。
  • node.jsでAzure Functionsを動かせるようにこちらにもツールをインストール
  • package.jsonはプロジェクトの作成で作ったものを利用している。
docker/functions/Dockerfile
# docker-hubからnode入りコンテナを取得
# https://hub.docker.com/_/node/
FROM node:12.19.0
WORKDIR /app
# ツールのインストール
RUN npm i -g azure-functions-core-tools@3 --unsafe-perm true
# Functionsのインストール
COPY ./package.json /app/package.json
RUN npm install
docker/docker-compose.yml
version: "3.8"
services:
  az-functions:
    build: ./functions
    volumes:
      - ../HttpExample:/app/HttpExample
      - ../tsconfig.json:/app/tsconfig.json
      - ../local.settings.json:/app/local.settings.json
    ports:
      - 9229:9229
      - 7071:7071
    working_dir: /app
    command: [npm, run, start]

デバッグ設定

vscodeからデバッグを行うとき、プロジェクトの作成で作成されたとおりではできなかった。

  • 以下を追記している
    • addressに仮想環境のIPアドレス
    • remoteRootにdocker上のパス
.vscode/launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Vagrant - Docker: Attach to Node Functions",
      "type": "node",
      "request": "attach",
      "remoteRoot": "/app",
      "address": "192.168.50.10",
      "port": 9229,
      "protocol": "auto",
    }
  ]
}
  • docker上で動かすときに、0.0.0.0で動かさないとattachに失敗する。
local.settings.json
{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "languageWorkers:node:arguments": "--inspect=0.0.0.0:9229"
  }
}
  • 再度docker-compose upで動かしなおす
  • visual studioで「F5」キーを押し、ブレイクポイントで止まることを確認する
  • 懸念点
    • デバッグ実行時、worker-bundle.jsの7148行目のstr = JSON.parse(str);でエラー。。。 ## 参考

Docker + Node.js のデバッグ Visual Studio Code編
Visual Studio Code から Azure Functions をデプロイする
Functions ローカルデバッグ
Azure Functions Core Tools
node debug guide
Debianでsourcelist内のhttpsが取得出来ないとき
lsb_release: command not found in latest Ubuntu Docker container
Visual Studio Codeで、Node.jsアプリケーションをデバッグする(ローカルプロセスアタッチ/リモートデバッグ)
How to set inspect port for nodejs
Visual Studio Codeで始めるデバッグ可能なnode.jsアプリ開発(express+ejsでhelloworldするとこまでのチュートリアル)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0