A developer-focused setup for React Native apps, providing a consistent Android build environment using Docker. Also offers utilities for IDE integration, SSH provisioning, and configuration options. Not designed for iOS or production use.
Go to file
Silas 6d54472af3
rename script to dockman, update readme, add docker flag to npm script for RN projects to set up their watch folder
2023-07-28 00:41:17 -04:00
.dockerignore init commit 2023-07-26 20:13:25 -04:00
.env rename script to dockman, update readme, add docker flag to npm script for RN projects to set up their watch folder 2023-07-28 00:41:17 -04:00
.gitignore init commit 2023-07-26 20:13:25 -04:00
Dockerfile init commit 2023-07-26 20:13:25 -04:00
README.md rename script to dockman, update readme, add docker flag to npm script for RN projects to set up their watch folder 2023-07-28 00:41:17 -04:00
docker-compose.yml fix a bunch of variable mismatches, add checks for env vars, remove -it from docker commands that don't need it 2023-07-27 20:30:13 -04:00
dockman.sh rename script to dockman, update readme, add docker flag to npm script for RN projects to set up their watch folder 2023-07-28 00:41:17 -04:00
entrypoint.sh rename script to dockman, update readme, add docker flag to npm script for RN projects to set up their watch folder 2023-07-28 00:41:17 -04:00
sshd_config init commit 2023-07-26 20:13:25 -04:00
supervisord.conf rename script to dockman, update readme, add docker flag to npm script for RN projects to set up their watch folder 2023-07-28 00:41:17 -04:00

README.md

React Native Dockerized

This repository aims to establish a reliable Android build environment for QA and sanity checks in React Native apps. It's designed for parallel iOS development, as 'node_modules', 'Pods', and iOS build artifacts aren't synced between the host and the container, allowing them to function independently of the Docker setup.

Things to keep in mind:

  • This does not support iOS builds. For that, you'd require a remote Mac host. While there exist Docker images for Mac, they are experimental and using them goes against Apple's EULA.

  • It's intended strictly for development purposes. The Dockerfile sets up a container that operates everything as root, using default sshd configurations.

Requirements

  • Android Studio installed with platform-tools in your path.
    • Note: This is likely configured if you've already developed for Android/RN on your machine
  • Docker Desktop
    • OrbStack (great alternative but it will be out of its free beta soon and may not be stable)
    • If you're running Linux, you can also just use the docker CLI and docker service, installed through your package manager.
  • A React Native project to dockerize!

Installation

You will either want to dockerize an app you haven't cloned/created yet, or an existing repository on your machine.

Option 1: Cloning A New Project

Expand

Depending on whether you're starting a new project or dockerizing an existing one, there are two options:

cd ~/Projects
git clone git@github.com:my-github-org/react-native-dockerized.git my-rn-project-dockerized
cd my-rn-project-dockerized && rm -rf .git
git clone git@github.com:my-github-org/my-rn-project.git app

Option 2: An Existing Project

Expand

To dockerize an existing React Native repository, clone this repository first. Then, move your React Native repository into this repository under a folder named app.

cd ~/Projects
git clone git@github.com:my-github-org/react-native-dockerized.git my-rn-project-dockerized
mv my-rn-project my-rn-project-dockerized/app
cd my-rn-project-dockerized && rm -rf .git

Add dockman.sh to your PATH

Expand

