Skip to content
This repository has been archived by the owner on Jul 23, 2022. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Svorc committed Dec 5, 2020
0 parents commit d6fce0f
Show file tree
Hide file tree
Showing 7 changed files with 304 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Ignore everything
**
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/workspace/*
!/workspace/.keep
94 changes: 94 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
FROM ubuntu:20.04

# Build arguments
ARG android_sdk_version
ARG android_build_tools_version
ARG nodejs_version

ARG user_name='user'
ARG user_id='1000'
ARG group_name='mount'
ARG group_id='1000'

# Update package repositories and install packages
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive \
apt-get install -y \
adb \
curl \
git \
libglu1-mesa \
openjdk-8-jdk \
unzip \
usbutils \
xz-utils \
wget \
zip \
gnupg \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Install Node.js and Yarn
RUN curl "https://deb.nodesource.com/setup_${nodejs_version}.x" | bash
RUN curl -sS 'https://dl.yarnpkg.com/debian/pubkey.gpg' | apt-key add -
RUN echo 'deb https://dl.yarnpkg.com/debian/ stable main' | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive \
apt-get install -y \
nodejs \
yarn

# Create non-system user
RUN addgroup --gid "${group_id}" "${group_name}" \
&& useradd \
--create-home \
--shell /bin/bash \
--uid "${user_id}" \
--gid "${group_id}" \
"${user_name}" \
&& usermod -aG plugdev "${user_name}"

# Set non-system user
USER "${user_name}"
ENV HOME="/home/${user_name}"

# Change workdir
WORKDIR "${HOME}"

# Prepare Android SDK directory structure
RUN mkdir -p Android/sdk \
&& mkdir -p .android \
&& touch .android/repositories.cfg

# Android SDK env variables
ENV ANDROID_SDK_ROOT "${HOME}/Android/sdk"

# Download Android SDK tools
RUN wget -O sdk-tools.zip https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip \
--no-verbose \
--show-progress \
--progress=bar:force \
&& unzip sdk-tools.zip \
&& rm sdk-tools.zip \
&& mv tools "${ANDROID_SDK_ROOT}/tools"

# Install Android SDK tools
RUN cd "${ANDROID_SDK_ROOT}/tools/bin" \
&& yes | ./sdkmanager --licenses \
&& ./sdkmanager \
"build-tools;${android_build_tools_version}" \
'patcher;v4' \
'platform-tools' \
"platforms;android-${android_sdk_version}" \
"sources;android-${android_sdk_version}"

# Add Android SDK tools to PATH
ENV PATH "${PATH}:${ANDROID_SDK_ROOT}/platform-tools"

# Add yarn bin to PATH
ENV PATH "${PATH}:${HOME}/.yarn/bin"

# Install npm libraries with Yarn
RUN yarn global add \
react-native-cli \
npx
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Michal Svorc <michalsvorc.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# [React Native](https://reactnative.dev) development environment Docker image

Dockerized React Native development environment for VS Code remote development and USB connected physical Android device. Works without Android Studio.

Features:
- based on Ubuntu LTS
- Android SDK version: see repository tags
- Node.js version: v14 LTS

## Start Docker container

Use `docker.sh` script in project directory.

1. Build Docker image: execute `./docker.sh build`.
2. Run Docker image: execute `./docker.sh run`.

Docker container is not removed after exiting the application. To start the container again, execute `./docker.sh start`.

## Setup VS Code

1. Install VS Code extension [Remote - Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers).
2. Optional: Install VS Code extension [React Native Tools](https://marketplace.visualstudio.com/items?itemName=msjsdiag.vscode-react-native).

## Connect Android device

1. Enable `Developer mode` and `USB debugging` option on your device.
2. Plug your device into your computer.
3. Select `PTP` or `MTP` connection on your device. `PTP` worked for me in most cases.
4. In VS Code, run `Attach to Running Container` command.
5. Run `adb devices` command in attached VS Code terminal and verify the output.
6. If prompted on your device, authorize your computer to access the device.

## Mount directories overview

- **workspace**: share files between the host and containerized app

You can initialize new React Native applications inside the `workspace` directory to make them persistent on host machine.

## Initialize new React Native application

1. Connect your Android device as described above.
2. Open attached VS Code terminal.
3. `cd $HOME/workspace` for persistent code base.
4. `react-native init appName`
5. `cd $HOME/workspace/appName && react-native start`
6. Open new remote terminal tab in attached VS Code.
7. `cd $HOME/workspace/appName && react-native run-android`

## Troubleshooting

### Write access to mounted directories

Mount directories must be writable by group with id `1000`. Execute these commands in project root directory:

```sh
chown -R $(id -u):1000 "${PWD}"/workspace
chmod -R g+w "${PWD}"/workspace
```

### adb: no permissions (user in plugdev group; are your udev rules wrong?)

Try running `adb kill-server` command in container and reconnect the device

### React Native white blank screen issue

Run react-native start in a separate terminal and then run react-native run-android. [Source](https://stackoverflow.com/questions/51705627/react-native-white-blank-screen-issue)
119 changes: 119 additions & 0 deletions docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/usr/bin/env bash
# Author: Michal Svorc <michalsvorc.com>
# Description: Docker commands helper script

#===============================================================================
# Script options
#===============================================================================

set -o errexit # abort on nonzero exitstatus
set -o nounset # abort on unbound variable
set -o pipefail # don't hide errors within pipes
# set -o xtrace # debugging

#===============================================================================
# Variables
#===============================================================================

# Docker arguments
application_name='react-native'
image_name="michalsvorc/${application_name}"

android_sdk_version='29'
android_build_tools_version='29.0.2'
nodejs_version='14'

image_tag="android-sdk-${android_sdk_version}"
container_name="michalsvorc_${application_name}-${image_tag}"

mount_workspace_source="${PWD}/workspace"
mount_workspace_target="/home/user/workspace"

#===============================================================================
# Help
#===============================================================================

_print_help() {
cat <<HELP
Usage: ./docker.sh [COMMAND]
Docker commands helper script
Commands:
-h|--help print this help text and exit
build build Docker image
run run Docker image
start start Docker container
HELP
}

#===============================================================================
# Functions
#===============================================================================

_die() {
local error_message="${1}"
printf 'Error: %s\nSee help for proper usage:\n\n' "${error_message}" >&2
_print_help
exit 1
}

_docker_build() {
printf 'Docker build image %s\n' "${image_name}:${image_tag}"

docker build \
--build-arg android_sdk_version="${android_sdk_version}" \
--build-arg android_build_tools_version="${android_build_tools_version}" \
--build-arg nodejs_version="${nodejs_version}" \
--tag "${image_name}:${image_tag}" \
.
}

_docker_run() {
printf 'Docker run image %s\n' "${image_name}:${image_tag}"

docker run \
-d \
-it \
--privileged \
--env DISPLAY="${DISPLAY}" \
-v '/tmp/.X11-unix:/tmp/.X11-unix' \
-v '/dev/bus/usb:/dev/bus/usb' \
--mount type=bind,source="${mount_workspace_source}",target="${mount_workspace_target}" \
--name "${container_name}" \
"${image_name}:${image_tag}" \
bash
}

_docker_start() {
printf 'Docker start container %s\n' "${container_name}"

docker start "${container_name}"
}

#===============================================================================
# Execution
#===============================================================================

if [ $# -eq 0 ]; then
_die 'No arguments provided'
fi

case "${1}" in
-h|--help)
_print_help
;;
build)
_docker_build
;;
run)
_docker_run
;;
start)
_docker_start
;;
*)
_die "Unknown argument '${1}'"
;;
esac
Empty file added workspace/.keep
Empty file.

0 comments on commit d6fce0f

Please sign in to comment.