DEV Community

Cover image for How to Connect an Android Phone to a Docker Container ? - ADB, Docker, VSCode
Rémi Lavedrine
Rémi Lavedrine

Posted on • Updated on

How to Connect an Android Phone to a Docker Container ? - ADB, Docker, VSCode

So, You decided to develop in Docker containers. 🐳

That’s a good choice because it solves a lot of problems. 👌🏼
On the other hand, it adds new ones. 😅

Great, We, developers, love to solve problems. 👍🏼🧑🏼‍💻


In the video below, I show you how to connect your Android phone, via ADB, to your Linux machine.

No worries, it’s easy as pie.

Follow the steps described in the video below, and you should be able to interact with your android phone from your Docker Container on a Linux Host.

Video with French Audio & English Subtitles


If you are more a reading person, just follow the steps below.

First you have to have VSCode installed as we are going to develop in a Docker Container using VSCode and the Remote Container extension.

Everything you’re doing here is done on a Linux host machine. The steps are different on MacOS or Windows.

I did all of it on an Ubuntu machine, so tell me if you tested it on another distro and it worked (or not).

You have to describe the Dockerfile you are going to use to develop in a Docker Container.

Here is the one I used in the video :

# Pull Ubuntu LTS image.
FROM shostarson/base-dev-env:v1

# Labels and Credits
LABEL \
name=”Adb on Linux Host” \
author=”Rémi Lavedrine <remi@github.com>” \
maintainer=”Rémi Lavedrine <remi@github.com>” \
description=”Adb in Docker Container on a Linux Host machine.”

ENV SRC_DIR=/root/home/adb-linux

WORKDIR $SRC_DIR

COPY . .

RUN apt update -y && apt install -y — no-install-recommends android-tools-adb
Enter fullscreen mode Exit fullscreen mode

And the .devcontainer.json file that describes the configuration for this container on VSCode.

You just have to add these args to the file :

“runArgs”: [   privileged”,
“-v”,
“/dev/bus/usb:/dev/bus/usb” ],
Enter fullscreen mode Exit fullscreen mode

Rebuild everything in the Container and you are good to go.

Try adb devices on your Container with your Android phone connected to your host machine. You should see your device as an authorized one.

Tip :

Don’t forget that an Android phone can be connected to only one ADB server at a given time. So if it’s not working get sure that an ADB server is not running on your host machine.

If so, kill it and retry in your Docker Container.


I hope you enjoyed it and that it helps you to use Docker and Containers at their maximum possibilities.

If you like this content, push the like button, that helps spread the message. 👍🏼
If you think it can be useful to anyone in your network, share it. 📨


Video produced by Wild & Secure, your consulting firm to all things security and real estate.
If you want to receive weekly quality content about security, subscribe to our newsletter on our website.

Top comments (1)

Collapse
 
momt99 profile image
Mohammad Omidvar

Thanks for this simple instruction.
As of writing this comment, some new fields are added to the devcontainer.json schema. So you can set it up like this:

{
    "privileged": true,
    "mounts": [
        {
            "type": "bind",
            "source": "/dev/bus/usb",
            "target": "/dev/bus/usb",
        }
    ],
}
Enter fullscreen mode Exit fullscreen mode