sorccu / docker-adb

Dockerfile and instructions for ADB.
Other
200 stars 74 forks source link

docker-adb

This repository contains a Dockerfile for the Android Debug Bridge. It gives you access to platform tools such as adb and fastboot.

Changes

Gotchas

Security

The container is preloaded with an RSA key for authentication, so that you won't have to accept a new key on the device every time you run the container (normally the key is generated on-demand by the adb binary). While convenient, it means that your device will be accessible over ADB to others who possess the key. You can supply your own keys by using -v /your/key_folder:/root/.android with docker run.

Updating the platform tools manually

If you feel like the platform tools are out of date and can't wait for a new image, you can update the platform tools with the following command:

update-platform-tools.sh

It's in /usr/local/bin and therefore already in $PATH.

Usage

There are various ways to use this image. Some of the possible usage patterns are listed below. It may sometimes be possible to mix them depending on the case. Also, you don't have to limit yourself to the patterns mentioned here. If you can find another way that works for you, go ahead.

Pattern 1 - Shared network on the same machine (easy)

This usage pattern shares the ADB server container's network with ADB client containers.

Start the server:

docker run -d --privileged -v /dev/bus/usb:/dev/bus/usb --name adbd sorccu/adb

Then on the same machine:

docker run --rm -ti --net container:adbd sorccu/adb adb devices
docker run --rm -i --net container:adbd ubuntu nc localhost 5037 <<<000chost:devices

Pros:

Cons:

Pattern 2 - Host network (easy but feels wrong)

This usage pattern binds the ADB server directly to the host.

Start the server:

docker run -d --privileged --net host -v /dev/bus/usb:/dev/bus/usb --name adbd sorccu/adb

Then on the same machine:

docker run --rm -ti --net host sorccu/adb adb devices
docker run --rm -i --net host ubuntu nc localhost 5037 <<<000chost:devices

Or on another machine:

docker run --rm -ti sorccu/adb adb -H x.x.x.x -P 5037 devices

Pros:

Cons:

Pattern 3 - Linked containers on the same machine (can be annoying)

This usage pattern shares the ADB server container's network with ADB client containers.

Start the server:

docker run -d --privileged -v /dev/bus/usb:/dev/bus/usb --name adbd sorccu/adb

Then on the same machine:

docker run --rm -ti --link adbd:adbd sorccu/adb \
  sh -c 'adb -H $ADBD_PORT_5037_TCP_ADDR -P 5037 devices'

Pros:

Cons:

Pattern 4 - Remote client

This usage pattern works best when you want to access the ADB server from a remote host.

Start the server:

docker run -d --privileged -v /dev/bus/usb:/dev/bus/usb --name adbd -p 5037:5037 sorccu/adb

Then on the client host:

docker run --rm -ti sorccu/adb adb -H x.x.x.x -P 5037 devices

Where x.x.x.x is the server host machine.

Pros:

Cons:

Systemd units

Sample systemd units are provided in the systemd/ folder.

Unit Role Purpose
adb-image.service Support Pulls the image from Docker Hub.
adbd-container.service Support Creates a container for the ADB daemon based on the adb image, but doesn't run it.
adbd.service Primary Runs the prepared ADB daemon container and makes sure it stays alive.

This 3-unit configuration, while slightly complex, offers superior benefits such as incredibly fast start time on failure since everything has already been prepared for adbd.service so that it doesn't have to do any extra work. The adb image will only get pulled once at boot time instead of at every launch (or manually by calling systemctl restart adb-image, which will also restart the other units).

Copy the units to /etc/systemd/system/ on your target machine.

Then, enable adbd.service so that it starts automatically after booting the machine:

systemctl enable adbd

Finally, either reboot or start the service manually:

systemctl start adbd

If you change the units, don't forget to run systemctl daemon-reload or they won't get updated.

Thanks

License

See LICENSE.