水曜日, 6月 11, 2025
- Advertisment -
ホームニューステックニュースcontainer-useやってみた #ClaudeCode - Qiita

container-useやってみた #ClaudeCode – Qiita



container-useやってみた #ClaudeCode - Qiita

ロゴが可愛いと評判のcontainer-useをやってみました。

GitHubリポジトリはこちら

container-useとは

READMEより

Container Use lets each of your coding agents have their own containerized environment. Go from babysitting one agent at a time to enabling multiple agents to work safely and independently with your preferred stack.

コンテナの使用により、コーディングエージェントごとにコンテナ化された環境を持つことができます。エージェントを個別に管理するのではなく、複数のエージェントが好みのスタックを使用して安全かつ独立して作業できるようにすることができます。

MCPサーバーとして利用が可能です ので、Amazon Q Developer CLIと組み合わせて使ってみました。
Claude Codeとかでも、おそらく同じような感じになると思います。

container-useバイナリのインストール

Dockerがインストールされている必要があります。

git clone https://github.com/dagger/container-use.git
cd container-use

ビルドしてインストールします

ビルドはコンテナ内で行われているようです。

build.sh

#!/usr/bin/env bash
set -uoe pipefail

: ${TARGETPLATFORM:=local}
which docker >/dev/null || ( echo "Please follow instructions to install Docker at https://docs.docker.com/get-started/get-docker/"; exit 1 )
docker build --platform "$TARGETPLATFORM" -o . .
ls cu

環境のセットアップ

Ubuntu(WSL上)で試しました。

container-useの動作にはGitリポジトリが必要です。

mkdir work
cd work

git init
echo "Hello" > README.md
git add .
git commit -m "initial"

続いて、MCPサーバーの設定です。workディレクトリ内でqを起動するので、work/.amazonq/mcp.jsonを作成し、設定を記述します。

work/.amazonq/mcp.json

{
    "mcpServers": {
        "container-use": {
            "command": "cu",
            "args": [
                "stdio"
            ]
        }
    }
}

そして、AmazonQ.mdを作成します。記述内容はcontainer-useが用意してくれているルールです。

また、Amazon Q Developer CLIの既存のファイル書き込みなどが誤動作することがあったので、それを防ぐ記述を追加しています。

Claude Codeの場合はCLAUDE.mdに記述します

work/AmazonQ.md

ALWAYS use ONLY Environments for ANY and ALL file, code, or shell operations—NO EXCEPTIONS—even for simple or generic requests.

DO NOT install or use the git cli with the environment_run_cmd tool. All environment tools will handle git operations for you. Changing ".git" yourself will compromise the integrity of your environment.

You MUST inform the user how to view your work using `git checkout `. Failure to do this will make your work inaccessible to others.

---

以下のツールは使用禁止です。使ってはいけません。

- execute_bash
- fs_read
- fs_write
- report_issue
- use_aws

使っていいのは以下のツールのみです。

- container_use___environment_checkpoint
- container_use___environment_file_delete
- container_use___environment_file_list
- container_use___environment_file_read
- container_use___environment_file_write
- container_use___environment_open
- container_use___environment_run_cmd
- container_use___environment_update

これで準備環境です。

動作確認

それでは、q chatでAmazon Q Developer CLIを起動します。

Claude Sonnet 4使えるよ

image.png

READMEに記載のサンプルを試します。

create a simple flask app

ちょっと長いですが、動作のログがこちらです。

動作のログ
> create a simple flask app
I'll help you create a simple Flask application. First, let's set up a development environment with Python and Flask, then create the necessary files.

Let's start by creating a Python environment:


🛠️  Using tool: environment_open from mcp server container_use
 ⋮ 
 ● Running environment_open with the param:
 ⋮  {
 ⋮    "arguments": {
 ⋮      "explanation": "Creating a Python environment for Flask application development",
 ⋮      "name": "flask-app",
 ⋮      "source": "/workspaces/workspace/container-use/work"
 ⋮    },
 ⋮    "name": "environment_open"
 ⋮  }
Allow this action? Use 't' to trust (always allow) this tool for the session. [y/n/t]:

> t

 ⋮ 
 ● Completed in 3.43s
Now let's update the environment with Python and Flask:


🛠️  Using tool: environment_update from mcp server container_use
 ⋮ 
 ● Running environment_update with the param:
 ⋮  {
 ⋮    "arguments": {
 ⋮      "base_image": "python:3.11-slim",
 ⋮      "environment_id": "flask-app/touching-collie",
 ⋮      "explanation": "Setting up Python environment with Flask for web application development",
 ⋮      "instructions": "Python Flask development environment with Flask installed. Use 'python app.py' to run the Flask application. The app will be available on port 5000.",
 ⋮      "secrets": [],
 ⋮      "setup_commands": [
 ⋮        "pip install flask"
 ⋮      ]
 ⋮    },
 ⋮    "name": "environment_update"
 ⋮  }
