1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Mac miniでもサクサク動くRust開発環境をPodman + Dev Containersで構築する

Posted at

TL;DR

  • Mac miniの限られたメモリでもRust開発を快適にする方法
  • Podman + VS Code Dev Containersでローカル環境を汚さない
  • rust-analyzerが重い問題をMakefileで解決
  • 811MBの軽量イメージで容量も節約

モチベーション

Mac miniでなるべく軽量なrust開発環境を構築したい。でも以下のような問題があるので解消したい。

  • rust-analyzerがメモリを食いすぎてクラッシュする
  • ローカルのRustツールチェーンが容量を圧迫する
  • 複数のプロジェクトで異なるRustバージョンを管理したい
  • Docker Desktopは重いからPodmanを使いたい

環境

  • macOS(Mac mini 想定)
  • Podman 5.5.2+
  • VS Code + Dev Containers拡張

1. Podman環境の準備

Podmanのインストールとセットアップ

# Homebrewでインストール
brew install podman

# VM初期化・起動
podman machine init --now

# または既存VMを起動
podman machine start

VS Code用のDocker互換ソケット設定

Dev ContainersでPodmanを使うため、Docker互換のAPIソケットを設定します。

# ソケットパスを取得
podman machine inspect --format '{{.ConnectionInfo.PodmanSocket.Path}}'

~/.zshrcに動的な設定を追加:

# Use Podman as a drop-in replacement for Docker CLI
alias docker=podman

# Configure Docker-compatible API socket for Dev Containers using Podman
# Automatically detect the current Podman machine socket on macOS
if command -v podman >/dev/null 2>&1; then
  PODMAN_SOCK_PATH=$(podman machine inspect --format '{{.ConnectionInfo.PodmanSocket.Path}}' 2>/dev/null)
  if [ -n "$PODMAN_SOCK_PATH" ] && [ -S "$PODMAN_SOCK_PATH" ]; then
    export DOCKER_HOST="unix://$PODMAN_SOCK_PATH"
  fi
fi

2. コンテナイメージの選択

容量を抑えつつ安定性を重視してrust:slim-bookwormを選択しました。

イメージ サイズ 特徴
rust:latest 1.48GB 標準、サイズが大きい
rust:alpine 986MB 軽量だがmusl libcで互換性問題あり
rust:slim-bookworm 811MB 軽量かつ高互換性

3. Containerfile(Dockerfile)の作成

# Development Containerfile for Rust
# Using Debian slim for better compatibility (avoiding musl issues)

FROM rust:slim-bookworm AS dev
WORKDIR /app

