Hi, I'm Tiago

Running Android Studio from a docker.io container

Published on

One of the benefits or docker containers is that it allows you to test a new application in an environment different from that you already run without risking it, for example, by installing a new JDK you suspect doesn’t play well with the tools you have.

This article shows how you can run Android Studio inside a docker.io container, reusing the X session in your host machine.

Pre-configuration

I assume you have docker.io installed and running. If not, check out here how to do it.

Building the image from Dockerfile

Clone my dockerfiles repository in github to somewhere in your computer and cd into it.

$ git clone https://github.com/reul/dockerfiles.git dockerfiles
$ cd dockerfiles

Build the docker image with Android Studio:

$ sudo docker build --tag=android-studio android-studio

Running the container

First disable access control for the current X session:

$ xhost +

Then run the container for the first time:

$ sudo docker run -t -i \
    -e DISPLAY \
    -v /tmp/.X11-unix:/tmp/.X11-unix:ro \
    -v $HOME/.Xauthority:/home/developer/.Xauthority:ro \
    --net=host \
    --name android-studio \
    android-studio

In subsequent runs, you just need to start the container with:

$ docker start android-studio

Version control

Android Studio has a embedded terminal emulator, and the Dockerfile linked in this article installs git, therefore you can use it to clone your project and work entirely from inside the container.

Copying files

If you want to copy a file or a directory from inside the container to the host machine, you can do that by using the docker cp command. The syntax is as follows (from docker.io help):

$ docker help cp

Usage: docker cp [OPTIONS] CONTAINER:PATH HOSTDIR|-

Copy files/folders from a PATH on the container to a HOSTDIR on the host
running the command. Use '-' to write the data
as a tar file to STDOUT.

Volumes

If you still need access from the outside machine, you can mount the AndroidStudioProjects directory using a docker.io volume or even share a volume with another container.