If you would like to run dockman.sh from any directory on your system, follow these steps:

  1. Create a new directory named .react-native-dockerized in your home directory (if it doesn't already exist):

    mkdir -p $HOME/.react-native-dockerized
    
  2. Move the dockman.sh script into this new directory:

    mv /path/to/dockman.sh $HOME/.react-native-dockerized
    
  3. Make the script executable:

    chmod +x $HOME/.react-native-dockerized/dockman.sh
    
  4. Add the .react-native-dockerized directory to your PATH. The exact command depends on your shell:

    • For bash:

      echo 'export PATH=$PATH:$HOME/.react-native-dockerized' >> ~/.bashrc
      source ~/.bashrc
      
    • For zsh:

      echo 'export PATH=$PATH:$HOME/.react-native-dockerized' >> ~/.zshrc
      source ~/.zshrc
      

    Now you can run dockman.sh directly from any directory.

Note: The source command makes the changes effective immediately. You may need to restart your shell or terminal session to apply these changes if you choose not to use source.

Check out the other optional setup sections, especially Configure Github Auth if your project has dependencies installing from a private Github repository.

Usage

After completing Installation and any optional setup:

docker-compose up
./dockman.sh start-metro
# boot up an Android emulator before this step
./dockman.sh run-android

You're all set! The following table outlines the helper commands for managing the container, designed with the help of GPT-4. Regular Docker commands work too. Refer to dockman.sh for examples.

Command Description
./dockman.sh stop-metro Stop the Metro server
./dockman.sh start-metro Start the Metro server and reset its cache
./dockman.sh metro-logs Check the server logs
./dockman.sh connect-android Connect to your host machine's Android device
./dockman.sh run-android Connects to your host machine's Android device, builds and deploys the app
./dockman.sh reinstall-node-modules Removes the node_modules directory in the project and re-installs dependencies
./dockman.sh clear-known-hosts Removes the Docker container's entries from the host's ~/.ssh/known_hosts file
./dockman.sh shell Starts a bash shell in the project's directory inside the Docker container
./dockman.sh init-ssh Generates an SSH key and adds it to the host's SSH config

You may need to run sudo chmod +x ./dockman.sh first for executable permissions.

Settings

You can adjust a few settings in the .env file at the root of the repository.

Variable Description Default
CONTAINER_NAME The name of the Docker container with which the script interacts. react-native-dockerized
SSH_KEY The location of the SSH key file used for secure communication with the Docker container. ~/.ssh/id_docker_dev
RN_PROJECT_FOLDER_NAME The name of the React Native project folder. This should match the name of the folder that you cloned at the root of your repository. app

If you plan on running multiple Docker containers simultaneously, you will need to assign a unique name to each container. For instance, if you're developing two separate React Native apps, you'll need to dockerize the second app using a different container name.

Please note that modifying the RN_PROJECT_FOLDER_NAME variable won't rename the project folder on your host machine. This variable should match the existing name of your project folder.

Configure Github Auth

Expand - optional, but highly recommended

Prior to starting up the container, the image will run npm install during the build process. This may require Github authentication if you're installing NPM packages from private Github repositories.

  1. Create a legacy access token with all Repo-related permissions checked, and save the token in your password manager.
  2. Add export GH_TOKEN="your_legacy_access_token" to your ~/.zshrc file
  • Alternatively, you could add it to the .env file

Remote Workspace Inside The Container:

Expand - optional

You can open a new remote workspace in your IDE to access the React Native project within this Docker container. With VS Code, you may not require SSH for this, thanks to the robust integration of the Dev Containers extension with Docker. However, if you're using VSCodium, your best bet is the Open Remote SSH extension.

Do note that VSCodium comes with certain limitations, such as the need to manually provision an SSH server and client - but hey that's the FOSS life.

To simplify the setup for a remote workspace in VSCodium, you can run init-ssh, which will provision everything necessary. You can confirm this by running ssh root@{your.dockers.ip.address}.

Troubleshooting

  • If hot reloading doesn't work, first check the Metro logs with dockdroid metro-logs. If the logs don't help, make sure your Metro configuration includes watchFolders: ['/home/app'],, replacing 'app' with the value of RN_PROJECT_FOLDER_NAME. This ensures changes are detected in both your local machine and the container.

  • If you encounter issues connecting via SSH, try running ssh root@{your.docker.ip.address} -v for more debug info. The logs are typically clear and verbose enough to guide you further.

  • Your known_hosts file may cause an error after recreating the container. In this case, run ./dockdroid.sh clear-known-hosts. Then SSH in again to confirm the issue is resolved, and accept the new known_host entry.