update readme, add dockman script, require env vars when building, move npm install to image build process

This commit is contained in:
Silas 2023-07-28 18:17:12 -04:00
parent cf9b13efdf
commit fc602f01d7
Signed by: silentsilas
GPG Key ID: 4199EFB7DAA34349
9 changed files with 317 additions and 163 deletions

0
.dockerizedroot Normal file
View File

9
.env
View File

@ -1,3 +1,6 @@
CONTAINER_NAME="frayt-driver-v3-dockerized" CONTAINER_NAME=react-native-dockerized
RN_PROJECT_FOLDER_NAME="frayt-driver-v3" RN_PROJECT_FOLDER_NAME=app
SSH_KEY="~/.ssh/id_docker_dev" SSH_KEY="id_docker_dev"
NPM_TOKEN=
GH_TOKEN=
NODE_VERSION=16.13.0

View File

@ -3,20 +3,29 @@ FROM reactnativecommunity/react-native-android
# config and update # config and update
ENV DEBIAN_FRONTEND noninteractive ENV DEBIAN_FRONTEND noninteractive
ENV DOCKER=true
WORKDIR /home WORKDIR /home
RUN apt-get update RUN apt-get update
RUN mkdir /var/log/app RUN mkdir /var/log/app
ARG SSH_KEY ARG SSH_KEY
ARG NODE_VERSION
ARG GH_TOKEN
ARG NPM_TOKEN
ARG RN_PROJECT_FOLDER_NAME
# set up NVM and use v16 LTS # set up NVM and use v16 LTS
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
ENV NVM_DIR /root/.nvm ENV NVM_DIR /root/.nvm
ENV NODE_VERSION 16.20.1
RUN . "$NVM_DIR/nvm.sh" && nvm install $NODE_VERSION && nvm alias default $NODE_VERSION && nvm use default RUN . "$NVM_DIR/nvm.sh" && nvm install $NODE_VERSION && nvm alias default $NODE_VERSION && nvm use default
ENV NODE_PATH $NVM_DIR/versions/node/v$NODE_VERSION/lib/node_modules ENV NODE_PATH $NVM_DIR/versions/node/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
RUN npm install -g react-native-cli RUN npm install -g react-native-cli
# add gh/npm tokens and install project dependencies
RUN echo \"//github.com/:_authToken=$GH_TOKEN\" > /home/app/.npmrc
RUN echo \"//registry.npmjs.org/:_authToken=$NPM_TOKEN\" >> /home/app/.npmrc
RUN cd /home/$RN_PROJECT_FOLDER_NAME && npm install
# Install SSH server # Install SSH server
RUN apt-get -y install openssh-server RUN apt-get -y install openssh-server
COPY sshd_config /etc/ssh/sshd_config COPY sshd_config /etc/ssh/sshd_config
@ -31,4 +40,4 @@ COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY entrypoint.sh /entrypoint.sh COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"] CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

183
README.md
View File