Allow this action? Use 't' to trust (always allow) this tool for the session. [y/n/t]:

> t

 ⋮ 
 ● Completed in 10.287s
Now let's create a simple Flask application:


🛠️  Using tool: environment_file_write from mcp server container_use
 ⋮ 
 ● Running environment_file_write with the param:
 ⋮  {
 ⋮    "arguments": {
 ⋮      "contents": "from flask import Flask, render_template, jsonify\n\n# Create Flask application instance\napp = Flask(__name__)\n\n# Home route\[email protected]("https://qiita.com/")\ndef home():\n    return render_template('index.html')\n\n# About route\[email protected]("https://qiita.com/about")\ndef about():\n    return render_template('about.html')\n\n# API route that returns JSON\[email protected]('/api/hello')\ndef api_hello():\n    return jsonify({\n        'message': 'Hello from Flask API!',\n        'status': 'success'\n    })\n\n# Route with parameter\[email protected]('/user/')\ndef user_profile(name):\n    return f''\n\nif __name__ == '__main__':\n    # Run the app in debug mode\n    app.run(debug=True, host="0.0.0.0", port=5000)",
 ⋮      "environment_id": "flask-app/touching-collie",
 ⋮      "explanation": "Creating the main Flask application file with basic routes",
 ⋮      "target_file": "app.py"
 ⋮    },
 ⋮    "name": "environment_file_write"
 ⋮  }
Allow this action? Use 't' to trust (always allow) this tool for the session. [y/n/t]:

> t

 ⋮ 
 ● Completed in 0.357s
Let's create a templates directory and add HTML templates:


🛠️  Using tool: environment_run_cmd from mcp server container_use
 ⋮ 
 ● Running environment_run_cmd with the param:
 ⋮  {
 ⋮    "arguments": {
 ⋮      "command": "mkdir -p templates",
 ⋮      "environment_id": "flask-app/touching-collie",
 ⋮      "explanation": "Creating templates directory for HTML files"
 ⋮    },
 ⋮    "name": "environment_run_cmd"
 ⋮  }
Allow this action? Use 't' to trust (always allow) this tool for the session. [y/n/t]:

> t

 ⋮ 
 ● Completed in 1.118s
