iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐳

Saving Someone: How to Set Up a Flutter Development Environment with Docker

に公開
2025/03/06
7件

Introduction

On April 15, 2021, Docker for Mac with official support for Apple Silicon was released, making it possible to build and share virtual environments using Docker on Apple Silicon macOS.

In this article, we aim to set up a Flutter development environment by launching a Docker Container using Docker for Mac.

Environment (as of 2022/07/18)

  • Macbook Air (M1, 2020)
  • macOS: Monterey (12.4)
  • Docker for Mac: 4.10.1 (82475)
  • Visual Studio Code: 1.69.1
    • Remote - Containers: v0.241.3

Installing Docker for Mac

First, install Docker for Mac on macOS.

Here, I will introduce two methods: installing from a dmg file and installing using Homebrew-cask.

Installing from a dmg file

  1. Download the dmg file directly from the official website
  2. Open the dmg file and copy the Docker for Mac app within the driver to the /Applications folder

Installing using Homebrew-cask

To install using Homebrew-cask,

  1. (If Homebrew is not installed) Install Homebrew
  2. Install Docker for Mac using the Homebrew-cask plugin

the following steps are required.

1. Installing Homebrew

Install Homebrew by referring to the official website.

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
$ export PATH=/opt/homebrew/bin:$PATH
2. Installing Docker for Mac

Next, install Docker for Mac using Homebrew-cask, which is a plugin for the Homebrew you just installed.

$ brew install --cask docker

Docker for Mac will now be installed in /Applications.

Creating Configuration Files for the Virtual Environment (Container)

Next, set up the environment for building the Container, which will serve as the virtual environment in Docker.

Creating the Dockerfile

First, create a Dockerfile to specify the steps for environment construction.

.devcontainer/flutter/Dockerfile
# ① Base Docker image
FROM dart:latest

# Maintainer
LABEL maintainer="XXX <xxx@example.com>"

# Workdir
WORKDIR /workspace/xxx

# ② Installing Flutter using FVM
ARG PATH=/root/.pub-cache/bin:$PATH
ARG FLUTTER_VERSION=x.y.z
RUN dart pub global activate fvm --verbose && \
    fvm doctor --verbose && \
    fvm install $FLUTTER_VERSION --verbose && \
    fvm use --force $FLUTTER_VERSION --verbose

# Set paths
ARG FVM_ROOT=/root/.pub-cache
ENV PATH $FVM_ROOT/bin:$PATH

1. Base Docker Image

We use the Official Dart Image from Docker as the base.
The example above specifies the latest version of Dart, but you can also specify a version like dart:3.0-sdk if needed.

2. Installing Flutter

In this guide, we will install Flutter using Flutter Version Manager (FVM).
Using FVM allows you to change the Flutter version easily.

If the Dart image is unavailable

If you cannot use the Dart image mentioned above, you can also install Flutter using the following method:

Method without using the Dart image
.devcontainer/flutter/Dockerfile
# ① Base Docker image
FROM ubuntu:20.04

# Maintainer
LABEL maintainer="XXX <xxx@example.com>"

# Workdir
WORKDIR /workspace/xxx

# ② Downloading Dart
ARG DART_VERSION=x.y.z
RUN mkdir -p /usr/share/man/man1 && \
    apt-get update && apt-get install -y --no-install-recommends tzdata && \
    apt-get update && \
    apt-get install -y --no-install-recommends bash ca-certificates clang cmake curl file git libglu1-mesa libgtk-3-dev ninja-build pkg-config unzip xz-utils zip && \
    curl https://storage.googleapis.com/dart-archive/channels/stable/release/$DART_VERSION/sdk/dartsdk-linux-x64-release.zip -o /tmp/dart-sdk.zip && \
    unzip /tmp/dart-sdk.zip -d /usr/lib && rm /tmp/dart-sdk.zip

# ③ Installing Flutter using FVM
ARG PATH=/usr/lib/dart-sdk/bin:$PATH
ARG PATH=/root/.pub-cache/bin:$PATH
ARG FLUTTER_VERSION=x.y.z
RUN dart pub global activate fvm --verbose && \
    fvm doctor --verbose && \
    fvm install $FLUTTER_VERSION --verbose && \
    fvm use --force $FLUTTER_VERSION --verbose