@ -1,10 +1,10 @@
# Dockerized React Native # 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. 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: 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.._ * **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. * **It's intended strictly for development purposes**. The Dockerfile sets up a container that operates everything as root, using default sshd configurations.
@ -12,20 +12,24 @@ Things to keep in mind:
* [Android Studio](https://developer.android.com/studio) installed with `platform-tools` in your path. * [Android Studio](https://developer.android.com/studio) installed with `platform-tools` in your path.
* _Note:_ This is likely configured if you've already developed for Android/RN on your machine * _Note:_ This is likely configured if you've already developed for Android/RN on your machine
* [Docker Desktop ](https://docs.docker.com/desktop/install/mac-install/) * [Docker Desktop ](https://docs.docker.com/desktop/install/mac-install/)
* [OrbStack](https://orbstack.dev/) _(great alternative but it will be out of its free beta soon and may not be stable)_ * [OrbStack](https://orbstack.dev/) _(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 Github and NPM account
* A React Native project to dockerize! * A React Native project to dockerize!
## Installation ## Installation
You will either want to dockerize an app you haven't cloned/created yet, or an existing repository on your machine. 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 Project #### Option 1: Cloning A New Project
<details> <details>
<summary>Expand</summary> <summary>Expand</summary>
Let's say you have a React Native repository you've yet to clone but wish to run under docker. You'll clone this repository first, then clone your React Native project inside this repository with its folder named `app`. &NewLine;
Depending on whether you're starting a new project or dockerizing an existing one, there are two options:
```zsh ```zsh
cd ~/Projects cd ~/Projects
@ -41,7 +45,9 @@ git clone git@github.com:my-github-org/my-rn-project.git app
<details> <details>
<summary>Expand</summary> <summary>Expand</summary>
Let's say you've already cloned a React Native repository and your directory structure is: `~/Projects/my-rn-project`. You'll want to clone this repository, then move the entirety of your react native repository into this repository under a folder named `app`. &NewLine;
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.
```zsh ```zsh
cd ~/Projects cd ~/Projects
@ -52,82 +58,137 @@ cd my-rn-project-dockerized && rm -rf .git
</details> </details>
&NewLine; #### Configure Github and NPM Auth
Jump through the other optional setup, particularly [Configure Github Auth](#configure-github-auth) if your project has dependencies that install from a private Github repository.
## Usage
After going through [Installation](#installation) and any of the optional setup:
```zsh
docker-compose up
./dockdroid.sh start-metro
# boot up an Android emulator before this step
./dockdroid.sh run-android
```
And you're all set! The following are all of the helper commands for managing the container. Regular docker commands work as well. Refer to `dockdroid.sh` for examples.
| Command | Description |
| ------------- | ------------- |
| `./dockdroid.sh stop-metro` | Stop the Metro server |
| `./dockdroid.sh start-metro` | Start the Metro server and reset its cache |
| `./dockdroid.sh metro-logs` | Check the server logs |
| `./dockdroid.sh connect-android` | Connect to your host machine's Android device |
| `./dockdroid.sh run-android` | Connects to your host machine's Android device, builds and deploys the app |
| `./dockdroid.sh reinstall-node-modules` | Removes the node_modules directory in the project and re-installs dependencies |
| `./dockdroid.sh clear-known-hosts` | Removes the Docker container's entries from the host's ~/.ssh/known_hosts file |
| `./dockdroid.sh shell` | Starts a bash shell in the project's directory inside the Docker container |
| `./dockdroid.sh init-ssh` | Generates an SSH key and adds it to the host's SSH config |
_You may need to run `sudo chmod +x ./dockdroid.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 that the script interacts with. | `react-native-dockerized` |
| `SSH_KEY` | Location of the SSH key file that is 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` |
You will need to rename your container if you'd like to run more than one of these containers at the same time. For example, if you're working on two separate React Native apps, you will not be able to dockerize the second app without it using a different container name.
Please be aware that modifying the RN_PROJECT_FOLDER_NAME variable doesn't rename the project folder on your host machine. Instead, this variable should reflect the name you have already assigned to your project folder.
### Configure Github Auth
<details> <details>
<summary>Expand - optional, but highly recommended</summary> <summary>Expand</summary>
Before booting up the container, the image will run `npm install` as its getting built and will need to authenticate with a Github account to install any NPM packages from private Github repositories. &NewLine;
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.
#### Github
1. Create a [legacy access token](https://github.com/settings/tokens) with all Repo-related permissions checked, and save the token in your password manager. 1. Create a [legacy access token](https://github.com/settings/tokens) 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 2. Add `export GH_TOKEN="your_legacy_access_token"` to your `~/.zshrc` file
* Alternatively, you could add it to the `.env` file * Alternatively, you could add it to the `.env` file
#### NPM
1. If you're using private npm packages, create an [access token](https://docs.npmjs.com/creating-and-viewing-access-tokens) on npm's website. Ensure it has the appropriate permissions for the packages you need.
2. Add `export NPM_TOKEN="your_npm_access_token"` to your `~/.zshrc` file
* Alternatively, you could add it to the `.env` file
</details> </details>
#### Add `dockman.sh` to your PATH
<details>
<summary>Expand - optional</summary>
&NewLine;
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):
```zsh
mkdir -p $HOME/.react-native-dockerized
```
2. Copy the `dockman.sh` script into this new directory:
```zsh
cp ./dockman.sh $HOME/.react-native-dockerized
```
3. Make the script executable:
```zsh
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:
```bash
echo 'export PATH=$PATH:$HOME/.react-native-dockerized' >> ~/.bashrc
source ~/.bashrc
```
- For zsh:
```zsh
echo 'export PATH=$PATH:$HOME/.react-native-dockerized' >> ~/.zshrc
source ~/.zshrc
```
Now you can run `dockman.sh` directly from inside any directory of a dockerized React Native project.
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`.
</details>
## Usage
After completing [Installation](#installation) and any optional setup:
```zsh
./dockman.sh 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](https://chat.openai.com/share/648b4a38-9876-48d5-9e3b-a9c10bb25921). 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. Alternatively you could add them to your shell's environment.
| 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 this repository. | `app` |
| `NODE_VERSION` | The version of Node.js that will be installed by the Node Version Manager (NVM). | `16.13.0` |
| `NPM_TOKEN` | Token for authenticating with npm for installing npm packages. This should be a valid token associated with your npm account. | - |
| `GH_TOKEN` | Token for authenticating with GitHub. This is required for installing packages from private GitHub repositories. | - |
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.
### Remote Workspace Inside The Container: ### Remote Workspace Inside The Container:
<details> <details>
<summary>Expand - optional</summary> <summary>Expand - optional</summary>
You can open a new remote workspace in your IDE to access the React Native project within your container. With VS Code, you may not require SSH for this task, thanks to the robust integration of the [Dev Containers](https://code.visualstudio.com/docs/devcontainers/containers) extension with Docker. However, if you're using VSCodium, your best bet is the [Open Remote SSH](https://open-vsx.org/vscode/item?itemName=jeanp413.open-remote-ssh) extension. &NewLine;
Do note, VSCodium comes with certain limitations, such as the need to manually provision an SSH server and client - but hey that's the FOSS life. 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](https://code.visualstudio.com/docs/devcontainers/containers) extension with Docker. However, if you're using VSCodium, your best bet is the [Open Remote SSH](https://open-vsx.org/vscode/item?itemName=jeanp413.open-remote-ssh) extension.
To simplify the setup for a remote workspace in VSCodium, you can run `init-ssh` and it will provision everything necessary. You can confirm this by running `ssh root@{your.dockers.ip.address}`. 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}`.
</details> </details>
### Troubleshooting ### Troubleshooting
- If hot reloading doesn't work, first check the Metro logs with ./dockdroid.sh 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 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 have issues connecting via SSH, try running `ssh root@{your.docker.ip.address} -v` for more debug info. Often times the logs will be clear and verbose enough to instruct you further. - 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 will likely throw an error after recreating the container, and you'll have to run `./dockdroid.sh clear-known-hosts`. SSH in again to confirm it's fixed, and accept the new known_host entry.
- 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.

View File

@ -1,76 +0,0 @@
#!/bin/bash
if [[ -z "${RN_PROJECT_FOLDER_NAME}" ]]; then
echo "Please set the RN_PROJECT_FOLDER_NAME environment variable before running the script."
exit 1
fi
case $1 in
stop-metro)
docker exec -it ${RN_PROJECT_FOLDER_NAME} supervisorctl stop npm
;;
start-metro)
docker exec -it ${RN_PROJECT_FOLDER_NAME} supervisorctl start npm -- --reset-cache
docker logs -f ${RN_PROJECT_FOLDER_NAME}
;;
metro-logs)
docker exec -it ${RN_PROJECT_FOLDER_NAME} cat /var/log/app/npm.log
;;
connect-android)
docker exec -ti ${RN_PROJECT_FOLDER_NAME} adb connect host.docker.internal
;;
run-android)
docker exec -ti ${RN_PROJECT_FOLDER_NAME} adb connect host.docker.internal
docker exec -ti ${RN_PROJECT_FOLDER_NAME} npx react-native run-android
;;
reinstall-node-modules)
docker exec -it ${RN_PROJECT_FOLDER_NAME} /bin/bash -c "cd /home/${RN_PROJECT_FOLDER_NAME} && rm -rf node_modules && npm install"
;;
shell)
if [[ -z "${RN_PROJECT_FOLDER_NAME}" ]]; then
echo "Please set the RN_PROJECT_FOLDER_NAME environment variable before running the script."
exit 1
fi
docker exec -it ${RN_PROJECT_FOLDER_NAME} /bin/bash -c "cd /home/${RN_PROJECT_FOLDER_NAME} && exec /bin/bash"
;;
init-ssh)
if [[ -z "${SSH_KEY}" ]]; then
echo "Please set the SSH_KEY environment variable before running the script."
exit 1
fi
# generate ssh key
if [ ! -f "${SSH_KEY}" ]; then
ssh-keygen -t ed25519 -f "${SSH_KEY}" -N ""
echo "SSH key generated at ${SSH_KEY}"
else
echo "SSH key already exists at ${SSH_KEY}"
fi
# get docker container's IP
CONTAINER_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${CONTAINER_NAME})
# check if the docker IP is already in the ssh config
if grep -q "${CONTAINER_IP}" ~/.ssh/config
then
echo "SSH config already contains the Docker IP."
else
echo "Adding Docker IP to SSH config..."
echo -e "Host ${CONTAINER_IP}\n\tPort 22\n\tUser root\n\tPubkeyAuthentication yes\n\tIdentityFile ${SSH_KEY}" >> ~/.ssh/config
echo "Docker IP added to SSH config."
fi
;;
clear-known-hosts)
if [ "$EUID" -ne 0 ]
then
echo "Please run this command as root or use sudo."
exit 1
fi
CONTAINER_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${CONTAINER_NAME})
ssh-keygen -R ${CONTAINER_IP}
;;
*)
echo "Invalid command"
echo "Usage: $0 {logs|connect|run-android|stop}"
;;
esac

