I'll modify both files to automatically update Flutter to the latest stable version. The key changes will use Flutter's built-in flutter upgrade command and ensure we're always on the stable channel.
Modified Dockerfile
# Use a recent Ubuntu LTS version as the base image
FROM ubuntu:22.04
# Avoid interactive prompts during package installation
ENV DEBIAN_FRONTEND=noninteractive
# Install essential packages:
# - git: Needed to clone the Flutter repository
# - curl, wget, unzip: For downloading tools and files
# - build-essential: Basic compilation tools (gcc, make, etc.)
# - clang, cmake: Often needed for native Flutter builds (especially desktop)
# - bash-completion: Useful for command-line autocompletion
RUN apt-get update && apt-get install -y \
git \
curl \
wget \
unzip \
build-essential \
clang \
cmake \
bash-completion \
--no-install-recommends && \
rm -rf /var/lib/apt/lists/*
# Set up environment variables for Flutter and Android SDK paths
ENV FLUTTER_ROOT=/usr/local/flutter
ENV PATH="${FLUTTER_ROOT}/bin:${PATH}"
ENV ANDROID_SDK_ROOT=/usr/local/android-sdk
ENV PATH="${ANDROID_SDK_ROOT}/cmdline-tools/latest/bin:${ANDROID_SDK_ROOT}/platform-tools:${PATH}"
# Install Flutter SDK from the stable branch
# We clone the entire repository (not just depth 1) to allow proper git operations for updates
RUN git clone https://github.com/flutter/flutter.git $FLUTTER_ROOT --branch stable
# Ensure we're on the stable channel and get the initial setup
RUN bash -c "cd $FLUTTER_ROOT && flutter channel stable && flutter doctor -v"
# Install Android SDK Command-Line Tools and essential platform tools
RUN mkdir -p $ANDROID_SDK_ROOT && \
cd $ANDROID_SDK_ROOT && \
wget -q https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip -O /tmp/cmdline-tools.zip && \
unzip -q /tmp/cmdline-tools.zip -d $ANDROID_SDK_ROOT && \
mv $ANDROID_SDK_ROOT/cmdline-tools $ANDROID_SDK_ROOT/cmdline-tools/latest && \
rm /tmp/cmdline-tools.zip && \
# Install platform-tools (for adb) and a specific Android SDK Platform
yes | sdkmanager --licenses && \
sdkmanager --update && \
sdkmanager "platform-tools" "platforms;android-33" "build-tools;33.0.1" "emulator" "system-images;android-33;google_apis;x86_64"
# Create a non-root user for security and better practice
RUN groupadd --system --gid 1000 developer && \
useradd --system --gid 1000 --uid 1000 --shell /bin/bash --create-home developer
# Create workspace directory
RUN mkdir -p /workspace
# Create a script for updating Flutter to the latest stable version
# This script will be used in postCreateCommand and can be run manually
RUN cat > /usr/local/bin/update-flutter.sh << 'EOF'
#!/bin/bash
set -e
echo "🔄 Updating Flutter to the latest stable version..."
# Ensure we're in the Flutter directory
cd $FLUTTER_ROOT
# Switch to stable channel (in case we're on another channel)
echo "📡 Switching to stable channel..."
flutter channel stable
# Update Flutter to the latest stable version
echo "⬆️ Upgrading Flutter..."
flutter upgrade --force
# Update pub dependencies cache
echo "📦 Updating pub cache..."
flutter pub cache repair
# Run doctor to verify everything is working
echo "🩺 Running flutter doctor..."
flutter doctor -v
# Precache artifacts for faster subsequent builds
echo "💾 Precaching artifacts..."
flutter precache --universal
echo "✅ Flutter update complete!"
echo "📋 Current Flutter version:"
flutter --version
EOF
# Make the update script executable
RUN chmod +x /usr/local/bin/update-flutter.sh
# Ensure the developer user owns the workspace and Flutter/Android SDK directories
RUN chown -R developer:developer /usr/local/flutter \
/usr/local/android-sdk \
/workspace \
/home/developer
# Switch to the non-root user
USER developer
# Set the default directory when the container starts
WORKDIR /workspace
# Create a simple .bashrc for the developer user with helpful aliases
RUN echo 'alias flutter-update="update-flutter.sh"' >> /home/developer/.bashrc && \
echo 'alias fdr="flutter doctor"' >> /home/developer/.bashrc && \
echo 'alias fv="flutter --version"' >> /home/developer/.bashrc && \
echo 'echo "🚀 Flutter Development Environment Ready!"' >> /home/developer/.bashrc && \
echo 'echo "💡 Use '\''flutter-update'\'' to update to the latest stable version"' >> /home/developer/.bashrc && \
echo 'echo "💡 Use '\''fdr'\'' for flutter doctor, '\''fv'\'' for version info"' >> /home/developer/.bashrc
Modified .devcontainer/devcontainer.json
{
"name": "Flutter Development Environment (Auto-Update)",
"build": {
"dockerfile": "Dockerfile"
},
// Settings for VS Code within the container
"settings": {
"dart.flutterSdkPath": "/usr/local/flutter",
"files.eol": "\n",
// Enable Flutter hot reload for better development experience
"dart.previewHotReloadOnSaveWatcher": true,
// Show Flutter outline
"dart.showTodos": true
},
// List of VS Code extensions to install automatically inside the container
"extensions": [
"dart-lang.dart-code", // The official Dart & Flutter extension
"eamodio.gitlens", // Enhanced Git integration
"ms-vscode.vscode-json", // Better JSON support for pubspec files
"bradlc.vscode-tailwindcss" // Useful if you're using Tailwind CSS in Flutter web
],
// Commands to run after the container is created
// This will update Flutter to the latest stable version every time the container starts
"postCreateCommand": "update-flutter.sh",
// Commands to run each time the container starts (not just on creation)
// This ensures Flutter is always up-to-date when you reopen the container
"postStartCommand": "bash -c 'echo \"🔍 Checking Flutter status...\" && flutter --version && echo \"💡 Run '\''flutter-update'\'' to update to the latest version if needed\"'",
// Run the commands in the container as this user
"remoteUser": "developer",
// Ports to forward from the container to the host machine
"forwardPorts": [
8080, // Default port for Flutter web apps
8081, // Alternative web port
5000, 5001, 5002, 5003, 5004, 5005, // Flutter debugging/device discovery
9100, // Flutter DevTools
9101 // Alternative DevTools port
],
// Mount the local workspace folder into the container
"workspaceMount": "source=${localWorkspaceFolder},target=/workspace,type=bind,consistency=cached",
// Set the working directory inside the container
"workspaceFolder": "/workspace",
// Lifecycle scripts - these run at different container lifecycle events
"onCreateCommand": "echo '🏗️ Container created! Flutter will be updated to the latest stable version...'",
// Features to install (alternative to manually installing in Dockerfile)
// Uncomment if you want additional development tools
// "features": {
// "ghcr.io/devcontainers/features/github-cli:1": {},
// "ghcr.io/devcontainers/features/node:1": {}
// },
// Environment variables available inside the container
"containerEnv": {
"FLUTTER_ROOT": "/usr/local/flutter",
"ANDROID_SDK_ROOT": "/usr/local/android-sdk"
},
// Additional container configuration
"runArgs": [
// Increase shared memory size for better performance
"--shm-size=2g"
],
// Mount additional volumes if needed (e.g., for persistent pub cache)
"mounts": [
// Persist Flutter pub cache across container rebuilds for faster package downloads
"source=flutter-pub-cache,target=/home/developer/.pub-cache,type=volume",
// Persist Android SDK downloads and cache
"source=android-sdk-cache,target=/home/developer/.android,type=volume"
]
}
Key Changes Explained:
Dockerfile Changes:
-
Removed --depth 1 from the git clone: We need the full git history to properly use flutter upgrade, which relies on git operations to update to the latest stable version.
-
Added update-flutter.sh script: This custom script handles the complete Flutter update process:
- Switches to the stable channel (
flutter channel stable)
- Upgrades to the latest stable version (
flutter upgrade --force)
- Repairs the pub cache (
flutter pub cache repair)
- Runs
flutter doctor to verify everything works
- Precaches artifacts for better performance (
flutter precache --universal)
-
Added helpful bash aliases: When you open a terminal, you'll see helpful commands:
flutter-update: Runs the update script
fdr: Shortcut for flutter doctor
fv: Shows Flutter version info
devcontainer.json Changes:
-
postCreateCommand: Now runs update-flutter.sh automatically when the container is first created, ensuring you always start with the latest stable Flutter version.
-
postStartCommand: Runs every time you start/restart the container, showing the current Flutter version and reminding you how to update.
-
Added persistent volumes:
flutter-pub-cache: Persists downloaded pub packages across container rebuilds
android-sdk-cache: Persists Android SDK cache and settings
-
Enhanced port forwarding: Added more ports commonly used by Flutter development tools.
-
Container environment variables: Explicitly sets Flutter and Android SDK paths as environment variables.
How the Auto-Update Works:
-
On first container creation: The postCreateCommand automatically runs update-flutter.sh, which updates Flutter to the latest stable version.
-
Manual updates: You can run flutter-update (or update-flutter.sh) anytime to get the latest stable version.
-
Smart updating: The script uses flutter upgrade --force which:
- Downloads the latest stable Flutter SDK
- Updates the Dart SDK to the compatible version
- Updates cached artifacts
- Handles any migration steps automatically
-
Persistent cache: Package downloads are cached in Docker volumes, so subsequent updates and package installations are faster.
Usage:
After setting up with these files:
- Automatic: Flutter updates to latest stable when the container is first created
- Manual update: Run
flutter-update in the terminal anytime
- Check version: Run
fv to see current Flutter version
- Verify setup: Run
fdr to run flutter doctor
This setup ensures you're always working with the latest stable Flutter version while maintaining a consistent, reproducible development environment.