iTranslated by AI
Saving Someone: How to Set Up a Flutter Development Environment with Docker
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
- Remote - Containers:
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
- Download the dmg file directly from the official website
- Open the dmg file and copy the
Docker for Macapp within the driver to the/Applicationsfolder
Installing using Homebrew-cask
To install using Homebrew-cask,
- (If
Homebrewis not installed) InstallHomebrew - Install
Docker for Macusing theHomebrew-caskplugin
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.
# ① 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
# ① 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.
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
- Install
Remote - Containersin VSCode. - 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
認識違い・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
@ki504178
スクラップを書いた段階では Apple Silicon (arm64) だと Dart の公式 Docker Image が対応しておらず、こちらの記事では「Dart SDK を zip でダウンロードして Container 内で解凍して利用する」という方法を採用しています。
私も手元で @ki504178 さんとなるべく近い環境で記事の手順を再現してみましたが、上記のエラーは再現しませんでした。
この記事にて説明が不足している箇所がある可能性もあるので、こちらの記事の内容をアップデートして更新します。アップデートされた手順でも失敗する場合はまたご一報ください。
@nagakutaさん
ご回答ありがとうございました。
なるほど。。。どこまで同じ環境なのかはちょっと分かりませんが、
あるとしたらOSのメジャーバージョン違いとかがありそうかと感じました。
ありがとうございます!!
アップデート後の手順で再度試してみてご報告いたしますm(_ _)m
@ki504178
こちら、最新 ver で動作確認したものに記事を更新しました!
もしかしたらすでにご自身で解決なさっているかもしれませんが、ご参考になれば嬉しいです!
@nagakuta
ありがとうございます!!
最新の記事の通りに構築したらイケましたmm
環境
@nagakuta
記事拝見させていただきました。
Remote - Containers の Reopen in Container > From docker-compose.yml をクリックで開くディレクトリは.devcontainerでよろしいのでしょうか?
@ryoma
はい、
.devcontainer/docker-compose.ymlを対象にして Open してください!