View File

@ -7,6 +7,13 @@ services:
dockerfile: Dockerfile dockerfile: Dockerfile
args: args:
- SSH_KEY=${SSH_KEY} - SSH_KEY=${SSH_KEY}
- RN_PROJECT_FOLDER_NAME=${RN_PROJECT_FOLDER_NAME}
- GH_TOKEN=${GH_TOKEN}
- NPM_TOKEN=${NPM_TOKEN}
- NODE_VERSION=${NODE_VERSION}
environment:
- GH_TOKEN=${GH_TOKEN}
- NPM_TOKEN=${NPM_TOKEN}
container_name: "${CONTAINER_NAME}" container_name: "${CONTAINER_NAME}"
volumes: volumes:
- type: bind - type: bind
@ -14,6 +21,3 @@ services:
target: /home/app target: /home/app
ports: ports:
- 8081:8081 - 8081:8081
environment:
- EXPO_DEVTOOLS_LISTEN_ADDRESS=0.0.0.0

162
dockman.sh Executable file
View File

@ -0,0 +1,162 @@
#!/bin/bash
# Exit immediately if a command exits with a non-zero status.
set -e
# Find the project root directory (i.e., the directory containing .dockerizedroot)
dir=$(pwd)
while [ "$dir" != "" ] && [ ! -f "$dir/.dockerizedroot" ]; do
dir=${dir%/*}
done
# Source the .env file
if [ "$dir" != "" ]; then
echo "Sourcing .env file from $dir"
source "$dir/.env"
fi
# Validate the container name
if [[ -z "${CONTAINER_NAME}" ]]; then
echo "Please set the CONTAINER_NAME environment variable before running the script."
exit 1
fi
# Define the project directory
PROJECT_DIR="/home/$RN_PROJECT_FOLDER_NAME"
# Docker command handling
case $1 in
up)
# List of required environment variables
REQUIRED_VARS=("SSH_KEY" "GH_TOKEN" "NPM_TOKEN" "NODE_VERSION" "RN_PROJECT_FOLDER_NAME" "CONTAINER_NAME")
for VAR in ${REQUIRED_VARS[@]}; do
if [[ -z "${!VAR}" ]]; then
echo "Error: Required environment variable $VAR is not set"
exit 1
fi
done
# If we get here, all variables are set
docker-compose up
;;
stop-metro)
docker exec -it ${CONTAINER_NAME} supervisorctl stop npm
;;
start-metro)
if [[ -z "${RN_PROJECT_FOLDER_NAME}" ]]; then
echo "Please set the RN_PROJECT_FOLDER_NAME environment variable before running the script."
exit 1
fi
docker exec ${CONTAINER_NAME} /bin/bash -c "cd ${PROJECT_DIR} && npm install" || { echo 'npm install failed' ; exit 1; }
docker exec ${CONTAINER_NAME} supervisorctl start npm
docker logs -f ${CONTAINER_NAME}
;;
metro-logs)
echo "Press CTRL + C to safely exit. It won't stop the server."
docker exec -it ${CONTAINER_NAME} supervisorctl tail -f npm stdout
;;
connect-android)
docker exec ${CONTAINER_NAME} adb connect host.docker.internal
;;
run-android)
docker exec ${CONTAINER_NAME} adb connect host.docker.internal
docker exec -ti ${CONTAINER_NAME} /bin/bash -c "cd ${PROJECT_DIR} && npx react-native run-android"
;;
reinstall-node-modules)
if [[ -z "${RN_PROJECT_FOLDER_NAME}" ]]; then
echo "Please set the RN_PROJECT_FOLDER_NAME environment variable before running the script."
exit 1
fi
docker exec ${CONTAINER_NAME} /bin/bash -c "cd ${PROJECT_DIR} && rm -rf node_modules && npm install" || { echo 'npm install failed' ; exit 1; }
;;
shell)
docker exec -it ${CONTAINER_NAME} /bin/bash -c "cd ${PROJECT_DIR} && exec /bin/bash"
;;
init-ssh)
if [[ -z "${SSH_KEY}" ]]; then
echo "Please set the SSH_KEY environment variable before running the script."
exit 1
fi
SSH_KEY_PATH="$HOME/.ssh/id_docker_dev"
# generate ssh key
if [ ! -f "${SSH_KEY_PATH}" ]; then
ssh-keygen -t ed25519 -f ${SSH_KEY_PATH} -N ""
echo "SSH key generated at ${SSH_KEY_PATH}"
else
echo "SSH key already exists at ${SSH_KEY_PATH}"
fi
# Check if Docker is running
if ! docker info >/dev/null 2>&1; then
echo "Docker is not running. Please start Docker and try again."
exit 1
fi
# Check if the specific container is running
if [ "$(docker inspect -f '{{.State.Running}}' ${CONTAINER_NAME})" != "true" ]; then
echo "The container ${CONTAINER_NAME} does not exist or is not running."
exit 1
fi
# get docker container's IP
CONTAINER_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${CONTAINER_NAME})
# check if the docker IP is already in the ssh config
if grep -q "${CONTAINER_IP}" ~/.ssh/config
then
echo "SSH config already contains the Docker IP."
else
echo "Adding Docker IP to SSH config..."
echo -e "Host ${CONTAINER_IP}\n\tPort 22\n\tUser root\n\tPubkeyAuthentication yes\n\tIdentityFile ${SSH_KEY_PATH}" >> ~/.ssh/config
echo "Docker IP added to SSH config."
fi
;;
clear-known-hosts)
echo "This will clear known hosts."
# EUID may not be available in all environments, instead you can use id -u to get the user ID
if [ "$(id -u)" -ne 0 ]
then
echo "Please run as root or use sudo"
exit 1
fi
CONTAINER_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${CONTAINER_NAME})
# Define known_hosts and temporary file path
KNOWN_HOSTS_PATH="$HOME/.ssh/known_hosts"
TEMP_FILE="$HOME/.ssh/temp"
# Check if the .ssh directory exists and is writable
if [ ! -d "$(dirname ${KNOWN_HOSTS_PATH})" ] || [ ! -w "$(dirname ${KNOWN_HOSTS_PATH})" ]; then
echo "The .ssh directory does not exist or is not writable"
exit 1
fi
# Check if the known_hosts file exists, if not, exit early as there's nothing to clear
if [ ! -f ${KNOWN_HOSTS_PATH} ]; then
echo "The known_hosts file does not exist. Nothing to clear."
exit 0
fi
# Create a temporary file and remove the line with CONTAINER_IP. If grep doesn't find a match, it will still create an empty file.
grep -v "${CONTAINER_IP}" ${KNOWN_HOSTS_PATH} > ${TEMP_FILE} || true
mv ${TEMP_FILE} ${KNOWN_HOSTS_PATH}
;;
*)
echo "Invalid command"
echo "Usage: $0 {stop-metro|start-metro|metro-logs|connect-android|run-android|reinstall-node-modules|shell|init-ssh|clear-known-hosts}"
;;
esac

View File

@ -1,15 +0,0 @@
#!/bin/bash
set -e
# Github auth for private npm packages
su -c "echo \"//github.com/:_authToken=$GH_TOKEN\" > /home/${RN_PROJECT_FOLDER_NAME}/.npmrc"
# install npm dependencies
su -c "cd /home/${RN_PROJECT_FOLDER_NAME} && npm install"
# remove the token
su -c "rm -f /home/${RN_PROJECT_FOLDER_NAME}/.npmrc"
# run supervisord
exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf

View File

@ -4,6 +4,12 @@ nodaemon=true
[unix_http_server] [unix_http_server]
file=/var/run/supervisor.sock file=/var/run/supervisor.sock
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock
[program:ssh] [program:ssh]
command=/usr/sbin/sshd -D command=/usr/sbin/sshd -D
user=root user=root
@ -13,10 +19,10 @@ stdout_logfile=/var/log/supervisor/%(program_name)s.log
stderr_logfile=/var/log/supervisor/%(program_name)s.log stderr_logfile=/var/log/supervisor/%(program_name)s.log
[program:npm] [program:npm]
command=bash -c "npm start -- --reset-cache" command=npm start -- --reset-cache
user=root user=root
autostart=false autostart=true
autorestart=false autorestart=true
directory=/home/app directory=/home/app
stdout_logfile=/var/log/app/%(program_name)s.log stdout_logfile=/var/log/app/%(program_name)s.log
stderr_logfile=/var/log/app/%(program_name)s.log stderr_logfile=/var/log/app/%(program_name)s.log