DEV Community

Cover image for Dockerize your Flutter App
Amey Sunu
Amey Sunu

Posted on • Updated on

Dockerize your Flutter App

Docker is a platform that delivers software in containers and usually uses OS-level virtualization.

The Need

Of course, the first question to pop up is the need. Why do we need Docker for Flutter Apps? Well, here it is, as Flutter Developers, we know the hardship of setting up Flutter SDK to Android SDK, Java, and whatnot. With Docker, we can say goodbye to all these installation processes and just grab the Dockerfile to build, test, execute and even deploy. You even don't need to worry about scaling, as Docker does it all for you.

Getting Started

Let's begin by creating a Dockerfile. Here, we will be using Docker to run integration and smoke testing on your Flutter app.

FROM ubuntu:20.10 as builder
RUN apt update && apt install -y curl git unzip xz-utils zip libglu1-mesa openjdk-8-jdk wget
RUN useradd -ms /bin/bash user
USER user
WORKDIR /home/user

#Installing Android SDK
RUN mkdir -p Android/sdk
ENV ANDROID_SDK_ROOT /home/user/Android/sdk
RUN mkdir -p .android && touch .android/repositories.cfg
RUN wget -O sdk-tools.zip https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip
RUN unzip sdk-tools.zip && rm sdk-tools.zip
RUN mv tools Android/sdk/tools
RUN cd Android/sdk/tools/bin && yes | ./sdkmanager --licenses
RUN cd Android/sdk/tools/bin && ./sdkmanager "build-tools;29.0.2" "patcher;v4" "platform-tools" "platforms;android-29" "sources;android-29"
ENV PATH "$PATH:/home/user/Android/sdk/platform-tools"

#Installing Flutter SDK
RUN git clone https://github.com/flutter/flutter.git
ENV PATH "$PATH:/home/user/flutter/bin"
RUN flutter channel dev
RUN flutter upgrade
RUN flutter doctor
Enter fullscreen mode Exit fullscreen mode

Run this Dockerfile in your system using the command docker build -t flutterdockerfile .
This will begin your Dockerfile and start installing all the required tools for the perfect Flutter Development.

Now go ahead and push your Flutter app to your git repository and make sure you set up testing in your widget_test.dart file.

Once you are done pushing the Flutter app, add few more lines to the Dockerfile.

RUN git clone GIT_REPO_URL
RUN cd FILE_NAME && flutter test
Enter fullscreen mode Exit fullscreen mode

Replace GIT_REPO_URL with your Flutter git repo URL and FILE_NAME will be the name of your repo.
This will now begin test on your Flutter application.

Alternative Method

  • Pull the Docker image by running docker pull ameysunu/flutterdocker in your terminal.

  • Now run docker images and get the IMAGE ID for the container.

  • With the obtained IMAGE ID, run docker run -i -t IMAGE ID, and you will be in the container.

  • Clone the Flutter app from your git repository and run flutter test for smoke testing

I'm also linking this amazing post on DEV for Flutter Dockerization with Android Emulator as well.

Discussion (6)

Collapse
silverhairs profile image
Boris Kayi (銀髪)

Is it possible to run the app in the container while the emulator is installed in the local computer?

Collapse
ameysunu profile image
Amey Sunu Author

No, not exactly. You can connect a physical android device via WiFi and then debug on it via the container.

Collapse
silverhairs profile image
Boris Kayi (銀髪)

Ok, thanks

Collapse
lucasalustiano profile image
Lucas Salustiano

Could you explain how it would be?

Thread Thread
ameysunu profile image
Amey Sunu Author

This link could be of use to you:

stackoverflow.com/questions/489395...

Thread Thread
lucasalustiano profile image
Lucas Salustiano

Thank you.