To automatically update Flutter to the latest stable version when the container is built, we'll modify the Dockerfile to include a script that checks for updates and applies them if necessary. We'll also add a step to the devcontainer.json to ensure the update script runs every time the container starts.
Updated Dockerfile
# Use Ubuntu 22.04 as our base - it's stable and well-supported
FROM ubuntu:22.04
# Prevent interactive prompts during package installation
ARG DEBIAN_FRONTEND=noninteractive
# Set up environment variables for Flutter
ENV FLUTTER_HOME="/opt/flutter"
ENV PATH="$FLUTTER_HOME/bin:$PATH"
ENV ANDROID_HOME="/opt/android-sdk"
ENV PATH="$ANDROID_HOME/cmdline-tools/latest/bin:$ANDROID_HOME/platform-tools:$PATH"
# Install essential system packages
RUN apt-get update && apt-get install -y \
# Basic utilities
curl \
git \
unzip \
wget \
# Build tools and libraries needed for Flutter
build-essential \
libglu1-mesa \
# Java 17 (required for Android development)
openjdk-17-jdk \
# Clean up package cache to reduce image size
&& rm -rf /var/lib/apt/lists/*
# Create a non-root user for development (security best practice)
RUN useradd -m -s /bin/bash developer && \
usermod -aG sudo developer
# Download and install Flutter SDK
RUN git clone https://github.com/flutter/flutter.git $FLUTTER_HOME && \
# Switch to stable channel for reliability
cd $FLUTTER_HOME && git checkout stable && \
# Make Flutter accessible to all users
chown -R developer:developer $FLUTTER_HOME
# Download and set up Android SDK (needed for Android app development)
RUN mkdir -p $ANDROID_HOME && \
cd $ANDROID_HOME && \
# Download Android command line tools
wget https://dl.google.com/android/repository/commandlinetools-linux-9477386_latest.zip && \
unzip commandlinetools-linux-9477386_latest.zip && \
rm commandlinetools-linux-9477386_latest.zip && \
# Organize tools in expected directory structure
mkdir -p cmdline-tools/latest && \
mv cmdline-tools/* cmdline-tools/latest/ 2>/dev/null || true && \
# Set proper ownership
chown -R developer:developer $ANDROID_HOME
# Switch to the developer user for remaining operations
USER developer
# Add a script to update Flutter to the latest stable version
RUN echo '#!/bin/bash\n\
cd /opt/flutter && git pull && git checkout stable && git pull' > /usr/local/bin/flutter-update && \
chmod +x /usr/local/bin/flutter-update
# Accept Android SDK licenses and install essential components
RUN yes | sdkmanager --licenses && \
# Install Android SDK platform tools (adb, fastboot, etc.)
sdkmanager "platform-tools" && \
# Install Android 33 (recent stable version)
sdkmanager "platforms;android-33" && \
# Install build tools
sdkmanager "build-tools;33.0.2"
# Pre-download Flutter dependencies to speed up first run
RUN flutter doctor && \
flutter precache
# Set the working directory where your project will be mounted
WORKDIR /workspace
# Default command when container starts
CMD ["/bin/bash"]
Updated devcontainer.json
{
"name": "Flutter Development Environment",
// Build configuration - tells VS Code to build our custom Dockerfile
"build": {
"dockerfile": "Dockerfile",
"context": "."
},
// Mount your project files into the container's /workspace directory
"workspaceFolder": "/workspace",
// VS Code extensions to automatically install in the container
"customizations": {
"vscode": {
"extensions": [
"Dart-Code.dart-code",
"Dart-Code.flutter",
"ms-vscode.vscode-json",
"bradlc.vscode-tailwindcss",
"usernamehw.errorlens",
"esbenp.prettier-vscode"
],
"settings": {
"dart.flutterSdkPath": "/opt/flutter",
"dart.checkForSdkUpdates": false,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": true
}
}
}
},
// Forward ports from container to your local machine
"forwardPorts": [
8080,
3000,
5000
],
// Commands to run after the container is created
"postCreateCommand": "flutter-update && flutter doctor -v",
// Keep the container running even when VS Code is closed
"shutdownAction": "stopContainer",
// Run container as the developer user (not root)
"remoteUser": "developer",
// Mount additional volumes for persistent data
"mounts": [
"source=vscode-extensions,target=/home/developer/.vscode-server/extensions,type=volume",
"source=flutter-pub-cache,target=/home/developer/.pub-cache,type=volume"
],
// Environment variables available in the container
"containerEnv": {
"FLUTTER_ROOT": "/opt/flutter",
"PUB_CACHE": "/home/developer/.pub-cache"
}
}
Explanation of Changes
Dockerfile:
- Added a script
/usr/local/bin/flutter-update that pulls the latest changes from the Flutter repository and switches to the stable branch.
- Made the script executable with
chmod +x.
devcontainer.json:
- Modified the
postCreateCommand to run flutter-update before flutter doctor -v. This ensures that Flutter is updated to the latest stable version every time the container is started.
Usage
- When you start the container, it will automatically check for and apply any updates to the Flutter SDK, ensuring you're always working with the latest stable version.