# Install make and other development tools
RUN apt-get update && apt-get install -y \
    make \
    && rm -rf /var/lib/apt/lists/*

# Install essential Rust components for development
RUN rustup component add rust-src rustfmt clippy rust-analyzer

# Pre-fetch dependencies to leverage Docker/Podman layer cache
COPY Cargo.toml Cargo.lock ./
RUN mkdir -p src \
    && echo 'fn main() { println!("build cache"); }' > src/main.rs \
    && cargo fetch

# Copy the actual source
COPY . .

# Default command for iterative development
CMD ["cargo", "run"]

4. VS Code Dev Containers設定

.devcontainer/devcontainer.json

{
  "name": "todo_app dev",
  "build": {
    "dockerfile": "../Containerfile",
    "context": ".."
  },
  "overrideCommand": true,
  "mounts": [
    "type=volume,source=cargo-registry,destination=/usr/local/cargo/registry",
    "type=volume,source=cargo-git,destination=/usr/local/cargo/git",
    "type=volume,source=cargo-target,destination=/usr/local/cargo/target"
  ],
  "postCreateCommand": "rustup update && rustup component add rust-src rustfmt clippy rust-analyzer && cargo fetch",
  "remoteUser": "root",
  "customizations": {
    "vscode": {
      "extensions": [
        "serayuzgur.crates",
        "tamasfe.even-better-toml"
      ],
      "settings": {
        "terminal.integrated.defaultProfile.linux": "bash",
        "rust-analyzer.server.path": "/usr/local/cargo/bin/rust-analyzer",
        "rust-analyzer.server.extraEnv": {
          "RUST_LOG": "warn"
        },
        "rust-analyzer.lens.enable": false,
        "rust-analyzer.inlayHints.enable": false,
        "rust-analyzer.completion.limit": 50,
        "rust-analyzer.checkOnSave.enable": false
      }
    }
  }
}

.vscode/settings.json

{
  "dev.containers.dockerPath": "podman"
}

ポイント

  • Named volumes: Cargoの依存関係をキャッシュして高速化
  • rust-analyzer軽量化: メモリ使用量を抑える設定
  • Podman指定: DockerのかわりにPodmanを使用

5. メモリ節約のMakefile開発ワークフロー

rust-analyzerが重い問題を解決するため、必要な時だけツールを実行するMakefileを作成しました。

Makefile

# Rust Development Makefile
# Usage: make <target>

.PHONY: help build run test clean check fmt clippy fix install watch release

# Default target
help:
	@echo "Available targets:"
	@echo "  build     - Build the project (debug)"
	@echo "  release   - Build the project (release)"
	@echo "  run       - Build and run the project"
	@echo "  test      - Run all tests"
	@echo "  check     - Quick compile check (fastest)"
	@echo "  fmt       - Format code"
	@echo "  clippy    - Run linter"
	@echo "  fix       - Auto-fix clippy issues"
	@echo "  clean     - Clean build artifacts"
	@echo "  install   - Install dependencies"
	@echo "  watch     - Watch for changes and rebuild"
	@echo "  all       - Run check + clippy + fmt + test"

# Build commands
build:
	cargo build

release:
	cargo build --release

run:
	cargo run

# Testing
test:
	cargo test

# Development helpers
check:
	@echo "🔍 Checking compilation..."
	cargo check

fmt:
	@echo "🎨 Formatting code..."
	cargo fmt

clippy:
	@echo "📎 Running clippy..."
	cargo clippy -- -D warnings

fix:
	@echo "🔧 Auto-fixing issues..."
	cargo clippy --fix --allow-dirty --allow-staged
	cargo fmt

# Maintenance
clean:
	@echo "🧹 Cleaning build artifacts..."
	cargo clean

install:
	@echo "📦 Installing dependencies..."
	cargo fetch

# Comprehensive check
all: check clippy fmt test
	@echo "✅ All checks passed!"

# Quick development loop
dev: check clippy
	@echo "✅ Quick dev check complete!"

# Docker/Podman specific
container-build:
	podman build -t todo-app-dev -f Containerfile .

container-run:
	podman run --rm -it todo-app-dev

VS Code タスク設定(.vscode/tasks.json)

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "rust: check",
            "type": "shell",
            "command": "make",
            "args": ["check"],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "silent",
                "focus": false,
                "panel": "shared"
            },
            "problemMatcher": "$rustc"
        },
        {
            "label": "rust: run",
            "type": "shell",
            "command": "make",
            "args": ["run"],
            "group": "build",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            }
        },
        {
            "label": "rust: dev check",
            "type": "shell",
            "command": "make",
            "args": ["dev"],
            "group": "build",
            "presentation": {
                "echo": true,
                "reveal": "silent",
                "focus": false,
                "panel": "shared"
            },
            "problemMatcher": "$rustc"
        }
    ]
}

6. 使い方

基本ワークフロー

  1. VS Codeでプロジェクトを開く
  2. Dev Containers: Reopen in Container
  3. コンテナ内でMakefileコマンドを使用
# 開発時の基本チェック
make dev

# コード実行
make run

# コミット前の全チェック
make all

# 自動修正
make fix

VS Codeからの実行

  • Ctrl+Shift+PTasks: Run Task → 使いたいタスクを選択
  • Ctrl+Shift+B でデフォルトタスク(check)を実行

7. メリット・デメリット

✅ メリット

  • メモリ節約: rust-analyzerのクラッシュなし
  • 容量節約: 811MBの軽量イメージ
  • 環境独立: ローカル環境を汚さない
  • 高速チェック: 必要な時だけコンパイル確認
  • 統合環境: VS Codeでエラー表示・タスク実行

❌ デメリット

  • リアルタイム補完なし: rust-analyzerの恩恵を受けられない
  • 初期セットアップ: 設定ファイルが複数必要
  • VM経由のオーバーヘッド: ネイティブより若干遅い

8. 実行結果

実際にMac miniで動作させた結果:

root@40665b92555:/workspaces/todo_app# make dev
🔍 Checking compilation...
cargo check
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s
📎 Running clippy...
cargo clippy -- -D warnings
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.45s
✅ Quick dev check complete!
  • コンパイルチェック: 0.04秒
  • Clippyチェック: 0.45秒

まとめ

この構成により、Mac miniの限られたリソースでも快適なRust開発環境を構築できました。

  • 軽量: 811MBのコンテナイメージ
  • 高速: 必要な時だけのチェック実行
  • 安定: rust-analyzerクラッシュなし
  • 統合: VS Codeとの連携も完璧

メモリに制約があるマシンでも、工夫次第で十分な開発環境が作れることを実証できたと思います。
🦀✨ Happy Hacking with Rust! ✨🦀

参考リンク


1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?