-
Install Docker and VS Code:
-
Create a Flutter Project Directory:
mkdir my_flutter_project
cd my_flutter_project
mkdir .devcontainer
-
Create a Dockerfile in the .devcontainer directory:
# Use a recent Ubuntu LTS as the base image
FROM ubuntu:22.04
# Prevent interactive prompts during package installation
ARG DEBIAN_FRONTEND=noninteractive
# Set up environment variables for Flutter and Android SDK paths
ENV FLUTTER_HOME="/opt/flutter"
ENV ANDROID_SDK_ROOT="/opt/android-sdk"
ENV PATH="${FLUTTER_HOME}/bin:${ANDROID_SDK_ROOT}/cmdline-tools/latest/bin:${ANDROID_SDK_ROOT}/platform-tools:${PATH}"
# Install essential system packages
RUN apt-get update && apt-get install -y \
curl \
git \
unzip \
wget \
build-essential \
clang \
cmake \
libglu1-mesa \
openjdk-17-jdk \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/*
# Install Flutter SDK
RUN git clone https://github.com/flutter/flutter.git $FLUTTER_HOME --depth 1 --branch stable
# Install Android SDK Command-Line Tools
RUN mkdir -p ${ANDROID_SDK_ROOT} && \
cd ${ANDROID_SDK_ROOT} && \
wget https://dl.google.com/android/repository/commandlinetools-linux-9477386_latest.zip && \
unzip commandlinetools-linux-9477386_latest.zip && \
rm commandlinetools-linux-9477386_latest.zip && \
mkdir -p cmdline-tools/latest && \
mv cmdline-tools/* cmdline-tools/latest/ 2>/dev/null || true
# Accept Android SDK licenses and install essential SDK components
RUN yes | sdkmanager --licenses && \
sdkmanager "platform-tools" && \
sdkmanager "platforms;android-33" && \
sdkmanager "build-tools;33.0.2"
# Run flutter doctor to download Dart SDK, Flutter engine, etc.
RUN bash -c "flutter doctor"
# Create a non-root user for running commands within the container
RUN useradd -m -s /bin/bash developer && \
usermod -aG sudo developer
# Set the user for subsequent commands and the running container
USER developer
# Set the default working directory where your project will be mounted
WORKDIR /workspace
# Optional: Keep the container running indefinitely
CMD ["tail", "-f", "/dev/null"]
-
Create a devcontainer.json file in the .devcontainer directory:
{
"name": "Flutter Development Environment",
// Configuration for building the Docker image
"build": {
"dockerfile": "Dockerfile", // Points to our Dockerfile
"context": "." // Use the current folder (project root) as build context
},
// Mount your local project folder into the container
"workspaceFolder": "/workspace", // The directory inside the container
"workspaceMount": "source=${localWorkspaceFolder},target=/workspace,type=bind,consistency=cached",
// Settings for VS Code within the container
"settings": {
// Crucial: Tells the Dart/Flutter extension where to find the SDK inside the container
"dart.flutterSdkPath": "/opt/flutter",
// Disable VS Code's own SDK update checks, as our Dockerfile handles it
"dart.checkForSdkUpdates": false,
// Enable format on save and automatic code actions for cleaner code
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": true
}
},
// VS Code extensions to install automatically inside the container
"customizations": {
"vscode": {
"extensions": [
"Dart-Code.dart-code", // The official Dart & Flutter extension
"eamodio.gitlens", // Useful Git insights
"esbenp.prettier-vscode" // Code formatter
]
}
},
// Commands to run automatically after the container is created
// 'flutter doctor' checks the environment, 'flutter precache' downloads necessary engine artifacts
"postCreateCommand": "flutter doctor -v && flutter precache",
// Run container commands as the 'developer' user defined in the Dockerfile
"remoteUser": "developer",
// Ports to forward from the container to your host machine
// Flutter web apps often run on 8080. Add others as needed
"forwardPorts": [
8080, // Default for Flutter web
// Add other common Flutter ports if needed for debugging or device communication
5000, 5001, 5002, 5003, 5004, 5005
],
// Use volumes for persistent storage of Flutter's pub cache
// This speeds up subsequent container builds significantly
"mounts": [
"source=flutter-pub-cache,target=/home/developer/.pub-cache,type=volume"
],
// Environment variables available in the container
"containerEnv": {
"PUB_CACHE": "/home/developer/.pub-cache"
}
}
-
Open the Project in VS Code:
code .
Once VS Code is open, reopen the folder in a container by clicking the notification or pressing Ctrl+Shift+P and selecting "Reopen in Container."
-
Verify the Setup:
In the terminal within VS Code (which is now inside the container), run:
flutter doctor
This should show that Flutter is properly installed and configured.
-
Create a New Flutter Project:
In the terminal within VS Code, run:
flutter create .
-
Run the Flutter App:
For example, to run a web app:
flutter run -d web
By following these steps, you can set up Bluefin-dx for Flutter development using Dev Containers, which provides a clean and consistent development environment.