# Clean up
RUN apt-get clean -y && \
    apt-get autoremove -y && \
    rm -rf /var/lib/apt/lists/*

# Set paths
ARG FVM_ROOT=/root/.pub-cache
ENV PATH $FVM_ROOT/bin:$PATH
1. Base Docker Image

In this case, we selected ubuntu:20.04 as the base image. Alternatively, you can use other lightweight Linux-based images.

2. Installing Dart

Before installing Dart, use apt-get to install the command-line tools required to install Flutter.

Then, download the Dart SDK from https://storage.googleapis.com/dart-archive/channels/be/raw/latest/sdk/dartsdk-<platform>-<architecture>-release.zip and install it by extracting the file.

3. Installing Flutter using FVM

In this guide, we will install Flutter using Flutter Version Manager (FVM).
Using FVM allows you to change the Flutter version easily.

Creating docker-compose.yml

Next, create a docker-compose.yml to specify the files and volumes to be used when building the Container.

.devcontainer/docker-compose.yml
version: '3.8'
services:
  <project_name>:
    # ① Specify the Dockerfile to use when building the virtual environment
    build:
      context: .
      dockerfile: flutter/Dockerfile
    # ② Specify files to mount to the Container
    volumes:
      - ../${YOUR_PROJECT_DIRECTORY}:/workspace/${YOUR_PROJECT_DIRECTORY}:cached
      # (Optional) VSCode settings
      - ../.vscode:/workspace/.vscode:cached
    command: /bin/sh -c "while sleep 1000; do :; done"
    # ③ Specify the environment variable definition file
    env_file:
      - flutter/.flutter.env

1. Specify the Dockerfile to use when building the virtual environment (Container)

Specify the .devcontainer/flutter/Dockerfile created earlier to be referenced when building the Container, which is the virtual environment.

2. Specify files to mount to the Container

Specify the local files you want to mount to the Container.
When developing with VSCode, mount the .vscode file under the /workspace directory so that workspace settings are applied to the VSCode instance opened within the Remote Container.

3. Specify the environment variable definition file

Define environment variables in .devcontainer/flutter/.flutter.env and make them accessible within the Container.

Generating the Virtual Environment (Container)

Build the Container based on the Dockerfile and docker-compose.yml created earlier, and then connect to it.

Here, I will introduce two methods: using the docker command and using the VSCode Remote - Containers extension.

When using the docker command

If you use the docker command, use the following commands to build and connect to the virtual environment:

$ docker compose build
$ docker compose up -d
$ docker exec -it <container_id> /bin/bash

When using VSCode's Remote - Containers

  1. Install Remote - Containers in VSCode.
  2. Click Remote - Containers > Reopen in Container > From docker-compose.yml.

With this, you have created a Flutter development environment as a Container.

Creating a Flutter Project inside the Container using Flutter Commands

After connecting to the Container, create a Flutter Project using the following command:

$ fvm flutter create <project_name>

Using the above command will create a Flutter Project under /workspace/<project_name>.

This completes the setup of the Flutter development environment within the Docker Container. Great job!

Summary

In this article, I introduced how to launch a Docker Container on an Apple Silicon macOS and build a development environment for a Flutter Project.

Let's build and share development environments using Docker Containers to create highly reusable development setups!

If you have any questions or feedback about this article, please feel free to leave a comment!

Discussion

ki504178ki504178

認識違い・OSなど環境差異のせいなら申し訳ありませんが、
こちらスクラップを見た感じだと、
M1だとNGで、Intel CPUならOKの場合の構築手順に見えるのですがいかがでしょうか?
因みに手元のM1 Macbook Airでこの記事の手順を試してみましたが、
スクラップと同じで、flutterインストールスクリプト(dart pub global ...)で「/lib64/ld-linux-x86-64.so.2: No such file or directory」エラーが発生しました。

[環境]
M1 Macbook Air
OS: macOS Monterey 12.1
Docker Desktop: 4.3.2
Docker Engine: 20.10.11
Docker Compose: v2.2.1

nagakutanagakuta

@ki504178

こちらスクラップを見た感じだと、M1だとNGで、Intel CPUならOKの場合の構築手順に見えるのですがいかがでしょうか?

スクラップを書いた段階では Apple Silicon (arm64) だと Dart の公式 Docker Image が対応しておらず、こちらの記事では「Dart SDK を zip でダウンロードして Container 内で解凍して利用する」という方法を採用しています。

因みに手元のM1 Macbook Airでこの記事の手順を試してみましたが、スクラップと同じで、flutterインストールスクリプト(dart pub global ...)で「/lib64/ld-linux-x86-64.so.2: No such file or directory」エラーが発生しました。

私も手元で @ki504178 さんとなるべく近い環境で記事の手順を再現してみましたが、上記のエラーは再現しませんでした。

この記事にて説明が不足している箇所がある可能性もあるので、こちらの記事の内容をアップデートして更新します。アップデートされた手順でも失敗する場合はまたご一報ください。

ki504178ki504178

@nagakutaさん
ご回答ありがとうございました。

私も手元で @ki504178 さんとなるべく近い環境で記事の手順を再現してみましたが、上記のエラーは再現しませんでした。

なるほど。。。どこまで同じ環境なのかはちょっと分かりませんが、
あるとしたらOSのメジャーバージョン違いとかがありそうかと感じました。

この記事にて説明が不足している箇所がある可能性もあるので、こちらの記事の内容をアップデートして更新します。アップデートされた手順でも失敗する場合はまたご一報ください。

ありがとうございます!!
アップデート後の手順で再度試してみてご報告いたしますm(_ _)m

nagakutanagakuta

@ki504178

こちら、最新 ver で動作確認したものに記事を更新しました!

もしかしたらすでにご自身で解決なさっているかもしれませんが、ご参考になれば嬉しいです!

1
ki504178ki504178

@nagakuta
ありがとうございます!!
最新の記事の通りに構築したらイケましたmm

環境

  • Macbook Air (M1, 2020)
  • macOS: Monterey (12.4)
  • Docker for Mac: 4.10.1 (82475)
  • Visual Studio Code: 1.68.1
    • Remote - Containers: v0.241.3
ryomaryoma

@nagakuta

記事拝見させていただきました。
Remote - Containers の Reopen in Container > From docker-compose.yml をクリックで開くディレクトリは.devcontainerでよろしいのでしょうか?

nagakutanagakuta

@ryoma

Remote - Containers の Reopen in Container > From docker-compose.yml をクリックで開くディレクトリは.devcontainerでよろしいのでしょうか?

はい、.devcontainer/docker-compose.yml を対象にして Open してください!