🛠️  Using tool: environment_file_write (trusted) from mcp server container_use
 ⋮ 
 ● Running environment_file_write with the param:
 ⋮  {
 ⋮    "arguments": {
 ⋮      "contents": "\n\n\n    \n    \n    {% block title %}Flask App{% endblock %}\n    \n\n\n    
\n \n {% block content %}{% endblock %}\n
\n\n", ⋮ "environment_id": "flask-app/touching-collie", ⋮ "explanation": "Creating base HTML template with common structure", ⋮ "target_file": "templates/base.html" ⋮ }, ⋮ "name": "environment_file_write" ⋮ } ⋮ ● Completed in 0.667s
🛠️  Using tool: environment_file_write (trusted) from mcp server container_use
 ⋮ 
 ● Running environment_file_write with the param:
 ⋮  {
 ⋮    "arguments": {
 ⋮      "contents": "{% extends \"base.html\" %}\n\n{% block title %}Home - Flask App{% endblock %}\n\n{% block content %}\n\n

This is a simple Flask application with the following features:

\n
    \n
  • Home page - You are here!
  • \n
  • About page - Learn more about this app
  • \n
  • API endpoint - Returns JSON data
  • \n
  • Dynamic routes - Try visiting /user/yourname
  • \n
\n\n

Quick Links

\n

\n Visit About Page | \n Try API Endpoint | \n User Profile Example\n

\n\n

Getting Started

\n

This Flask app demonstrates basic routing, templating, and API endpoints. \n You can extend it by adding more routes, database integration, or additional features.

\n{% endblock %}", ⋮ "environment_id": "flask-app/touching-collie", ⋮ "explanation": "Creating home page template", ⋮ "target_file": "templates/index.html" ⋮ }, ⋮ "name": "environment_file_write" ⋮ } ⋮ ● Completed in 0.676s
🛠️  Using tool: environment_file_write (trusted) from mcp server container_use
 ⋮ 
 ● Running environment_file_write with the param:
 ⋮  {
 ⋮    "arguments": {
 ⋮      "contents": "{% extends \"base.html\" %}\n\n{% block title %}About - Flask App{% endblock %}\n\n{% block content %}\n\n

This is a simple Flask web application that demonstrates:

\n\n

Features

\n
    \n
  • Template Inheritance - Using Jinja2 templates with base template
  • \n
  • Static Routes - Home and About pages
  • \n
  • Dynamic Routes - User profile pages with URL parameters
  • \n
  • JSON API - RESTful endpoint returning JSON data
  • \n
  • CSS Styling - Basic responsive design
  • \n
\n\n

Technology Stack

\n
    \n
  • Python 3.11
  • \n
  • Flask web framework
  • \n
  • Jinja2 templating engine
  • \n
  • HTML5 & CSS3
  • \n
\n\n

File Structure

\n
\nflask-app/\n├── app.py              # Main Flask application\n├── templates/          # HTML templates\n│   ├── base.html      # Base template\n│   ├── index.html     # Home page\n│   └── about.html     # About page\n└── requirements.txt   # Python dependencies\n

\n\n

← Back to Home

\n{% endblock %}",
⋮ "environment_id": "flask-app/touching-collie",
⋮ "explanation": "Creating about page template",
⋮ "target_file": "templates/about.html"
⋮ },
⋮ "name": "environment_file_write"
⋮ }

● Completed in 0.389s

Let's also create a requirements.txt file:


🛠️  Using tool: environment_file_write (trusted) from mcp server container_use
 ⋮ 
 ● Running environment_file_write with the param:
 ⋮  {
 ⋮    "arguments": {
 ⋮      "contents": "Flask==2.3.3\nWerkzeug==2.3.7\nJinja2==3.1.2",
 ⋮      "environment_id": "flask-app/touching-collie",
 ⋮      "explanation": "Creating requirements file for Python dependencies",
 ⋮      "target_file": "requirements.txt"
 ⋮    },
 ⋮    "name": "environment_file_write"
 ⋮  }
 ⋮ 
 ● Completed in 0.439s
Now let's run the Flask application:


🛠️  Using tool: environment_run_cmd (trusted) from mcp server container_use
 ⋮ 
 ● Running environment_run_cmd with the param:
 ⋮  {
 ⋮    "arguments": {
 ⋮      "background": true,
 ⋮      "command": "python app.py",
 ⋮      "environment_id": "flask-app/touching-collie",
 ⋮      "explanation": "Starting the Flask development server",
 ⋮      "ports": [
 ⋮        5000
 ⋮      ]
 ⋮    },
 ⋮    "name": "environment_run_cmd"
 ⋮  }
 ⋮ 
 ● Completed in 0.893s
Perfect! I've created a simple Flask application for you. Here's what I've built:

## Flask App Structure

The application includes:

1. Main Application (app.py):
   • Home route (/) - displays the main page
   • About route (/about) - shows information about the app
   • API route (/api/hello) - returns JSON data
   • Dynamic route (/user/) - personalized user pages

2. HTML Templates:
   • base.html - base template with navigation and styling
   • index.html - home page with app overview
   • about.html - detailed information about the app

3. Dependencies (requirements.txt) - lists Flask and related packages

## Features

• Template inheritance using Jinja2
• Responsive CSS styling
• JSON API endpoint
• Dynamic URL routing
• Navigation between pages

## Access Your App

Your Flask application is now running and accessible at:
• **External URL**: http://127.0.0.1:5669

You can visit these endpoints:
• / - Home page
• /about - About page  
• /api/hello - JSON API endpoint
• /user/yourname - Dynamic user profile (replace "yourname" with any name)

## View Your Code

To view and work with your Flask application code, use:
bash
git checkout flask-app/touching-collie


The app is running in debug mode, so any changes you make to the code will automatically reload the server. You can extend this basic Flask app by adding more 
routes, database integration, forms, or additional features as needed!

なにか作ってくれまして、http://localhost:5669/にアクセスするとWebアプリが出来上がってました。

で?なんなの?

Claude Codeなどをすでに使ってる方は、「で?なにが違うの?」と思ったと思います。

違うんです。ほら、ソースが存在しません。

image.png

ホラーではありません(笑)

Flaskはいずこへ。。。?

実は、Gitの中にいます。

image.png

masterブランチがこれまでの手順でinitしたブランチで、flask-app/touching-collieというブランチは私が作ったものではありません。

どこにあるかわかりませんがリモートブランチまであります。(どいうこと。。??)

flask-app/touching-collieブランチに切り替えると履歴が確認できました。

image.png

リモートブランチからプルすることで、無事ソースに出会うことができました。

image.png

これがcontainer-useなんですね。ローカルに影響を与えずソースコード生成と動作確認ができます。





Source link

Views: 0

RELATED ARTICLES

返事を書く

あなたのコメントを入力してください。
ここにあなたの名前を入力してください

- Advertisment -